File: shape.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (398 lines) | stat: -rw-r--r-- 13,807 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "bladerunner/shape.h"

#include "bladerunner/bladerunner.h"
#include "bladerunner/mouse.h"

#include "common/debug.h"
#include "common/ptr.h"
#include "common/util.h"

#include "graphics/surface.h"

namespace BladeRunner {

bool Shape::load(Common::SeekableReadStream *stream) {
	_width = stream->readUint32LE();
	_height = stream->readUint32LE();
	uint32 size = stream->readUint32LE();

	if (size != (uint32)(_width * _height * 2)) {
		warning("Shape::load size mismatch (w %d, h %d, sz %d)", _width, _height, size);
		return false;
	}

	// Enfoce a reasonable size limit
	if (_width >= 2048 || _height >= 2048) {
		warning("Shape::load shape too big (%d, %d)", _width, _height);
	}

	_data = new byte[size];

	if (stream->read(_data, size) != size) {
		warning("Shape::load error reading shape (w %d, h %d, sz %d)", _width, _height, size);
		return false;
	}

	return true;
}

Shape::~Shape() {
	delete[] _data;
}

void Shape::drawFilledTriangleAux(Graphics::Surface &surface, const int &dst_x, const int &dst_y, int x1, int y1, int x2, int y2, int x3, int y3, uint32 colorRGB) const {
	// Code used is based on the Bresenham-like algorithm as described in:
	// https://mcejp.github.io/2020/11/06/bresenham.html
	// http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html

	const Vector2 triangleV1 = Vector2(x1, y1);
	const Vector2 triangleV2 = Vector2(x2, y2); // (V2, V3) should be the flat side, so either x2 == x3 or y2 == y3
	const Vector2 triangleV3 = Vector2(x3, y3);

	// vTmp1 will be moving along the "left" side of the triangle, and vTmp2 along the "right" side.
	Vector2 vTmp1 = triangleV1;
	Vector2 vTmp2 = triangleV1;

	bool swappedDx1WithDy1 = false;
	bool swappedDx2WithDy2 = false;

	int dx1 = abs(triangleV2.x - triangleV1.x);
	int dy1 = abs(triangleV2.y - triangleV1.y);

	if (dy1 > dx1) {
		SWAP(dy1, dx1);
		swappedDx1WithDy1 = true;
	}

	int dx2 = abs(triangleV3.x - triangleV1.x);
	int dy2 = abs(triangleV3.y - triangleV1.y);

	if (dy2 > dx2) {
		SWAP(dy2, dx2);
		swappedDx2WithDy2 = true;
	}

	int signx1 = (triangleV2.x - triangleV1.x) > 0 ? 1: -1;
	if (triangleV2.x == triangleV1.x) {
		signx1 = 0;
	}

	int signx2 = (triangleV3.x - triangleV1.x) > 0 ? 1: -1;
	if (triangleV3.x == triangleV1.x) {
		signx2 = 0;
	}

	int signy1 = (triangleV2.y - triangleV1.y) > 0 ? 1: -1;
	if (triangleV2.y == triangleV1.y) {
		signy1 = 0;
	}

	int signy2 = (triangleV3.y - triangleV1.y) > 0 ? 1: -1;
	if (triangleV3.y == triangleV1.y) {
		signy2 = 0;
	}

	int e1 = 2 * dy1 - dx1;
	int e2 = 2 * dy2 - dx2;

	for (int i = 0; i <= dx1; ++i) {
		// dx1 here may be dy1 (if they swapped above). It is whichever was the largest of the two.
		// We loop over each pixel of "horizontal" (triangle filling) line
		// check if paint goes from left endpoint of "horizontal" (triangle filling) line to right or other way round
		int leftEndPoint, rightEndPoint;

		if (triangleV2.y == triangleV3.y) {
			if (vTmp1.x < vTmp2.x) {
				leftEndPoint = ceil(vTmp1.x);
				rightEndPoint = int(vTmp2.x);
			} else {
				leftEndPoint = ceil(vTmp2.x);
				rightEndPoint = int(vTmp1.x);
			}
		} else {
			if (vTmp1.y < vTmp2.y) {
				leftEndPoint = ceil(vTmp1.y);
				rightEndPoint = int(vTmp2.y);
			} else {
				leftEndPoint = ceil(vTmp2.y);
				rightEndPoint = int(vTmp1.y);
			}
		}

		void *dstPtr;
		for (int xPos = leftEndPoint; xPos <= rightEndPoint; ++xPos) {
			if (triangleV2.y == triangleV3.y) {
				dstPtr = surface.getBasePtr(CLIP(dst_x + xPos, 0, surface.w - 1), CLIP(dst_y + (int)ceil(vTmp1.y), 0, surface.h - 1));
			} else {
				dstPtr = surface.getBasePtr(CLIP(dst_x + (int)ceil(vTmp1.x), 0, surface.w - 1), CLIP(dst_y + xPos, 0, surface.h - 1));
			}
			drawPixel(surface, dstPtr, colorRGB);
		}

		while (e1 >= 0) {
			if (swappedDx1WithDy1) {
				vTmp1.x += signx1;
			} else {
				vTmp1.y += signy1;
			}
			e1 = e1 - 2 * dx1;
		}

		if (swappedDx1WithDy1) {
			vTmp1.y += signy1;
		} else {
			vTmp1.x += signx1;
		}
		e1 = e1 + 2 * dy1;

		// Here we've rendered the next point on the "left" edge triangle line.
		// We now do the same for the "right" edge triangle line, until we are on
		// the same y-value as on the "left" edge triangle line.
		if (triangleV2.y == triangleV3.y) {
			while (vTmp2.y != vTmp1.y) {
				while (e2 >= 0) {
					if (swappedDx2WithDy2) {
						vTmp2.x += signx2;
					} else {
						vTmp2.y += signy2;
					}
					e2 = e2 - 2 * dx2;
				}

				if (swappedDx2WithDy2) {
					vTmp2.y += signy2;
				} else {
					vTmp2.x += signx2;
				}
				e2 = e2 + 2 * dy2;
			}
		} else {
			while (vTmp2.x != vTmp1.x) {
				while (e2 >= 0) {
					if (swappedDx2WithDy2) {
						vTmp2.x += signx2;
					} else {
						vTmp2.y += signy2;
					}
					e2 = e2 - 2 * dx2;
				}

				if (swappedDx2WithDy2) {
					vTmp2.y += signy2;
				} else {
					vTmp2.x += signx2;
				}
				e2 = e2 + 2 * dy2;
			}
		}
	}
}

void Shape::draw(Graphics::Surface &surface, int x, int y, uint16 drawModeBitFlags) const {
	int src_x = CLIP(-x, 0, _width);
	int src_y = CLIP(-y, 0, _height);

	int dst_x = CLIP<int>(x, 0, surface.w);
	int dst_y = CLIP<int>(y, 0, surface.h);

	int rect_w = MIN(CLIP(_width + x, 0, _width), surface.w - x);
	int rect_h = MIN(CLIP(_height + y, 0, _height), surface.h - y);

	if (rect_w <= 0 || rect_h <= 0) {
		// Checking here for negative values also,
		// prevents segmentation fault (in the for loop below)
		return;
	}

	if (drawModeBitFlags & Mouse::MouseDrawFlags::CUSTOM) {
		// for both scene exit cursors and static ESPER edge cursors
		// we choose 25x25px -- odd dimensions chosen so that pixel 12 is the absolute middle (with edges 0-24)
		rect_w = MIN(CLIP(25 + x, 0, 25), surface.w - x);
		rect_h = MIN(CLIP(25 + y, 0, 25), surface.h - y);

		const uint32 exitDemoShapeColors[3] = { surface.format.RGBToColor(255, 255, 143),
		                                        surface.format.RGBToColor(228, 108,  10),
		                                        surface.format.RGBToColor(140,   0,  37)  };

		const uint32 esperExitDemoShapeColor = surface.format.RGBToColor(230, 230, 230); //  For ESPER arrows
		Common::Rect tailRect;

		// We mask out the irrelevant bitflags and exploit the fact
		// that these exit cursor bitflags do not combine,
		// so only one of them can be set at any time.
		switch (drawModeBitFlags & ~0x007F) {
		case Mouse::MouseDrawFlags::EXIT_RIGHT:
			// 6 px h, 6 px w
			drawFilledTriangleAux(surface, dst_x, dst_y, 24, 12, 18,  6, 18, 18, exitDemoShapeColors[ (drawModeBitFlags & 0x7)/2 % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y, 16, 12, 10,  6, 10, 18, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 1) % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y,  8, 12,  2,  6,  2, 18, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 2) % 3]);
			break;

		case Mouse::MouseDrawFlags::EXIT_LEFT:
			drawFilledTriangleAux(surface, dst_x, dst_y,  0, 12,  6,  6,  6, 18, exitDemoShapeColors[ (drawModeBitFlags & 0x7)/2 % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y,  8, 12, 14,  6, 14, 18, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 1) % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y, 16, 12, 22,  6, 22, 18, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 2) % 3]);
			break;

		case Mouse::MouseDrawFlags::EXIT_UP:
			drawFilledTriangleAux(surface, dst_x, dst_y, 12,  0,  6,  6, 18,  6, exitDemoShapeColors[ (drawModeBitFlags & 0x7)/2 % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y, 12,  8,  6, 14, 18, 14, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 1) % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y, 12, 16,  6, 22, 18, 22, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 2) % 3]);
			break;

		case Mouse::MouseDrawFlags::EXIT_DOWN:
			drawFilledTriangleAux(surface, dst_x, dst_y, 12, 24,  6, 18, 18, 18, exitDemoShapeColors[ (drawModeBitFlags & 0x7)/2 % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y, 12, 16,  6, 10, 18, 10, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 1) % 3]);
			drawFilledTriangleAux(surface, dst_x, dst_y, 12,  8,  6,  2, 18,  2, exitDemoShapeColors[((drawModeBitFlags & 0x7)/2 + 2) % 3]);
			break;

		case Mouse::MouseDrawFlags::ESPER_RIGHT:
			drawFilledTriangleAux(surface, dst_x, dst_y, 24, 12, 12,  0, 12, 24, esperExitDemoShapeColor);
			tailRect = Common::Rect(dst_x +  4, dst_y + 6, dst_x + 12, dst_y + 18);
			surface.fillRect(tailRect, esperExitDemoShapeColor);
			break;

		case Mouse::MouseDrawFlags::ESPER_LEFT:
			drawFilledTriangleAux(surface, dst_x, dst_y,  0, 12, 12,  0, 12, 24, esperExitDemoShapeColor);
			tailRect = Common::Rect(dst_x + 12, dst_y + 6, dst_x + 20, dst_y + 18);
			surface.fillRect(tailRect, esperExitDemoShapeColor);
			break;

		case Mouse::MouseDrawFlags::ESPER_UP:
			drawFilledTriangleAux(surface, dst_x, dst_y, 12,  0,  0, 12, 24, 12, esperExitDemoShapeColor);
			tailRect = Common::Rect(dst_x +  6, dst_y + 12, dst_x + 18, dst_y + 20);
			surface.fillRect(tailRect, esperExitDemoShapeColor);
			break;

		case Mouse::MouseDrawFlags::ESPER_DOWN:
			drawFilledTriangleAux(surface, dst_x, dst_y, 12, 24,  0, 12, 24, 12, esperExitDemoShapeColor);
			tailRect = Common::Rect(dst_x +  6,  dst_y + 4, dst_x + 18, dst_y + 12);
			surface.fillRect(tailRect, esperExitDemoShapeColor);
			break;

		default:
			debug("Unsupported custom shape %d", drawModeBitFlags &~ 0xEF);
			break;
		}
	} else {
		const uint8 *src_p = _data + 2 * (src_y * _width + src_x);

		uint16 shpColor = 0;
		uint32 surfaceColorRGBPrev = 0;
		uint32 newSurfaceColorRGB = 0;
		uint8 a, r, g, b;
		uint8 rPrev, gPrev, bPrev;
		uint16 rgb16bitPrev = 0;
		uint16 rgb16bitAdd = 0;
		for (int yi = 0; yi != rect_h; ++yi) {
			for (int xi = 0; xi != rect_w; ++xi) {
				shpColor = READ_LE_UINT16(src_p);
				src_p += 2;

				getGameDataColor(shpColor, a, r, g, b);

				if (!a) {
					// Ignore the alpha in the output as it is inversed in the input
					void *dstPtr = surface.getBasePtr(CLIP(dst_x + xi, 0, surface.w - 1), CLIP(dst_y + yi, 0, surface.h - 1));
					if (drawModeBitFlags & Mouse::MouseDrawFlags::SPECIAL) {
						// It seems that the additive mode was supposed to be used only for cursor shapes
						// From testing, the only cursor shape that seems to work with it is the green rotating cursor
						// We add extra code here to cover the cases of the beta crosshairs cursor
						// being drawn a different color based on bullet power
						// The code for creating the specific color is custom.
						if (drawModeBitFlags & Mouse::MouseDrawFlags::REDCROSSHAIRS) {
							newSurfaceColorRGB = surface.format.RGBToColor((b & 0x8B) | (g >> 1), 0, 0);
						} else if (drawModeBitFlags & Mouse::MouseDrawFlags::YELLOWCROSSHAIRS) {
							newSurfaceColorRGB = surface.format.RGBToColor(b & 0xDF, (b & 0xA5) | (g >> 1), 0);
						} else if (drawModeBitFlags & Mouse::MouseDrawFlags::BLUECROSSHAIRS) {
							newSurfaceColorRGB = surface.format.RGBToColor(r, g, b);
						} else {
							// Additive modes
							getPixel(surface, dstPtr, surfaceColorRGBPrev);
							if (drawModeBitFlags & Mouse::MouseDrawFlags::ADDITIVE_MODE0) {
								// This code makes the cursor semi-transparent
								// but it may not be what the disassembly of the original was going for.
								newSurfaceColorRGB = surface.format.RGBToColor(r, g, b);
								newSurfaceColorRGB = (((uint16)surfaceColorRGBPrev >> 1) & 0xFBEF)
								                    + (((uint16)newSurfaceColorRGB >> 1) & 0xFBEF);
							} else if (drawModeBitFlags & Mouse::MouseDrawFlags::ADDITIVE_MODE1) {
								// This code may be closer to what the disassembly of the original was doing
								// for additive draw mode but it doesn't look well.
								surface.format.colorToRGB(surfaceColorRGBPrev, rPrev, gPrev, bPrev);
								rgb16bitPrev = (  ((uint16)(rPrev >> 3) << 10)
								                | ((uint16)(gPrev >> 3) <<  5)
								                | ((uint16)(bPrev >> 3)));
								rgb16bitAdd = (((uint16)rgb16bitPrev >> 1) & 0xFBEF)
								             + ((shpColor >> 1) & 0xFBEF);
								getGameDataColor(rgb16bitAdd, a, r, g, b);
								newSurfaceColorRGB = surface.format.RGBToColor(r, g, b);
							}
						}
					} else {
						newSurfaceColorRGB = surface.format.RGBToColor(r, g, b);
					}
					drawPixel(surface, dstPtr, newSurfaceColorRGB);
				}
			}
			src_p += 2 * (_width - rect_w);
		}
	}
}

Shapes::Shapes(BladeRunnerEngine *vm) {
	_vm = vm;
}

Shapes::~Shapes() {
	unload();
}

bool Shapes::load(const Common::String &container) {
	unload();

	Common::ScopedPtr<Common::SeekableReadStream> stream(_vm->getResourceStream(container));
	if (!stream) {
		warning("Shape::open failed to open '%s'", container.c_str());
		return false;
	}

	uint32 count = stream->readUint32LE();

	_shapes.resize(count);

	for (uint32 i = 0; i < count; ++i) {
		if (!_shapes[i].load(stream.get())) {
			return false;
		}
	}

	return true;
}

void Shapes::unload() {
	_shapes.clear();
}


} // End of namespace BladeRunner