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
|
/* 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 "engines/util.h"
#include "mads/compression.h"
#include "mads/screen.h"
#include "mads/mads.h"
#include "mads/msurface.h"
#include "mads/resources.h"
#include "mads/sprites.h"
namespace MADS {
MADSEngine *MSurface::_vm = nullptr;
int MSurface::scaleValue(int value, int scale, int err) {
int scaled = 0;
while (value--) {
err -= scale;
while (err < 0) {
scaled++;
err += 100;
}
}
return scaled;
}
void MSurface::drawSprite(const Common::Point &pt, SpriteInfo &info, const Common::Rect &clipRect) {
enum {
kStatusSkip,
kStatusScale,
kStatusDraw
};
// NOTE: The current clipping code assumes that the top left corner of the clip
// rectangle is always 0, 0
assert(clipRect.top == 0 && clipRect.left == 0);
int errX = info.hotX * info.scaleX % 100;
int errY = info.hotY * info.scaleY % 100;
int scaledWidth = scaleValue(info.width, info.scaleX, errX);
int scaledHeight = scaleValue(info.height, info.scaleY, errY);
int x = pt.x, y = pt.y;
int clipX = 0, clipY = 0;
// Clip the sprite's width and height according to the clip rectangle's dimensions
// This clips the sprite to the bottom and right
if (x >= 0) {
scaledWidth = MIN<int>(x + scaledWidth, clipRect.right) - x;
} else {
clipX = x;
scaledWidth = x + scaledWidth;
}
if (y >= 0) {
scaledHeight = MIN<int>(y + scaledHeight, clipRect.bottom) - y;
} else {
clipY = y;
scaledHeight = y + scaledHeight;
}
// Check if sprite is inside the screen. If it's not, there's no need to draw it
if (scaledWidth + x <= 0 || scaledHeight + y <= 0) // check left and top (in case x,y are negative)
return;
if (scaledWidth <= 0 || scaledHeight <= 0) // check right and bottom
return;
int heightAmt = scaledHeight;
const byte *src = (const byte *)info.sprite->getPixels();
byte *dst = (byte *)getBasePtr(x - info.hotX - clipX, y - info.hotY - clipY);
int status = kStatusSkip;
byte *scaledLineBuf = new byte[scaledWidth];
while (heightAmt > 0) {
if (status == kStatusSkip) {
// Skip line
errY -= info.scaleY;
if (errY < 0)
status = kStatusScale;
else
src += info.width;
} else {
if (status == kStatusScale) {
// Scale current line
byte *lineDst = scaledLineBuf;
int curErrX = errX;
int width = scaledWidth;
const byte *tempSrc = src;
int startX = clipX;
while (width > 0) {
byte pixel = *tempSrc++;
curErrX -= info.scaleX;
while (curErrX < 0) {
if (startX == 0) {
*lineDst++ = pixel;
width--;
} else {
startX++;
}
curErrX += 100;
}
}
src += info.width;
status = kStatusDraw;
}
if (status == kStatusDraw && clipY == 0) {
// Draw previously scaled line
byte *tempDst = dst;
for (int lineX = 0; lineX < scaledWidth; lineX++) {
byte pixel = scaledLineBuf[lineX];
if (info.encoding & 0x80) {
if (pixel == 0x80) {
pixel = 0;
} else {
byte destPixel = *tempDst;
byte r, g, b;
r = CLIP((info.palette[destPixel * 3] * pixel) >> 10, 0, 31);
g = CLIP((info.palette[destPixel * 3 + 1] * pixel) >> 10, 0, 31);
b = CLIP((info.palette[destPixel * 3 + 2] * pixel) >> 10, 0, 31);
pixel = info.inverseColorTable[(b << 10) | (g << 5) | r];
}
}
if (pixel)
*tempDst = pixel;
tempDst++;
}
dst += pitch;
heightAmt--;
errY += 100;
if (errY >= 0)
status = kStatusSkip;
} else if (status == kStatusDraw && clipY < 0) {
clipY++;
errY += 100;
if (errY >= 0)
status = kStatusSkip;
}
}
}
delete[] scaledLineBuf;
}
void MSurface::scrollX(int xAmount) {
if (xAmount == 0)
return;
byte buffer[80];
int direction = (xAmount > 0) ? -1 : 1;
int xSize = ABS(xAmount);
assert(xSize <= 80);
byte *srcP = (byte *)getBasePtr(0, 0);
for (int y = 0; y < this->h; ++y, srcP += pitch) {
if (direction < 0) {
// Copy area to be overwritten
Common::copy(srcP, srcP + xSize, &buffer[0]);
// Shift the remainder of the line over the given area
Common::copy(srcP + xSize, srcP + this->w, srcP);
// Move buffered area to the end of the line
Common::copy(&buffer[0], &buffer[xSize], srcP + this->w - xSize);
} else {
// Copy area to be overwritten
Common::copy_backward(srcP + this->w - xSize, srcP + this->w, &buffer[80]);
// Shift the remainder of the line over the given area
Common::copy_backward(srcP, srcP + this->w - xSize, srcP + this->w);
// Move buffered area to the start of the line
Common::copy_backward(&buffer[80 - xSize], &buffer[80], srcP + xSize);
}
}
markAllDirty();
}
void MSurface::scrollY(int yAmount) {
if (yAmount == 0)
return;
int direction = (yAmount > 0) ? 1 : -1;
int ySize = ABS(yAmount);
assert(ySize < (this->h / 2));
assert(this->w == pitch);
int blockSize = ySize * this->w;
byte *tempData = new byte[blockSize];
byte *pixelsP = (byte *)getBasePtr(0, 0);
if (direction > 0) {
// Buffer the lines to be overwritten
byte *srcP = (byte *)getBasePtr(0, this->h - ySize);
Common::copy(srcP, srcP + (pitch * ySize), tempData);
// Vertically shift all the lines
Common::copy_backward(pixelsP, pixelsP + (pitch * (this->h - ySize)),
pixelsP + (pitch * this->h));
// Transfer the buffered lines top the top of the screen
Common::copy(tempData, tempData + blockSize, pixelsP);
} else {
// Buffer the lines to be overwritten
Common::copy(pixelsP, pixelsP + (pitch * ySize), tempData);
// Vertically shift all the lines
Common::copy(pixelsP + (pitch * ySize), pixelsP + (pitch * this->h), pixelsP);
// Transfer the buffered lines to the bottom of the screen
Common::copy(tempData, tempData + blockSize, pixelsP + (pitch * (this->h - ySize)));
}
markAllDirty();
delete[] tempData;
}
void MSurface::translate(Common::Array<RGB6> &palette) {
for (int y = 0; y < this->h; ++y) {
byte *pDest = (byte *)getBasePtr(0, y);
for (int x = 0; x < this->w; ++x, ++pDest) {
if (*pDest < 255) // scene 752 has some palette indices of 255
*pDest = palette[*pDest]._palIndex;
}
}
markAllDirty();
}
void MSurface::translate(byte map[PALETTE_COUNT]) {
for (int y = 0; y < this->h; ++y) {
byte *pDest = (byte *)getBasePtr(0, y);
for (int x = 0; x < this->w; ++x, ++pDest) {
*pDest = map[*pDest];
}
}
markAllDirty();
}
MSurface *MSurface::flipHorizontal() const {
MSurface *dest = new MSurface(this->w, this->h);
for (int y = 0; y < this->h; ++y) {
const byte *srcP = getBasePtr(this->w - 1, y);
byte *destP = dest->getBasePtr(0, y);
for (int x = 0; x < this->w; ++x)
*destP++ = *srcP--;
}
return dest;
}
void MSurface::copyRectTranslate(MSurface &srcSurface, const byte *paletteMap,
const Common::Point &destPos, const Common::Rect &srcRect) {
// Loop through the lines
for (int yCtr = 0; yCtr < srcRect.height(); ++yCtr) {
const byte *srcP = (const byte *)srcSurface.getBasePtr(srcRect.left, srcRect.top + yCtr);
byte *destP = (byte *)getBasePtr(destPos.x, destPos.y + yCtr);
// Copy the line over
for (int xCtr = 0; xCtr < srcRect.width(); ++xCtr, ++srcP, ++destP) {
*destP = paletteMap[*srcP];
}
}
addDirtyRect(Common::Rect(destPos.x, destPos.y, destPos.x + srcRect.width(),
destPos.y + srcRect.height()));
}
void MSurface::copyFrom(MSurface &src, const Common::Point &destPos, int depth,
DepthSurface *depthSurface, int scale, bool flipped, int transparentColor) {
int destX = destPos.x, destY = destPos.y;
int frameWidth = src.w;
int frameHeight = src.h;
int direction = flipped ? -1 : 1;
int highestDim = MAX(frameWidth, frameHeight);
bool lineDist[MADS_SCREEN_WIDTH];
int distXCount = 0, distYCount = 0;
if (scale != -1) {
int distCtr = 0;
int distIndex = 0;
do {
distCtr += scale;
if (distCtr < 100) {
lineDist[distIndex] = false;
}
else {
lineDist[distIndex] = true;
distCtr -= 100;
if (distIndex < frameWidth)
++distXCount;
if (distIndex < frameHeight)
++distYCount;
}
} while (++distIndex < highestDim);
destX -= distXCount / 2;
destY -= distYCount - 1;
}
// Special case for quicker drawing of non-scaled images
if (scale == 100 || scale == -1) {
// Copy the specified area
Common::Rect copyRect(0, 0, src.w, src.h);
if (destX < 0) {
copyRect.left += -destX;
destX = 0;
}
else if (destX + copyRect.width() > w) {
copyRect.right -= destX + copyRect.width() - w;
}
if (destY < 0) {
copyRect.top += -destY;
destY = 0;
}
else if (destY + copyRect.height() > h) {
copyRect.bottom -= destY + copyRect.height() - h;
}
if (!copyRect.isValidRect())
return;
if (flipped)
copyRect.moveTo(0, copyRect.top);
byte *data = src.getPixels();
byte *srcPtr = data + (src.w * copyRect.top + copyRect.left);
byte *destPtr = (byte *)getPixels() + (destY * pitch) + destX;
if (flipped)
srcPtr += copyRect.width() - 1;
// 100% scaling variation
for (int rowCtr = 0; rowCtr < copyRect.height(); ++rowCtr) {
// Copy each byte one at a time checking against the depth
for (int xCtr = 0; xCtr < copyRect.width(); ++xCtr) {
byte *srcP = srcPtr + xCtr * direction;
int pixelDepth = depthSurface == nullptr ? 15 :
depthSurface->getDepth(Common::Point(destX + xCtr, destY + rowCtr));
if ((depth <= pixelDepth) && (*srcP != transparentColor))
destPtr[xCtr] = *srcP;
}
srcPtr += src.w;
destPtr += this->w;
}
return;
}
// Start of draw logic for scaled sprites
const byte *srcPixelsP = src.getPixels();
int destRight = this->w - 1;
int destBottom = this->h - 1;
// Check x bounding area
int spriteLeft = 0;
int spriteWidth = distXCount;
int widthAmount = destX + distXCount - 1;
if (destX < 0) {
spriteWidth += destX;
spriteLeft -= destX;
}
widthAmount -= destRight;
if (widthAmount > 0)
spriteWidth -= widthAmount;
if (spriteWidth <= 0)
return;
int spriteRight = spriteLeft + spriteWidth;
if (flipped) {
destX += distXCount - 1;
spriteLeft = -(distXCount - spriteRight);
spriteRight = (-spriteLeft + spriteWidth);
}
// Check y bounding area
int spriteTop = 0;
int spriteHeight = distYCount;
int heightAmount = destY + distYCount - 1;
if (destY < 0) {
spriteHeight += destY;
spriteTop -= destY;
}
heightAmount -= destBottom;
if (heightAmount > 0)
spriteHeight -= heightAmount;
int spriteBottom = spriteTop + spriteHeight;
if (spriteHeight <= 0)
return;
byte *destPixelsP = this->getBasePtr(destX + spriteLeft, destY + spriteTop);
spriteLeft = spriteLeft * direction;
// Loop through the lines of the sprite
for (int yp = 0, sprY = -1; yp < frameHeight; ++yp, srcPixelsP += src.pitch) {
if (!lineDist[yp])
// Not a display line, so skip it
continue;
// Check whether the sprite line is in the display range
++sprY;
if ((sprY >= spriteBottom) || (sprY < spriteTop))
continue;
// Found a line to display. Loop through the pixels
const byte *srcP = srcPixelsP;
byte *destP = destPixelsP;
for (int xp = 0, sprX = -1; xp < frameWidth; ++xp, ++srcP) {
if (!lineDist[xp])
// Not a display pixel
continue;
++sprX;
if (sprX < spriteLeft || sprX >= spriteRight)
// Skip pixel if it's not in horizontal display portion
continue;
// Get depth of current output pixel in depth surface
Common::Point pt((destP - (byte *)getPixels()) % this->pitch,
(destP - (byte *)getPixels()) / this->pitch);
int pixelDepth = (depthSurface == nullptr) ? 15 : depthSurface->getDepth(pt);
if ((*srcP != transparentColor) && (depth <= pixelDepth))
*destP = *srcP;
destP += direction;
}
// Move to the next destination line
destPixelsP += this->pitch;
}
addDirtyRect(Common::Rect(destX, destY, destX + frameWidth, destY + frameHeight));
}
/*------------------------------------------------------------------------*/
int DepthSurface::getDepth(const Common::Point &pt) {
if (_depthStyle == 2) {
int bits = (3 - (pt.x % 4)) * 2;
byte v = *(const byte *)getBasePtr(pt.x >> 2, pt.y);
return v >> bits;
} else {
if (pt.x < 0 || pt.y < 0 || pt.x >= this->w || pt.y >= this->h)
return 0;
return *(const byte *)getBasePtr(pt.x, pt.y) & 0xF;
}
}
int DepthSurface::getDepthHighBit(const Common::Point &pt) {
if (_depthStyle == 2) {
int bits = (3 - (pt.x % 4)) * 2;
byte v = *(const byte *)getBasePtr(pt.x >> 2, pt.y);
return (v >> bits) & 2;
} else {
if (pt.x < 0 || pt.y < 0 || pt.x >= this->w || pt.y >= this->h)
return 0;
return *(const byte *)getBasePtr(pt.x, pt.y) & 0x80;
}
}
} // End of namespace MADS
|