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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
/* 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 "kyra/screen_v2.h"
#include "common/endian.h"
namespace Kyra {
Screen_v2::Screen_v2(KyraEngine_v1 *vm, OSystem *system) : Screen(vm, system), _wsaFrameAnimBuffer(0) {
_wsaFrameAnimBuffer = new uint8[1024];
assert(_wsaFrameAnimBuffer);
}
Screen_v2::~Screen_v2() {
delete[] _wsaFrameAnimBuffer;
}
uint8 *Screen_v2::generateOverlay(const uint8 *palette, uint8 *buffer, int startColor, uint16 factor) {
if (!palette || !buffer)
return buffer;
factor = MIN<uint16>(255, factor);
factor >>= 1;
factor &= 0xFF;
const byte col1 = palette[startColor * 3 + 0];
const byte col2 = palette[startColor * 3 + 1];
const byte col3 = palette[startColor * 3 + 2];
uint8 *dst = buffer;
*dst++ = 0;
for (int i = 1; i != 255; ++i) {
uint8 processedPalette[3];
const uint8 *src = palette + i*3;
byte col;
col = *src++;
col -= ((((col - col1) * factor) << 1) >> 8) & 0xFF;
processedPalette[0] = col;
col = *src++;
col -= ((((col - col2) * factor) << 1) >> 8) & 0xFF;
processedPalette[1] = col;
col = *src++;
col -= ((((col - col3) * factor) << 1) >> 8) & 0xFF;
processedPalette[2] = col;
*dst++ = findLeastDifferentColor(processedPalette, palette+3, 255)+1;
}
return buffer;
}
void Screen_v2::applyOverlay(int x, int y, int w, int h, int pageNum, const uint8 *overlay) {
if (pageNum == 0 || pageNum == 1)
addDirtyRect(x, y, w, h);
uint8 *dst = getPagePtr(pageNum) + y * 320 + x;
while (h--) {
for (int wi = 0; wi < w; ++wi) {
uint8 index = *dst;
*dst++ = overlay[index];
}
dst += 320 - w;
}
}
int Screen_v2::findLeastDifferentColor(const uint8 *paletteEntry, const uint8 *palette, uint16 numColors, bool skipSpecialColors) {
int m = 0x7fff;
int r = 0x101;
for (int i = 0; i < numColors; i++) {
if (skipSpecialColors && i >= 0xc0 && i <= 0xc3)
continue;
int v = paletteEntry[0] - *palette++;
int c = v * v;
v = paletteEntry[1] - *palette++;
c += (v * v);
v = paletteEntry[2] - *palette++;
c += (v * v);
if (c <= m) {
m = c;
r = i;
}
}
return r;
}
void Screen_v2::getFadeParams(const uint8 *palette, int delay, int &delayInc, int &diff) {
debugC(9, kDebugLevelScreen, "Screen_v2::getFadeParams(%p, %d, %p, %p)", (const void *)palette, delay, (const void *)&delayInc, (const void *)&diff);
int maxDiff = 0;
diff = 0;
int len = _use16ColorMode ? 48 : 768;
for (int i = 0; i < len; ++i) {
diff = ABS(palette[i] - _screenPalette[i]);
maxDiff = MAX(maxDiff, diff);
}
delayInc = delay << 8;
if (maxDiff != 0) {
delayInc /= maxDiff;
delayInc = MIN(delayInc, 0x7FFF);
}
delay = delayInc;
for (diff = 1; diff <= maxDiff; ++diff) {
if (delayInc >= 256)
break;
delayInc += delay;
}
}
void Screen_v2::copyWsaRect(int x, int y, int w, int h, int dimState, int plotFunc, const uint8 *src,
int unk1, const uint8 *unkPtr1, const uint8 *unkPtr2) {
uint8 *dstPtr = getPagePtr(_curPage);
uint8 *origDst = dstPtr;
const ScreenDim *dim = getScreenDim(dimState);
int dimX1 = dim->sx << 3;
int dimX2 = dim->w << 3;
dimX2 += dimX1;
int dimY1 = dim->sy;
int dimY2 = dim->h;
dimY2 += dimY1;
int temp = y - dimY1;
if (temp < 0) {
if ((temp += h) <= 0)
return;
else {
SWAP(temp, h);
y += temp - h;
src += (temp - h) * w;
}
}
temp = dimY2 - y;
if (temp <= 0)
return;
if (temp < h)
h = temp;
int srcOffset = 0;
temp = x - dimX1;
if (temp < 0) {
temp = -temp;
srcOffset = temp;
x += temp;
w -= temp;
}
int srcAdd = 0;
temp = dimX2 - x;
if (temp <= 0)
return;
if (temp < w) {
SWAP(w, temp);
temp -= w;
srcAdd = temp;
}
dstPtr += y * SCREEN_W + x;
uint8 *dst = dstPtr;
if (_curPage == 0 || _curPage == 1)
addDirtyRect(x, y, w, h);
clearOverlayRect(_curPage, x, y, w, h);
temp = h;
int curY = y;
while (h--) {
src += srcOffset;
++curY;
int cW = w;
switch (plotFunc) {
case 0:
memcpy(dst, src, cW);
dst += cW; src += cW;
break;
case 1:
while (cW--) {
uint8 d = *src++;
uint8 t = unkPtr1[d];
if (t != 0xFF)
d = unkPtr2[*dst + (t << 8)];
*dst++ = d;
}
break;
case 4:
while (cW--) {
uint8 d = *src++;
if (d)
*dst = d;
++dst;
}
break;
case 5:
while (cW--) {
uint8 d = *src++;
if (d) {
uint8 t = unkPtr1[d];
if (t != 0xFF)
d = unkPtr2[*dst + (t << 8)];
*dst = d;
}
++dst;
}
break;
case 8:
case 9:
while (cW--) {
uint8 d = *src++;
uint8 t = _shapePages[0][dst - origDst] & 7;
if (unk1 < t && (curY > _maskMinY && curY < _maskMaxY))
d = _shapePages[1][dst - origDst];
*dst++ = d;
}
break;
case 12:
case 13:
while (cW--) {
uint8 d = *src++;
if (d) {
uint8 t = _shapePages[0][dst - origDst] & 7;
if (unk1 < t && (curY > _maskMinY && curY < _maskMaxY))
d = _shapePages[1][dst - origDst];
*dst++ = d;
} else {
d = _shapePages[1][dst - origDst];
*dst++ = d;
}
}
break;
default:
break;
}
dst = (dstPtr += SCREEN_W);
src += srcAdd;
}
}
const uint8 *Screen_v2::getPtrToShape(const uint8 *shpFile, int shape) {
debugC(9, kDebugLevelScreen, "Screen_v2::getPtrToShape(%p, %d)", (const void *)shpFile, shape);
uint16 shapes = READ_LE_UINT16(shpFile);
if (shapes <= shape)
return 0;
uint32 offset = READ_LE_UINT32(shpFile + (shape << 2) + 2);
return shpFile + offset + 2;
}
uint8 *Screen_v2::getPtrToShape(uint8 *shpFile, int shape) {
debugC(9, kDebugLevelScreen, "Screen_v2::getPtrToShape(%p, %d)", (void *)shpFile, shape);
uint16 shapes = READ_LE_UINT16(shpFile);
if (shapes <= shape)
return 0;
uint32 offset = READ_LE_UINT32(shpFile + (shape << 2) + 2);
return shpFile + offset + 2;
}
int Screen_v2::getShapeScaledWidth(const uint8 *shpFile, int scale) {
debugC(9, kDebugLevelScreen, "Screen_v2::getShapeScaledWidth(%p, %d)", (const void*)shpFile, scale);
int width = READ_LE_UINT16(shpFile+3);
return (width * scale) >> 8;
}
int Screen_v2::getShapeScaledHeight(const uint8 *shpFile, int scale) {
debugC(9, kDebugLevelScreen, "Screen_v2::getShapeScaledHeight(%p, %d)", (const void*)shpFile, scale);
int height = shpFile[2];
return (height * scale) >> 8;
}
uint16 Screen_v2::getShapeSize(const uint8 *shp) {
debugC(9, kDebugLevelScreen, "Screen_v2::getShapeSize(%p)", (const void *)shp);
return READ_LE_UINT16(shp+6);
}
uint8 *Screen_v2::makeShapeCopy(const uint8 *src, int index) {
debugC(9, kDebugLevelScreen, "Screen_v2::makeShapeCopy(%p, %d)", (const void *)src, index);
const uint8 *shape = getPtrToShape(src, index);
if (!shape)
return 0;
int size = getShapeSize(shape);
uint8 *copy = new uint8[size];
assert(copy);
memcpy(copy, shape, size);
return copy;
}
int Screen_v2::getLayer(int x, int y) {
debugC(9, kDebugLevelScreen, "Screen_v2::getLayer(%d, %d)", x, y);
if (x < 0)
x = 0;
else if (x >= 320)
x = 319;
if (y < 0)
y = 0;
else if (y >= 144)
y = 143;
uint8 pixel = *(getCPagePtr(5) + y * 320 + x);
pixel &= 0x7F;
pixel >>= 3;
if (pixel < 1)
pixel = 1;
else if (pixel > 15)
pixel = 15;
return pixel;
}
int Screen_v2::getRectSize(int w, int h) {
debugC(9, kDebugLevelScreen, "Screen_v2::getRectSize(%d, %d)", w, h);
if (w > 320 || h > 200)
return 0;
return w*h;
}
void Screen_v2::setTextColorMap(const uint8 *cmap) {
debugC(9, kDebugLevelScreen, "Screen_v2::setTextColorMap(%p)", (const void *)cmap);
setTextColor(cmap, 0, 15);
}
void Screen_v2::wsaFrameAnimationStep(int x1, int y1, int x2, int y2,
int w1, int h1, int w2, int h2, int srcPage, int dstPage, int dim) {
if (!(w1 || h1 || w2 || h2))
return;
ScreenDim cdm = *getScreenDim(dim);
cdm.sx <<= 3;
cdm.w <<= 3;
int na = 0, nb = 0, nc = w2;
if (!calcBounds(cdm.w, cdm.h, x2, y2, w2, h2, na, nb, nc))
return;
const uint8 *src = getPagePtr(srcPage) + y1 * 320;
uint8 *dst = getPagePtr(dstPage) + (y2 + cdm.sy) * 320;
int u = -1;
do {
int t = (nb * h1) / h2;
if (t != u) {
u = t;
const uint8 *s = src + x1 + t * 320;
uint8 *dt = (uint8 *)_wsaFrameAnimBuffer;
t = w2 - w1;
if (!t) {
memcpy(dt, s, w2);
} else if (t > 0) {
if (w1 == 1) {
memset(dt, *s, w2);
} else {
t = ((((((w2 - w1 + 1) & 0xffff) << 8) / w1) + 0x100) & 0xffff) << 8;
int bp = 0;
for (int i = 0; i < w1; i++) {
int cnt = (t >> 16);
bp += (t & 0xffff);
if (bp > 0xffff) {
bp -= 0xffff;
cnt++;
}
memset(dt, *s++, cnt);
dt += cnt;
}
}
} else {
if (w2 == 1) {
*dt = *s;
} else {
t = (((((w1 - w2) & 0xffff) << 8) / w2) & 0xffff) << 8;
int bp = 0;
for (int i = 0; i < w2; i++) {
*dt++ = *s++;
bp += (t & 0xffff);
if (bp > 0xffff) {
bp -= 0xffff;
s++;
}
s += (t >> 16);
}
}
}
}
memcpy(dst + x2 + cdm.sx, _wsaFrameAnimBuffer + na, w2);
dst += 320;
} while (++nb < h2);
}
bool Screen_v2::calcBounds(int w0, int h0, int &x1, int &y1, int &w1, int &h1, int &x2, int &y2, int &w2) {
x2 = 0;
y2 = 0;
w2 = w1;
int t = x1 + w1;
if (t < 1) {
w1 = h1 = -1;
} else {
if (t <= x1) {
x2 = w1 - t;
w1 = t;
x1 = 0;
}
t = w0 - x1;
if (t < 1) {
w1 = h1 = -1;
} else {
if (t <= w1) {
w1 = t;
}
w2 -= w1;
t = h1 + y1;
if (t < 1) {
w1 = h1 = -1;
} else {
if (t <= y1) {
y2 = h1 - t;
h1 = t;
y1 = 0;
}
t = h0 - y1;
if (t < 1) {
w1 = h1 = -1;
} else {
if (t <= h1) {
h1 = t;
}
}
}
}
}
return (w1 == -1) ? false : true;
}
void Screen_v2::checkedPageUpdate(int srcPage, int dstPage) {
debugC(9, kDebugLevelScreen, "Screen_v2::checkedPageUpdate(%d, %d)", srcPage, dstPage);
const uint32 *src = (const uint32 *)getPagePtr(srcPage);
uint32 *dst = (uint32 *)getPagePtr(dstPage);
uint32 *page0 = (uint32 *)getPagePtr(0);
bool updated = false;
for (int y = 0; y < 200; ++y) {
for (int x = 0; x < 80; ++x, ++src, ++dst, ++page0) {
if (*src != *dst) {
updated = true;
*dst = *page0 = *src;
}
}
}
if (updated)
addDirtyRect(0, 0, 320, 200);
}
} // end of namespace Kyra
|