aboutsummaryrefslogtreecommitdiff
path: root/engines/prince/graphics.cpp
blob: 6be1782f1ca6e21b942125db149f8472e6d9a424 (plain)
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
/* 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 "prince/graphics.h"
#include "prince/prince.h"
#include "prince/mhwanh.h"

#include "graphics/palette.h"

#include "common/memstream.h"

namespace Prince {

GraphicsMan::GraphicsMan(PrinceEngine *vm) : _vm(vm), _changed(false) {
	initGraphics(640, 480, true);

	_frontScreen = new Graphics::Surface();
	_frontScreen->create(640, 480, Graphics::PixelFormat::createFormatCLUT8());

	_screenForInventory = new Graphics::Surface();
	_screenForInventory->create(640, 480, Graphics::PixelFormat::createFormatCLUT8());

	_mapScreen = new Graphics::Surface();
	_mapScreen->create(640, 480, Graphics::PixelFormat::createFormatCLUT8());

	_shadowTable70 = (byte *)malloc(256);
	_shadowTable50 = (byte *)malloc(256);

	_roomBackground = 0;
}

GraphicsMan::~GraphicsMan() {
	_frontScreen->free();
	delete _frontScreen;

	_screenForInventory->free();
	delete _screenForInventory;

	_mapScreen->free();
	delete _mapScreen;

	free(_shadowTable70);
	free(_shadowTable50);
}

void GraphicsMan::update(Graphics::Surface *screen) {
	if (_changed) {
		_vm->_system->copyRectToScreen((byte *)screen->getBasePtr(0, 0), 640, 0, 0, 640, 480);

		_vm->_system->updateScreen();
		_changed = false;
	}
}

void GraphicsMan::setPalette(const byte *palette) {
	_vm->_system->getPaletteManager()->setPalette(palette, 0, 256);
}

void GraphicsMan::change() {
	_changed = true;
}

void GraphicsMan::draw(Graphics::Surface *screen, const Graphics::Surface *s) {
	uint16 w = MIN(screen->w, s->w);
	const byte *src = (const byte *)s->getBasePtr(0, 0);
	byte *dst = (byte *)screen->getBasePtr(0, 0);
	for (uint y = 0; y < s->h; y++) {
		if (y < screen->h) {
			memcpy(dst, src, w);
		}
		src += s->pitch;
		dst += screen->pitch;
	}
	change();
}

// Black (value = 0) as a primary transparent color, fix for FLC animations
void GraphicsMan::drawTransparentSurface(Graphics::Surface *screen, int32 posX, int32 posY, const Graphics::Surface *s, int secondTransColor) {
	const byte *src1 = (const byte *)s->getBasePtr(0, 0);
	byte *dst1 = (byte *)screen->getBasePtr(posX, posY);
	for (int y = 0; y < s->h; y++) {
		if (y + posY < screen->h && y + posY >= 0) {
			const byte *src2 = src1;
			byte *dst2 = dst1;
			for (int x = 0; x < s->w; x++, src2++, dst2++) {
				if (*src2 && *src2 != secondTransColor) {
					if (x + posX < screen->w && x + posX >= 0) {
						*dst2 = *src2;
					}
				}
			}
		}
		src1 += s->pitch;
		dst1 += screen->pitch;
	}
	change();
}

/**
 * Similar to drawTransparentSurface but with use of shadowTable for color recalculation
 * and kShadowColor (191) as a transparent color.
 */
void GraphicsMan::drawAsShadowSurface(Graphics::Surface *screen, int32 posX, int32 posY, const Graphics::Surface *s, byte *shadowTable) {
	const byte *src1 = (const byte *)s->getBasePtr(0, 0);
	byte *dst1 = (byte *)screen->getBasePtr(posX, posY);
	for (int y = 0; y < s->h; y++) {
		if (y + posY < screen->h && y + posY >= 0) {
			const byte *src2 = src1;
			byte *dst2 = dst1;
			for (int x = 0; x < s->w; x++, src2++, dst2++) {
				if (*src2 == kShadowColor) {
					if (x + posX < screen->w && x + posX >= 0) {
						*dst2 = *(shadowTable + *dst2);
					}
				}
			}
		}
		src1 += s->pitch;
		dst1 += screen->pitch;
	}
}

/**
 * Used in glowing animation for inventory items. Creates special blendTable array of colors,
 * use black (0) as a transparent color.
 */
