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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "graphics.h"
#include "disk.h"
#include "common/algorithm.h"
#include "parallaction/parallaction.h"
namespace Parallaction {
GfxObj::GfxObj(uint objType, Frames *frames, const char* name) : _frames(frames), _keep(true), x(0), y(0), z(0), _flags(kGfxObjNormal), type(objType), frame(0), layer(3), scale(100) {
if (name) {
_name = strdup(name);
} else {
_name = 0;
}
}
GfxObj::~GfxObj() {
delete _frames;
free(_name);
}
void GfxObj::release() {
// _keep = false;
delete this;
}
const char *GfxObj::getName() const {
return _name;
}
uint GfxObj::getNum() {
return _frames->getNum();
}
void GfxObj::getRect(uint f, Common::Rect &r) {
_frames->getRect(f, r);
}
byte *GfxObj::getData(uint f) {
return _frames->getData(f);
}
uint GfxObj::getRawSize(uint f) {
return _frames->getRawSize(f);
}
uint GfxObj::getSize(uint f) {
return _frames->getSize(f);
}
void GfxObj::setFlags(uint32 flags) {
_flags |= flags;
}
void GfxObj::clearFlags(uint32 flags) {
_flags &= ~flags;
}
GfxObj* Gfx::loadAnim(const char *name) {
Frames* frames = _disk->loadFrames(name);
assert(frames);
GfxObj *obj = new GfxObj(kGfxObjTypeAnim, frames, name);
assert(obj);
// animation Z is not set here, but controlled by game scripts and user interaction.
// it is always >=0 and <screen height
obj->transparentKey = 0;
_gfxobjList.push_back(obj);
return obj;
}
GfxObj* Gfx::loadGet(const char *name) {
GfxObj *obj = _disk->loadStatic(name);
assert(obj);
obj->z = kGfxObjGetZ; // this preset Z value ensures that get zones are drawn after doors but before animations
obj->type = kGfxObjTypeGet;
obj->transparentKey = 0;
_gfxobjList.push_back(obj);
return obj;
}
GfxObj* Gfx::loadDoor(const char *name) {
Frames *frames = _disk->loadFrames(name);
assert(frames);
GfxObj *obj = new GfxObj(kGfxObjTypeDoor, frames, name);
assert(obj);
obj->z = kGfxObjDoorZ; // this preset Z value ensures that doors are drawn first
obj->transparentKey = 0;
_gfxobjList.push_back(obj);
return obj;
}
void Gfx::clearGfxObjects(uint filter) {
GfxObjList::iterator b = _gfxobjList.begin();
GfxObjList::iterator e = _gfxobjList.end();
for ( ; b != e; ) {
if (((*b)->_flags & filter) != 0) {
b = _gfxobjList.erase(b);
} else {
b++;
}
}
}
void Gfx::showGfxObj(GfxObj* obj, bool visible) {
if (!obj) {
return;
}
if (visible) {
obj->setFlags(kGfxObjVisible);
} else {
obj->clearFlags(kGfxObjVisible);
}
}
bool compareZ(const GfxObj* a1, const GfxObj* a2) {
return a1->z < a2->z;
}
void Gfx::sortAnimations() {
GfxObjList::iterator first = _gfxobjList.begin();
GfxObjList::iterator last = _gfxobjList.end();
Common::sort(first, last, compareZ);
}
void Gfx::drawGfxObject(GfxObj *obj, Graphics::Surface &surf, bool scene) {
if (!obj->isVisible()) {
return;
}
Common::Rect rect;
byte *data;
obj->getRect(obj->frame, rect);
rect.translate(obj->x, obj->y);
data = obj->getData(obj->frame);
if (obj->getSize(obj->frame) == obj->getRawSize(obj->frame)) {
blt(rect, data, &surf, obj->layer, obj->scale, obj->transparentKey);
} else {
unpackBlt(rect, data, obj->getRawSize(obj->frame), &surf, obj->layer, obj->scale, obj->transparentKey);
}
}
void Gfx::drawGfxObjects(Graphics::Surface &surf) {
sortAnimations();
// TODO: some zones don't appear because of wrong masking (3 or 0?)
GfxObjList::iterator b = _gfxobjList.begin();
GfxObjList::iterator e = _gfxobjList.end();
for (; b != e; b++) {
drawGfxObject(*b, surf, true);
}
}
void Gfx::drawText(Font *font, Graphics::Surface* surf, uint16 x, uint16 y, const char *text, byte color) {
byte *dst = (byte*)surf->getBasePtr(x, y);
font->setColor(color);
font->drawString(dst, surf->w, text);
}
#if 0
void Gfx::unpackBlt(const Common::Rect& r, byte *data, uint size, Graphics::Surface *surf, uint16 z, byte transparentColor) {
byte *d = _unpackedBitmap;
while (size > 0) {
uint8 p = *data++;
size--;
uint8 color = p & 0xF;
uint8 repeat = (p & 0xF0) >> 4;
if (repeat == 0) {
repeat = *data++;
size--;
}
memset(d, color, repeat);
d += repeat;
}
blt(r, _unpackedBitmap, surf, z, transparentColor);
}
#endif
void Gfx::unpackBlt(const Common::Rect& r, byte *data, uint size, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor) {
byte *d = _unpackedBitmap;
uint pixelsLeftInLine = r.width();
while (size > 0) {
uint8 p = *data++;
size--;
uint8 color = p & 0xF;
uint8 repeat = (p & 0xF0) >> 4;
if (repeat == 0) {
repeat = *data++;
size--;
}
if (repeat == 0) {
// end of line
repeat = pixelsLeftInLine;
pixelsLeftInLine = r.width();
} else {
pixelsLeftInLine -= repeat;
}
memset(d, color, repeat);
d += repeat;
}
blt(r, _unpackedBitmap, surf, z, scale, transparentColor);
}
void Gfx::bltMaskScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor) {
if (scale == 100) {
// use optimized path
bltMaskNoScale(r, data, surf, z, transparentColor);
return;
}
Common::Rect q(r);
Common::Rect clipper(surf->w, surf->h);
q.clip(clipper);
if (!q.isValidRect()) return;
uint inc = r.width() * (100 - scale);
uint thr = r.width() * 100;
uint xAccum = 0, yAccum = 0;
Common::Point dp;
dp.x = q.left + (r.width() * (100 - scale)) / 200;
dp.y = q.top + (r.height() * (100 - scale)) / 100;
q.translate(-r.left, -r.top);
byte *s = data + q.left + q.top * r.width();
byte *d = (byte*)surf->getBasePtr(dp.x, dp.y);
uint line = 0, col = 0;
for (uint16 i = 0; i < q.height(); i++) {
yAccum += inc;
if (yAccum >= thr) {
yAccum -= thr;
s += r.width();
continue;
}
xAccum = 0;
byte *d2 = d;
col = 0;
for (uint16 j = 0; j < q.width(); j++) {
xAccum += inc;
if (xAccum >= thr) {
xAccum -= thr;
s++;
continue;
}
if (*s != transparentColor) {
byte v = _backgroundInfo->mask.getValue(dp.x + col, dp.y + line);
if (z >= v) *d2 = *s;
}
s++;
d2++;
col++;
}
s += r.width() - q.width();
d += surf->w;
line++;
}
}
void Gfx::bltMaskNoScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, byte transparentColor) {
if (!_backgroundInfo->mask.data || (z == LAYER_FOREGROUND)) {
// use optimized path
bltNoMaskNoScale(r, data, surf, transparentColor);
return;
}
Common::Point dp;
Common::Rect q(r);
Common::Rect clipper(surf->w, surf->h);
q.clip(clipper);
if (!q.isValidRect()) return;
dp.x = q.left;
dp.y = q.top;
q.translate(-r.left, -r.top);
byte *s = data + q.left + q.top * r.width();
byte *d = (byte*)surf->getBasePtr(dp.x, dp.y);
uint sPitch = r.width() - q.width();
uint dPitch = surf->w - q.width();
for (uint16 i = 0; i < q.height(); i++) {
for (uint16 j = 0; j < q.width(); j++) {
if (*s != transparentColor) {
byte v = _backgroundInfo->mask.getValue(dp.x + j, dp.y + i);
if (z >= v) *d = *s;
}
s++;
d++;
}
s += sPitch;
d += dPitch;
}
}
void Gfx::bltNoMaskNoScale(const Common::Rect& r, byte *data, Graphics::Surface *surf, byte transparentColor) {
Common::Point dp;
Common::Rect q(r);
Common::Rect clipper(surf->w, surf->h);
q.clip(clipper);
if (!q.isValidRect()) return;
dp.x = q.left;
dp.y = q.top;
q.translate(-r.left, -r.top);
byte *s = data + q.left + q.top * r.width();
byte *d = (byte*)surf->getBasePtr(dp.x, dp.y);
uint sPitch = r.width() - q.width();
uint dPitch = surf->w - q.width();
for (uint16 i = q.top; i < q.bottom; i++) {
for (uint16 j = q.left; j < q.right; j++) {
if (*s != transparentColor)
*d = *s;
s++;
d++;
}
s += sPitch;
d += dPitch;
}
}
void Gfx::blt(const Common::Rect& r, byte *data, Graphics::Surface *surf, uint16 z, uint scale, byte transparentColor) {
Common::Point dp;
Common::Rect q(r);
Common::Rect clipper(surf->w, surf->h);
q.clip(clipper);
if (!q.isValidRect()) return;
dp.x = q.left;
dp.y = q.top;
q.translate(-r.left, -r.top);
byte *s = data + q.left + q.top * r.width();
byte *d = (byte*)surf->getBasePtr(dp.x, dp.y);
uint sPitch = r.width() - q.width();
uint dPitch = surf->w - q.width();
if (_varRenderMode == 2) {
for (uint16 i = 0; i < q.height(); i++) {
for (uint16 j = 0; j < q.width(); j++) {
if (*s != transparentColor) {
if (_backgroundInfo->mask.data && (z < LAYER_FOREGROUND)) {
byte v = _backgroundInfo->mask.getValue(dp.x + j, dp.y + i);
if (z >= v) *d = 5;
} else {
*d = 5;
}
}
s++;
d++;
}
s += sPitch;
d += dPitch;
}
} else {
bltMaskScale(r, data, surf, z, scale, transparentColor);
}
}
} // namespace Parallaction
|