File: image.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 (377 lines) | stat: -rw-r--r-- 10,354 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
/* 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 "engines/stark/resources/image.h"

#include "common/debug.h"
#include "image/png.h"

#include "engines/stark/debug.h"
#include "engines/stark/formats/xrc.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/resources/location.h"
#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/settings.h"
#include "engines/stark/services/services.h"
#include "engines/stark/visual/effects/bubbles.h"
#include "engines/stark/visual/effects/fireflies.h"
#include "engines/stark/visual/effects/fish.h"
#include "engines/stark/visual/image.h"
#include "engines/stark/visual/text.h"

#include "math/line2d.h"
#include "math/vector2d.h"

namespace Stark {
namespace Resources {

Object *Image::construct(Object *parent, byte subType, uint16 index, const Common::String &name) {
	switch (subType) {
	case kImageSub2:
	case kImageSub3:
		return new ImageStill(parent, subType, index, name);
	case kImageText:
		return new ImageText(parent, subType, index, name);
	default:
		error("Unknown image subtype %d", subType);
	}
}

Image::~Image() {
	delete _visual;
}

Image::Image(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Object(parent, subType, index, name),
		_transparent(false),
		_transparentColor(0),
		_field_44_ADF(0),
		_field_48_ADF(30),
		_visual(nullptr) {
	_type = TYPE;
}

void Image::readData(Formats::XRCReadStream *stream) {
	_filename = stream->readString();
	_hotspot = stream->readPoint();
	_transparent = stream->readBool();
	_transparentColor = stream->readUint32LE();

	uint32 polygonCount = stream->readUint32LE();
	for (uint32 i = 0; i < polygonCount; i++) {
		Polygon polygon;

		uint32 pointCount = stream->readUint32LE();
		for (uint32 j = 0; j < pointCount; j++) {
			polygon.push_back(stream->readPoint());
		}

		_polygons.push_back(polygon);
	}

	_archiveName = stream->getArchiveName();
}

Visual *Image::getVisual() {
	initVisual();
	return _visual;
}

void Image::printData() {
	debug("filename: %s", _filename.toString().c_str());
	debug("hotspot: x %d, y %d", _hotspot.x, _hotspot.y);
	debug("transparent: %d", _transparent);
	debug("transparentColor: %d", _transparentColor);
	debug("field_44: %d", _field_44_ADF);
	debug("field_48: %d", _field_48_ADF);

	for (uint32 i = 0; i < _polygons.size(); i++) {
		Common::String description;
		for (uint32 j = 0; j < _polygons[i].size(); j++) {
			description += Common::String::format("(x %d, y %d) ", _polygons[i][j].x, _polygons[i][j].y);
		}
		debug("polygon %d: %s", i, description.c_str());
	}
}

int Image::indexForPoint(const Common::Point &point) const {
	int index = -1;
	for (uint32 i = 0; i < _polygons.size(); i++) {
		if (isPointInPolygon(_polygons[i], point)) {
			index = i;
		}
	}

	return index;
}

bool Image::isPointInPolygon(const Polygon &polygon, const Common::Point &point) const {
	if (polygon.size() <= 1) {
		return false; // Empty polygon
	}

	// A ray cast from the point
	Math::Segment2d testLine(Math::Vector2d(point.x, point.y), Math::Vector2d(-100, -100));

	// Special case the line created between the last point and the first
	Math::Vector2d prevPoint = Math::Vector2d(polygon.back().x, polygon.back().y);

	// Count the intersections of the ray with the polygon's edges
	int intersectCount = 0;
	for (uint32 j = 0; j < polygon.size(); j++) {
		Math::Vector2d curPoint = Math::Vector2d(polygon[j].x, polygon[j].y);

		if (Math::Segment2d(prevPoint, curPoint).intersectsSegment(testLine, nullptr)) {
			intersectCount++;
		}

		prevPoint = curPoint;
	}

	// If the ray crosses the polygon an odd number of times, the point is inside the polygon
	return intersectCount % 2 != 0;
}

Common::Point Image::getHotspotPosition(uint index) const {
	if (index >= _polygons.size()) {
		return Common::Point(-1, -1);
	}

	Polygon polygon = _polygons[index];

	// Return the top-middle point as the hotspot
	int right = polygon[0].x, top = polygon[0].y;

	for (uint i = 1; i < polygon.size(); ++i) {
		right += polygon[i].x;
		if (polygon[i].y < top) {
			top = polygon[i].y;
		}
	}

	right /= polygon.size();

	if (top < 0) {
		top = 0;
	}

	return Common::Point(right, top);
}

ImageStill::~ImageStill() {
}

ImageStill::ImageStill(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Image(parent, subType, index, name),
		_noName(false) {
}

void ImageStill::readData(Formats::XRCReadStream *stream) {
	Image::readData(stream);

	if (stream->isDataLeft()) {
		_field_44_ADF = stream->readUint32LE();
		_field_44_ADF /= 33;
	}

	if (stream->isDataLeft()) {
		_field_48_ADF = stream->readUint32LE();
	}

	_noName = _filename == "noname" || _filename == "noname.xmg";
}

void ImageStill::onPostRead() {
	initVisual();
}

void ImageStill::initVisual() {
	if (_visual) {
		return; // The visual is already there
	}

	if (_noName) {
		return; // No file to load
	}

	Common::ReadStream *xmgStream = StarkArchiveLoader->getFile(_filename, _archiveName);

	VisualImageXMG *visual = new VisualImageXMG(StarkGfx);

	if (StarkSettings->isAssetsModEnabled() && StarkGfx->supportsModdedAssets() && loadPNGOverride(visual)) {
		visual->readOriginalSize(xmgStream);
	} else {
		visual->load(xmgStream);
	}

	visual->setHotSpot(_hotspot);

	_visual = visual;

	delete xmgStream;
}

bool ImageStill::loadPNGOverride(VisualImageXMG *visual) const {
	if (!_filename.baseName().hasSuffixIgnoreCase(".xmg")) {
		return false;
	}

	Common::String pngFilename = _filename.baseName();
	pngFilename = Common::String(pngFilename.c_str(), pngFilename.size() - 4) + ".png";
	Common::Path pngFilePath = _filename.getParent().appendComponent(pngFilename);
	pngFilePath = StarkArchiveLoader->getExternalFilePath(pngFilePath, _archiveName);

	debugC(kDebugModding, "Attempting to load %s", pngFilePath.toString(Common::Path::kNativeSeparator).c_str());

	Common::SeekableReadStream *pngStream = SearchMan.createReadStreamForMember(pngFilePath);
	if (!pngStream) {
		return false;
	}

	if (!visual->loadPNG(pngStream)) {
		warning("Failed to load %s. It is not a valid PNG file.", pngFilePath.toString(Common::Path::kNativeSeparator).c_str());
		delete pngStream;
		return false;
	}

	debugC(kDebugModding, "Loaded %s", pngFilePath.toString(Common::Path::kNativeSeparator).c_str());

	delete pngStream;
	return true;
}

void ImageStill::printData() {
	Image::printData();
}

ImageText::ImageText(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Image(parent, subType, index, name),
		_color(Gfx::Color(0, 0, 0)),
		_font(0) {
}

ImageText::~ImageText() {
}

void ImageText::readData(Formats::XRCReadStream *stream) {
	Image::readData(stream);

	_size = stream->readPoint();
	_text = stream->readString();
	_color.r = stream->readByte();
	_color.g = stream->readByte();
	_color.b = stream->readByte();
	_color.a = 0xFF; stream->readByte();
	_font = stream->readUint32LE();

	// WORKAROUND: Give more space to text in the Archives' computer
	// So there are no line breaks in the French version of the game
	// when using a scaled font.
	Location *location = findParent<Location>();
	if (_name == "MAIN" && location && location->getName() == "Archive Database") {
		_size.x = 80;
	}
}

void ImageText::initVisual() {
	if (_visual) {
		return; // The visual is already there
	}

	if (_text.hasPrefix("GFX_Bubbles")) {
		VisualEffectBubbles *bubbles = new VisualEffectBubbles(StarkGfx, _size);
		bubbles->setParams(_text);
		_visual = bubbles;
	} else if (_text.hasPrefix("GFX_FireFlies")) {
		VisualEffectFireFlies *fireFlies = new VisualEffectFireFlies(StarkGfx, _size);
		fireFlies->setParams(_text);
		_visual = fireFlies;
	} else if (_text.hasPrefix("GFX_Fish")) {
		VisualEffectFish *fish = new VisualEffectFish(StarkGfx, _size);
		fish->setParams(_text);
		_visual = fish;
	} else if (_text.hasPrefix("GFX_")) {
		error("Unknown effect '%s'", _text.c_str());
	} else {
		VisualText *text = new VisualText(StarkGfx);
		text->setText(_text);
		text->setColor(_color);
		text->setTargetWidth(_size.x);
		text->setTargetHeight(_size.y);
		text->setFont(FontProvider::kCustomFont, _font);

		// WORKAROUND: Move the "White Cardinal" hotspot in the Archives'
		// computer so it matches the text. Fixes the hotspot being hard to
		// use with scaled fonts in the Spanish version of the game.
		if (_name == "The Church" && _polygons.size() == 2) {
			fixWhiteCardinalHotspot(text);
		}

		_visual = text;
	}
}

void ImageText::fixWhiteCardinalHotspot(VisualText *text) {
	Common::Rect textRect = text->getRect();

	Polygon &hotspotPoly = _polygons.back();
	if (hotspotPoly.size() != 4) {
		return;
	}

	Common::Point &topLeft     = hotspotPoly[0];
	Common::Point &topRight    = hotspotPoly[1];
	Common::Point &bottomRight = hotspotPoly[2];
	Common::Point &bottomLeft  = hotspotPoly[3];

	int16 hotspotHeight = bottomLeft.y - topLeft.y;
	if (hotspotHeight <= 0) {
		return;
	}

	bottomLeft .y = textRect.bottom;
	bottomRight.y = textRect.bottom;
	topLeft    .y = textRect.bottom - hotspotHeight;
	topRight   .y = textRect.bottom - hotspotHeight;
}

void ImageText::resetVisual() {
	if (!_visual) {
		return;
	}

	VisualText *text = _visual->get<VisualText>();
	if (text) {
		text->reset();
	}
}

void ImageText::printData() {
	Image::printData();

	debug("size: x %d, y %d", _size.x, _size.y);
	debug("text: %s", _text.c_str());
	debug("color: (%d, %d, %d, %d)", _color.r, _color.g, _color.b, _color.a);
	debug("font: %d", _font);
}

} // End of namespace Resources
} // End of namespace Stark