1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "cruise/cruise_main.h"
#include "cruise/cruise.h"
namespace Cruise {
backgroundIncrustStruct backgroundIncrustHead;
void resetBackgroundIncrustList(backgroundIncrustStruct *pHead) {
pHead->next = NULL;
pHead->prev = NULL;
}
// blit background to another one
void addBackgroundIncrustSub1(int fileIdx, int X, int Y, char *ptr2, int16 scale, char *destBuffer, char *dataPtr) {
assert((dataPtr != NULL) && (*dataPtr != 0));
buildPolyModel(X, Y, scale, ptr2, destBuffer, dataPtr);
}
void backupBackground(backgroundIncrustStruct *pIncrust, int X, int Y, int width, int height, uint8* pBackground) {
pIncrust->saveWidth = width;
pIncrust->saveHeight = height;
pIncrust->saveSize = width * height;
pIncrust->savedX = X;
pIncrust->savedY = Y;
pIncrust->ptr = (uint8 *)MemAlloc(width * height);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int xp = j + X;
int yp = i + Y;
pIncrust->ptr[i * width + j] = ((xp < 0) || (yp < 0) || (xp >= 320) || (yp >= 200)) ?
0 : pBackground[yp * 320 + xp];
}
}
}
void restoreBackground(backgroundIncrustStruct *pIncrust) {
if (!pIncrust)
return;
if (pIncrust->type != 1)
return;
if (pIncrust->ptr == NULL)
return;
uint8* pBackground = backgroundScreens[pIncrust->backgroundIdx];
if (pBackground == NULL)
return;
backgroundChanged[pIncrust->backgroundIdx] = true;
int X = pIncrust->savedX;
int Y = pIncrust->savedY;
int width = pIncrust->saveWidth;
int height = pIncrust->saveHeight;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int xp = j + X;
int yp = i + Y;
if ((xp >= 0) && (yp >= 0) && (xp < 320) && (yp < 200))
pBackground[yp * 320 + xp] = pIncrust->ptr[i * width + j];
}
}
}
backgroundIncrustStruct *addBackgroundIncrust(int16 overlayIdx, int16 objectIdx, backgroundIncrustStruct *pHead, int16 scriptNumber, int16 scriptOverlay, int16 backgroundIdx, int16 saveBuffer) {
objectParamsQuery params;
getMultipleObjectParam(overlayIdx, objectIdx, ¶ms);
uint8 *ptr = filesDatabase[params.fileIdx].subData.ptr;
// Don't process any further if not a sprite or polygon
if (!ptr)
return NULL;
if ((filesDatabase[params.fileIdx].subData.resourceType != OBJ_TYPE_SPRITE) &&
(filesDatabase[params.fileIdx].subData.resourceType != OBJ_TYPE_POLY))
return NULL;
uint8 *backgroundPtr = backgroundScreens[backgroundIdx];
assert(backgroundPtr != NULL);
backgroundChanged[backgroundIdx] = true;
backgroundIncrustStruct *currentHead = pHead;
backgroundIncrustStruct *currentHead2 = currentHead->next;
while (currentHead2) {
currentHead = currentHead2;
currentHead2 = currentHead->next;
}
backgroundIncrustStruct *newElement = (backgroundIncrustStruct *)mallocAndZero(sizeof(backgroundIncrustStruct));
if (!newElement)
return NULL;
newElement->next = currentHead->next;
currentHead->next = newElement;
if (!currentHead2)
currentHead2 = pHead;
newElement->prev = currentHead2->prev;
currentHead2->prev = newElement;
newElement->objectIdx = objectIdx;
newElement->type = saveBuffer;
newElement->backgroundIdx = backgroundIdx;
newElement->overlayIdx = overlayIdx;
newElement->scriptNumber = scriptNumber;
newElement->scriptOverlayIdx = scriptOverlay;
newElement->X = params.X;
newElement->Y = params.Y;
newElement->scale = params.scale;
newElement->frame = params.fileIdx;
newElement->spriteId = filesDatabase[params.fileIdx].subData.index;
newElement->ptr = NULL;
strcpy(newElement->name, filesDatabase[params.fileIdx].subData.name);
if (filesDatabase[params.fileIdx].subData.resourceType == OBJ_TYPE_SPRITE) {
// sprite
int width = filesDatabase[params.fileIdx].width;
int height = filesDatabase[params.fileIdx].height;
if (saveBuffer == 1)
backupBackground(newElement, newElement->X, newElement->Y, width, height, backgroundPtr);
drawSprite(width, height, NULL, filesDatabase[params.fileIdx].subData.ptr, newElement->Y,
newElement->X, backgroundPtr, filesDatabase[params.fileIdx].subData.ptrMask);
} else {
// poly
if (saveBuffer == 1) {
int newX;
int newY;
int newScale;
char *newFrame;
int sizeTable[4]; // 0 = left, 1 = right, 2 = bottom, 3 = top
// this function checks if the dataPtr is not 0, else it retrieves the data for X, Y, scale and DataPtr again (OLD: mainDrawSub1Sub1)
flipPoly(params.fileIdx, (int16 *)filesDatabase[params.fileIdx].subData.ptr, params.scale, &newFrame, newElement->X, newElement->Y, &newX, &newY, &newScale);
// this function fills the sizeTable for the poly (OLD: mainDrawSub1Sub2)
getPolySize(newX, newY, newScale, sizeTable, (unsigned char*)newFrame);
int width = (sizeTable[1] + 2) - (sizeTable[0] - 2) + 1;
int height = sizeTable[3] - sizeTable[2] + 1;
backupBackground(newElement, sizeTable[0] - 2, sizeTable[2], width, height, backgroundPtr);
}
addBackgroundIncrustSub1(params.fileIdx, newElement->X, newElement->Y, NULL, params.scale, (char *)backgroundPtr, (char *)filesDatabase[params.fileIdx].subData.ptr);
}
return newElement;
}
void regenerateBackgroundIncrust(backgroundIncrustStruct *pHead) {
lastAni[0] = 0;
backgroundIncrustStruct *pl = pHead->next;
while (pl) {
backgroundIncrustStruct* pl2 = pl->next;
int frame = pl->frame;
if (frame < 0)
error("regenerateBackgroundIncrust() : Unexpected use of negative frame index");
if ((filesDatabase[frame].subData.ptr == NULL) || (strcmp(pl->name, filesDatabase[frame].subData.name))) {
frame = NUM_FILE_ENTRIES - 1;
if (loadFile(pl->name, frame, pl->spriteId) < 0)
frame = -1;
}
if (frame >= 0) {
if (filesDatabase[frame].subData.resourceType == OBJ_TYPE_SPRITE) {
// Sprite
int width = filesDatabase[frame].width;
int height = filesDatabase[frame].height;
drawSprite(width, height, NULL, filesDatabase[frame].subData.ptr, pl->Y, pl->X, backgroundScreens[pl->backgroundIdx], filesDatabase[frame].subData.ptrMask);
} else {
// Poly
addBackgroundIncrustSub1(frame, pl->X, pl->Y, NULL, pl->scale, (char *)backgroundScreens[pl->backgroundIdx], (char *)filesDatabase[frame].subData.ptr);
}
backgroundChanged[pl->backgroundIdx] = true;
}
pl = pl2;
}
lastAni[0] = 0;
}
void freeBackgroundIncrustList(backgroundIncrustStruct *pHead) {
backgroundIncrustStruct *pCurrent = pHead->next;
while (pCurrent) {
backgroundIncrustStruct *pNext = pCurrent->next;
if (pCurrent->ptr)
MemFree(pCurrent->ptr);
MemFree(pCurrent);
pCurrent = pNext;
}
resetBackgroundIncrustList(pHead);
}
void removeBackgroundIncrust(int overlay, int idx, backgroundIncrustStruct * pHead) {
objectParamsQuery params;
getMultipleObjectParam(overlay, idx, ¶ms);
int x = params.X;
int y = params.Y;
backgroundIncrustStruct *pCurrent = pHead->next;
while (pCurrent) {
if ((pCurrent->overlayIdx == overlay || overlay == -1) && (pCurrent->objectIdx == idx || idx == -1) && (pCurrent->X == x) && (pCurrent->Y == y))
pCurrent->type = -1;
pCurrent = pCurrent->next;
}
backgroundIncrustStruct *pCurrentHead = pHead;
pCurrent = pHead->next;
while (pCurrent) {
if (pCurrent->type == -1) {
backgroundIncrustStruct *pNext = pCurrent->next;
backgroundIncrustStruct *bx = pCurrentHead;
bx->next = pNext;
backgroundIncrustStruct *cx = pNext;
if (!pNext)
cx = pHead;
bx = cx;
bx->prev = pCurrent->next;
if (pCurrent->ptr)
MemFree(pCurrent->ptr);
MemFree(pCurrent);
pCurrent = pNext;
} else {
pCurrentHead = pCurrent;
pCurrent = pCurrent->next;
}
}
}
void unmergeBackgroundIncrust(backgroundIncrustStruct * pHead, int ovl, int idx) {
objectParamsQuery params;
getMultipleObjectParam(ovl, idx, ¶ms);
int x = params.X;
int y = params.Y;
backgroundIncrustStruct *pl = pHead;
backgroundIncrustStruct *pl2 = pl;
pl = pl2->next;
while (pl) {
pl2 = pl;
if ((pl->overlayIdx == ovl) || (ovl == -1)) {
if ((pl->objectIdx == idx) || (idx == -1)) {
if ((pl->X == x) && (pl->Y == y))
restoreBackground(pl);
}
}
pl = pl2->next;
}
}
} // End of namespace Cruise
|