void GraphicsMan::drawTransparentWithBlendSurface(Graphics::Surface *screen, int32 posX, int32 posY, const Graphics::Surface *s) {
	const byte *src1 = (const byte *)s->getBasePtr(0, 0);
	byte *dst1 = (byte *)screen->getBasePtr(posX, posY);
	byte *blendTable = (byte *)malloc(256);
	for (int i = 0; i < 256; i++) {
		blendTable[i] = 255;
	}
	for (int y = 0; y < s->h; y++) {
		if (y + posY < screen->h && y + posY >= 0) {
			const byte *src2 = src1;
			byte *dst2 = dst1;
			for (int x = 0; x < s->w; x++, src2++, dst2++) {
				if (*src2) {
					if (x + posX < screen->w && x + posX >= 0) {
						*dst2 = getBlendTableColor(*src2, *dst2, blendTable);
					}
				}
			}
		}
		src1 += s->pitch;
		dst1 += screen->pitch;
	}
	free(blendTable);
	change();
}

/**
 * Similar to drawTransparentSurface but with with use of DrawNode as argument for Z axis sorting
 * and white (255) as transparent color.
 */
void GraphicsMan::drawTransparentDrawNode(Graphics::Surface *screen, DrawNode *drawNode) {
	byte *src1 = (byte *)drawNode->s->getBasePtr(0, 0);
	byte *dst1 = (byte *)screen->getBasePtr(drawNode->posX, drawNode->posY);
	for (int y = 0; y < drawNode->s->h; y++) {
		if (y + drawNode->posY < screen->h && y + drawNode->posY >= 0) {
			byte *src2 = src1;
			byte *dst2 = dst1;
			for (int x = 0; x < drawNode->s->w; x++, src2++, dst2++) {
				if (*src2 != 255) {
					if (x + drawNode->posX < screen->w && x + drawNode->posX >= 0) {
						*dst2 = *src2;
					}
				}
			}
		}
		src1 += drawNode->s->pitch;
		dst1 += screen->pitch;
	}
}

/**
 * Similar to drawTransparentDrawNode but with additional anti-aliasing code for sprite drawing.
 * Edge smoothing is based on 256 x 256 table of colors transition.
 * Algorithm is checking if currently drawing pixel is located next to the edge of sprite and if it makes jagged line.
 * If it does then this pixel is set with color from transition table calculated of original background pixel color
 * and sprite's edge pixel color.
 */
void GraphicsMan::drawTransparentWithTransDrawNode(Graphics::Surface *screen, DrawNode *drawNode) {
	// pos of first pixel for each row of source sprite
	byte *src1 = (byte *)drawNode->s->getBasePtr(0, 0);
	// pos of drawing first pixel for each row on screen surface
	byte *dst1 = (byte *)screen->getBasePtr(drawNode->posX, drawNode->posY);
	// trasition table for calculating new color value
	byte *transTableData = (byte *)drawNode->data;
	for (int y = 0; y < drawNode->s->h; y++) {
		if (y + drawNode->posY < screen->h && y + drawNode->posY >= 0) {
			// current pixel in row of source sprite
			byte *src2 = src1;
			// current pixel in row of screen surface
			byte *dst2 = dst1;
			for (int x = 0; x < drawNode->s->w; x++, src2++, dst2++) {
				if (x + drawNode->posX < screen->w && x + drawNode->posX >= 0) {
					if (*src2 != 255) {
						// if source sprite pixel is not mask color than draw it normally
						*dst2 = *src2;
					} else {
						// check for making jagged line
						if (x) {
							// not first pixel in row
							if (*(src2 - 1) == 255) {
								// if it has mask color to the left - check right
								if (x != drawNode->s->w - 1) {
									// not last pixel in row
									if (*(src2 + 1) == 255) {
										// pixel to the right with mask color - no anti-alias
										continue;
									}
									// it's not mask color to the right - we continue checking
								} else {
									// last pixel in row, no right check - no anti-alias
									continue;
								}
							}
							// it's not mask color to the left - we continue checking
						} else if (x != drawNode->s->w - 1) {
							// first pixel in row but not last - just right pixel checking
							if (*(src2 + 1) == 255) {
								// pixel to the right with mask color - no anti-alias
								continue;
							}
							// it's not mask color to the right - we continue checking
						} else {
							// it's first and last pixel in row at the same time (width = 1) - no anti-alias
							continue;
						}
						byte value = 0;
						if (y != drawNode->s->h - 1) {
							// not last row
							// check pixel below of current src2 pixel
							value = *(src2 + drawNode->s->pitch);
							if (value == 255) {
								// pixel below with mask color - check above
								if (y) {
									// not first row
									value = *(src2 - drawNode->s->pitch);
									if (value == 255) {
										// pixel above with mask color - no anti-alias
										continue;
									}
									// it's not mask color above - we draw as transition color
								} else {
									// first row - no anti-alias
									continue;
								}
							}
							// it's not mask color below - we draw as transition color
						} else if (y) {
							// last row - just check above
							value = *(src2 - drawNode->s->pitch);
							if (value == 255) {
								// pixel above with mask color - no anti-alias
								continue;
							}
							// it's not mask color above - we draw as transition color
						} else {
							// first and last row at the same time (height = 1) - no anti-alias
							continue;
						}
						// new color value based on original screen surface color and sprite's edge pixel color
						*dst2 = transTableData[*dst2 * 256 + value];
					}
				}
			}
		}
		// adding pitch to jump to next row of pixels
		src1 += drawNode->s->pitch;
		dst1 += screen->pitch;
	}
}

