aboutsummaryrefslogtreecommitdiff
path: root/engines/cryomni3d/fixed_image.cpp
blob: c519eb9deacd0deea50d555495ddd17b566de4ac (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
/* 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/cryomni3d/fixed_image.h"

#include "common/file.h"
#include "common/system.h"
#include "graphics/surface.h"
#include "image/image_decoder.h"

namespace CryOmni3D {

ZonFixedImage::ZonFixedImage(CryOmni3DEngine &engine,
                             Inventory &inventory,
                             const Sprites &sprites,
                             const FixedImageConfiguration *configuration) :
	_engine(engine), _inventory(inventory), _sprites(sprites),
	_configuration(configuration),
	_callback(nullptr), _imageDecoder(nullptr), _imageSurface(nullptr) {
}

ZonFixedImage::~ZonFixedImage() {
	delete _imageDecoder;
}

void ZonFixedImage::run(const CallbackFunctor *callback) {
	_exit = false;
	_zonesMode = kZonesMode_None;

	_callback = callback;

	g_system->showMouse(true);
	while (!_exit) {
		(*_callback)(this);
	}
	_engine.waitMouseRelease();
	g_system->showMouse(false);

	// Deselect object
	_inventory.setSelectedObject(nullptr);

	delete _callback;
	_callback = nullptr;

	// Don't use the current _imageSurface, we may not own it anymore
	_imageSurface = nullptr;
}

// Just pass a const char * for zone because it's for workarounds and constructing a null String at almost each load call is inefficient
void ZonFixedImage::load(const Common::String &image, const char *zone) {
	_imageSurface = nullptr;
	delete _imageDecoder;
	_imageDecoder = nullptr;

	_imageDecoder = _engine.loadHLZ(image);
	if (!_imageDecoder) {
		error("Can't display fixed image");
	}
	_imageSurface = _imageDecoder->getSurface();

	const Common::String &zoneFName = zone == nullptr ? image : zone;
	loadZones(zoneFName);

#if 0
	// This is not correct but to debug zones I think it's OK
	Graphics::Surface *tmpSurf = (Graphics::Surface *) _imageSurface;
	for (Common::Array<Zone>::const_iterator it = _zones.begin(); it != _zones.end(); it++) {
		Common::Rect tmp = it->rect;
		tmpSurf->frameRect(tmp, 244);
	}
#endif

	_zonesMode = kZonesMode_Standard;
	_refreshCursor = true;

	display();

	// WORKAROUND: Wait for release after displaying the fixed image to avoid handling events due to mouse being pressed
	// There is this bug in game
	// Don't display cursor to prevent displaying an invalid cursor
	g_system->showMouse(false);
	g_system->updateScreen();
	_engine.waitMouseRelease();
	g_system->showMouse(true);
}

void ZonFixedImage::display() const {
	_engine.setupPalette(_imageDecoder->getPalette(), _imageDecoder->getPaletteStartIndex(),
	                     _imageDecoder->getPaletteColorCount());

	g_system->copyRectToScreen(_imageSurface->getPixels(), _imageSurface->pitch, 0, 0,
	                           _imageSurface->w, _imageSurface->h);
	g_system->updateScreen();
}

void ZonFixedImage::loadZones(const Common::String &image) {
	_zones.clear();

	Common::String fname(_engine.prepareFileName(image, "zon"));

	Common::File zonFile;
	if (!zonFile.open(fname)) {
		error("Can't open ZON file '%s'", fname.c_str());
	}

	int32 zonesNumber = zonFile.size() / 26;
	_zones.reserve(zonesNumber);

	_highLeftId = -1;
	_highRightId = -1;

	int leftSeen = 0x7fffffff; // MAX_INT
	int rightSeen = 0;
	Common::Array<Zone>::size_type index = 0;

	while (zonesNumber > 0) {
		Zone zone;
		zone.rect.left = zonFile.readSint16BE();
		zone.rect.top = zonFile.readSint16BE();
		zone.rect.right = zonFile.readSint16BE();
		zone.rect.bottom = zonFile.readSint16BE();
		zone.spriteId = zonFile.readSint16BE();
		zone.cursorId = _sprites.revMapSpriteId(zone.spriteId);
		zone.valid = true;
		zonFile.skip(16);

		_zones.push_back(zone);

		if (zone.cursorId == _configuration->spriteHigh) {
			if (leftSeen > zone.rect.right) {
				// The right side is at the leftest seen
				leftSeen = zone.rect.right;
				_highLeftId = index;
			}
			if (rightSeen < zone.rect.left) {
				// The left side is at the rightest seen
				rightSeen = zone.rect.left;
				_highRightId = index;
			}
		}

		zonesNumber--;
		index++;
	}
}

Common::Point ZonFixedImage::getZoneCenter(uint zoneId) const {
	if (zoneId >= _zones.size()) {
		error("Invalid zoneId %u/%u", zoneId, _zones.size());
	}
	const Common::Rect &rect = _zones[zoneId].rect;

	return Common::Point((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2);
}

void ZonFixedImage::manage() {
	_currentZone = -1;
	_zoneLow = false;
	_zoneHigh = false;
	_zoneHighLeft = false;
	_zoneHighRight = false;
	_zoneLeft = false;
	_zoneRight = false;
	_zoneQuestion = false;
	_zoneListen = false;
	_zoneSee = false;
	_zoneUse = false;
	_zoneSpeak = false;
	_usedObject = nullptr;
	_key.reset();

	// Force poll events even when we must refresh the cursor
	if (!_engine.pollEvents() && !_refreshCursor) {
		g_system->updateScreen();
		return;
	}
	_refreshCursor = false;

	// Feed the key for the caller
	_key = _engine.getNextKey();
	Common::Point mousePos = _engine.getMousePos();

	if (_key == Common::KEYCODE_ESCAPE) {
		_exit = true;
		return;
	} else if (_engine.shouldAbort()) {
		_exit = true;
		return;
	}

	if (_key == Common::KEYCODE_SPACE ||
	        _engine.getCurrentMouseButton() == 2 ||
	        mousePos.y > _configuration->toolbarTriggerY) {
		bool mustRedraw = _engine.displayToolbar(_imageSurface);
		// We just came back from toolbar: check if an object is selected and go into object mode
		if (_inventory.selectedObject()) {
			_zonesMode = kZonesMode_Object;
		}
		if (mustRedraw) {
			display();
		}
		// Return without any event to redo the loop and force refresh
		_refreshCursor = true;
		return;
	}

	Common::Array<Zone>::iterator zoneIt;
	for (zoneIt = _zones.begin(); zoneIt != _zones.end(); zoneIt++) {
		if (zoneIt->valid && zoneIt->rect.contains(mousePos)) {
			break;
		}
	}

	if (zoneIt != _zones.end()) {
		_currentZone = zoneIt - _zones.begin();
	} else {
		_currentZone = -1;
	}

	if (_zonesMode == kZonesMode_Standard) {
		if (zoneIt != _zones.end()) {
			_engine.setCursor(zoneIt->cursorId);
			if (_engine.getCurrentMouseButton() == 1) {
				handleMouseZones(zoneIt);
			}
		} else {
			_engine.setCursor(_configuration->spriteNothing);
		}
	} else if (_zonesMode == kZonesMode_Object) {
		Object *selectedObj = _inventory.selectedObject();
		if (!selectedObj) {
			// Normally useless but we never know
			_engine.setCursor(_configuration->spriteNothing);
		} else if (zoneIt != _zones.end()) {
			_engine.setCursor(selectedObj->idSA());
			if (_engine.getDragStatus() == kDragStatus_Finished) {
				// Just clicked, store the event and go back to standard mode
				_usedObject = selectedObj;
				_zonesMode = kZonesMode_Standard;
				// We changed mode: need to refresh
				_refreshCursor = true;
			}
		} else {
			_engine.setCursor(selectedObj->idSl());
		}

	}

	g_system->updateScreen();
}

void ZonFixedImage::handleMouseZones(const Common::Array<Zone>::const_iterator &currentZone) {
	if (currentZone->cursorId == _configuration->spriteLow) {
		_zoneLow = true;
	} else if (currentZone->cursorId == _configuration->spriteHigh) {
		Common::Array<Zone>::size_type id = currentZone - _zones.begin();
		if (id == _highLeftId) {
			_zoneHighLeft = true;
		} else if (id == _highRightId) {
			_zoneHighRight = true;
		} else {
			_zoneHigh = true;
		}
	} else if (currentZone->cursorId == _configuration->spriteLeft) {
		_zoneLeft = true;
	} else if (currentZone->cursorId == _configuration->spriteRight) {
		_zoneRight = true;
	} else if (currentZone->cursorId == _configuration->spriteQuestion) {
		_zoneQuestion = true;
	} else if (currentZone->cursorId == _configuration->spriteListen) {
		_zoneListen = true;
	} else if (currentZone->cursorId == _configuration->spriteSee) {
		_zoneSee = true;
	} else if (currentZone->cursorId == _configuration->spriteUse) {
		_zoneUse = true;
	} else if (currentZone->cursorId == _configuration->spriteSpeak) {
		_zoneSpeak = true;
	} else {
		error("Invalid cursor ID: %d in ImgFix", currentZone->cursorId);
	}
}

void ZonFixedImage::updateSurface(const Graphics::Surface *newSurface) {
	if (newSurface->w != _imageSurface->w ||
	        newSurface->h != _imageSurface->h ||
	        newSurface->format != _imageSurface->format) {
		error("New surface has invalid attributes");
	}

	// Be careful the surface must be destroyed after the fixed image has finished with it
	_imageSurface = newSurface;

	display();
}

} // End of namespace CryOmni3D