void GraphicsMan::drawMaskDrawNode(Graphics::Surface *screen, DrawNode *drawNode) {
	byte *maskData = (byte *)drawNode->data;
	byte *src1 = (byte *)drawNode->originalRoomSurface->getBasePtr(drawNode->posX, drawNode->posY);
	byte *dst1 = (byte *)screen->getBasePtr(drawNode->posX, drawNode->posY);
	int maskWidth = drawNode->width >> 3;
	int maskPostion = 0;
	int maskCounter = 128;
	for (int y = 0; y < drawNode->height; y++) {
		if (y + drawNode->posY < screen->h && y + drawNode->posY >= 0) {
			byte *src2 = src1;
			byte *dst2 = dst1;
			int tempMaskPostion = maskPostion;
			for (int x = 0; x < drawNode->width; x++, src2++, dst2++) {
				if (x + drawNode->posX < screen->w && x + drawNode->posX >= 0) {
					if ((maskData[tempMaskPostion] & maskCounter) != 0) {
						*dst2 = *src2;
					}
				}
				maskCounter >>= 1;
				if (maskCounter == 0) {
					maskCounter = 128;
					tempMaskPostion++;
				}
			}
		}
		src1 += drawNode->originalRoomSurface->pitch;
		dst1 += screen->pitch;
		maskPostion += maskWidth;
		maskCounter = 128;
	}
}

void GraphicsMan::drawAsShadowDrawNode(Graphics::Surface *screen, DrawNode *drawNode) {
	byte *shadowData = (byte *)drawNode->data;
	byte *src1 = (byte *)drawNode->s->getBasePtr(0, 0);
	byte *dst1 = (byte *)screen->getBasePtr(drawNode->posX, drawNode->posY);
	for (int y = 0; y < drawNode->s->h; y++) {
		if (y + drawNode->posY < screen->h && y + drawNode->posY >= 0) {
			byte *src2 = src1;
			byte *dst2 = dst1;
			for (int x = 0; x < drawNode->s->w; x++, src2++, dst2++) {
				if (*src2 == kShadowColor) {
					if (x + drawNode->posX < screen->w && x + drawNode->posX >= 0) {
						*dst2 = *(shadowData + *dst2);
					}
				}
			}
		}
		src1 += drawNode->s->pitch;
		dst1 += screen->pitch;
	}
}

void GraphicsMan::drawBackSpriteDrawNode(Graphics::Surface *screen, DrawNode *drawNode) {
	byte *src1 = (byte *)drawNode->s->getBasePtr(0, 0);
	byte *dst1 = (byte *)screen->getBasePtr(drawNode->posX, drawNode->posY);
	for (int y = 0; y < drawNode->s->h; y++) {
		if (y + drawNode->posY < screen->h && y + drawNode->posY >= 0) {
			byte *src2 = src1;
			byte *dst2 = dst1;
			for (int x = 0; x < drawNode->s->w; x++, src2++, dst2++) {
				if (*src2 != 255) {
					if (x + drawNode->posX < screen->w && x + drawNode->posX >= 0) {
						if (*dst2 == 255) {
							*dst2 = *src2;
						}
					}
				}
			}
		}
		src1 += drawNode->s->pitch;
		dst1 += screen->pitch;
	}
}

byte GraphicsMan::getBlendTableColor(byte pixelColor, byte backgroundPixelColor, byte *blendTable) {
	int currColor = 0;

	if (blendTable[pixelColor] != 255) {
		currColor = blendTable[pixelColor];
	} else {
		const byte *originalPalette = _vm->_roomBmp->getPalette();

		int redFirstOrg = originalPalette[pixelColor * 3] * _vm->_mst_shadow / 256;
		redFirstOrg = CLIP(redFirstOrg, 0, 255);
		if (_vm->_mst_shadow <= 256) {
			int redFirstBack = originalPalette[backgroundPixelColor * 3] * (256 - _vm->_mst_shadow) / 256;
			redFirstBack = CLIP(redFirstBack, 0, 255);
			redFirstOrg += redFirstBack;
			redFirstOrg = CLIP(redFirstOrg, 0, 255);
		}

		int greenFirstOrg = originalPalette[pixelColor * 3 + 1] * _vm->_mst_shadow / 256;
		greenFirstOrg = CLIP(greenFirstOrg, 0, 255);
		if (_vm->_mst_shadow <= 256) {
			int greenFirstBack = originalPalette[backgroundPixelColor * 3 + 1] * (256 - _vm->_mst_shadow) / 256;
			greenFirstBack = CLIP(greenFirstBack, 0, 255);
			greenFirstOrg += greenFirstBack;
			greenFirstOrg = CLIP(greenFirstOrg, 0, 255);
		}

		int blueFirstOrg = originalPalette[pixelColor * 3 + 2] * _vm->_mst_shadow / 256;
		blueFirstOrg = CLIP(blueFirstOrg, 0, 255);
		if (_vm->_mst_shadow <= 256) {
			int blueFirstBack = originalPalette[backgroundPixelColor * 3 + 2] * (256 - _vm->_mst_shadow) / 256;
			blueFirstBack = CLIP(blueFirstBack, 0, 255);
			blueFirstOrg += blueFirstBack;
			blueFirstOrg = CLIP(blueFirstOrg, 0, 255);
		}

		int bigValue = PrinceEngine::kIntMax; // infinity
		for (int j = 0; j < 256; j++) {
			int redSecondOrg = originalPalette[3 * j];
			int redNew = redFirstOrg - redSecondOrg;
			redNew = redNew * redNew;

			int greenSecondOrg = originalPalette[3 * j + 1];
			int greenNew = greenFirstOrg - greenSecondOrg;
			greenNew = greenNew * greenNew;

			int blueSecondOrg = originalPalette[3 * j + 2];
			int blueNew = blueFirstOrg - blueSecondOrg;
			blueNew = blueNew * blueNew;

			int sumOfColorValues = redNew + greenNew + blueNew;

			if (sumOfColorValues < bigValue) {
				bigValue = sumOfColorValues;
				currColor = j;
			}

			if (sumOfColorValues == 0) {
				break;
			}
		}
		blendTable[pixelColor] = currColor;
	}
	return currColor;
}

void GraphicsMan::makeShadowTable(int brightness, byte *shadowPalette) {
	int shadow =  brightness * 256 / 100;
	const byte *originalPalette = _vm->_roomBmp->getPalette();

	for (int i = 0; i < 256; i++) {
		int redFirstOrg = originalPalette[3 * i] * shadow / 256;
		int greenFirstOrg = originalPalette[3 * i + 1] * shadow / 256;
		int blueFirstOrg = originalPalette[3 * i + 2] * shadow / 256;

		int currColor = 0;
		int bigValue = 999999999; // infinity

		for (int j = 0; j < 256; j++) {
			int redSecondOrg = originalPalette[3 * j];
			int redNew = redFirstOrg - redSecondOrg;
			redNew = redNew * redNew;

			int greenSecondOrg = originalPalette[3 * j + 1];
			int greenNew = greenFirstOrg - greenSecondOrg;
			greenNew = greenNew * greenNew;

			int blueSecondOrg = originalPalette[3 * j + 2];
			int blueNew = blueFirstOrg - blueSecondOrg;
			blueNew = blueNew * blueNew;

			int sumOfColorValues = redNew + greenNew + blueNew;

			if (sumOfColorValues < bigValue) {
				bigValue = sumOfColorValues;
				currColor = j;
			}

			if (sumOfColorValues == 0) {
				break;
			}
		}
		shadowPalette[i] = currColor;
	}
}

} // End of namespace Prince