aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/graphics/portrait.cpp
blob: 1208f8ed651fffbd33b01dbd0eb0ef6f0e1895b8 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/util.h"
#include "common/stack.h"
#include "graphics/primitives.h"

#include "sci/sci.h"
#include "sci/event.h"
#include "sci/engine/state.h"
#include "sci/graphics/gui.h"
#include "sci/graphics/screen.h"
#include "sci/graphics/palette.h"
#include "sci/graphics/portrait.h"
#include "sci/sound/audio.h"

namespace Sci {

Portrait::Portrait(ResourceManager *resMan, SciEvent *event, SciGui *gui, GfxScreen *screen, GfxPalette *palette, AudioPlayer *audio, Common::String resourceName)
	: _resMan(resMan), _event(event), _gui(gui), _screen(screen), _palette(palette), _audio(audio), _resourceName(resourceName) {
	init();
}

Portrait::~Portrait() {
	delete[] _bitmaps;
	delete[] _fileData;
}

void Portrait::init() {
	// .BIN files are loaded from actors directory and from .\ directory
	// header:
	// 3 bytes "WIN"
	// 2 bytes main width (should be the same as first bitmap header width)
	// 2 bytes main height (should be the same as first bitmap header height)
	// 2 bytes animation count
	// 2 bytes unknown
	// 2 bytes unknown
	// 4 bytes paletteSize (base 1)
	//  -> 17 bytes
	// paletteSize bytes paletteData
	// 14 bytes bitmap header
	//  -> 4 bytes unknown
	//  -> 2 bytes height
	//  -> 2 bytes width
	//  -> 6 bytes unknown
	// height * width bitmap data
	// another animation count times bitmap header and data
	int32 fileSize = 0;
	Common::SeekableReadStream *file =
		SearchMan.createReadStreamForMember("actors/" + _resourceName + ".bin");
	if (!file) {
		file = SearchMan.createReadStreamForMember(_resourceName + ".bin");
		if (!file)
			error("portrait %s.bin not found", _resourceName.c_str());
	}
	fileSize = file->size();
	_fileData = new byte[fileSize];
	file->read(_fileData, fileSize);
	delete file;

	if (strncmp((char *)_fileData, "WIN", 3)) {
		error("portrait %s doesn't have valid header", _resourceName.c_str());
	}
	_width = READ_LE_UINT16(_fileData + 3);
	_height = READ_LE_UINT16(_fileData + 5);
	_bitmapCount = READ_LE_UINT16(_fileData + 7);
	_bitmaps = new PortraitBitmap[_bitmapCount];

	uint16 portraitPaletteSize = READ_LE_UINT16(_fileData + 13);
	byte *data = _fileData + 17;
	// Read palette
	memset(&_portraitPalette, 0, sizeof(Palette));
	uint16 palSize = 0, palNr = 0;
	while (palSize < portraitPaletteSize) {
		_portraitPalette.colors[palNr].b = *data++;
		_portraitPalette.colors[palNr].g = *data++;
		_portraitPalette.colors[palNr].r = *data++;
		_portraitPalette.colors[palNr].used = 1;
		_portraitPalette.intensity[palNr] = 100;
		palNr++; palSize += 3;
	}

	// Read all bitmaps
	PortraitBitmap *curBitmap = _bitmaps;
	uint16 bitmapNr;
	uint16 bytesPerLine;

	for (bitmapNr = 0; bitmapNr < _bitmapCount; bitmapNr++) {
		curBitmap->width = READ_LE_UINT16(data + 2);
		curBitmap->height = READ_LE_UINT16(data + 4);
		bytesPerLine = READ_LE_UINT16(data + 6);
		if (bytesPerLine < curBitmap->width)
			error("kPortrait: bytesPerLine larger than actual width");
		curBitmap->extraBytesPerLine = bytesPerLine - curBitmap->width;
		curBitmap->rawBitmap = data + 14;
		data += 14 + (curBitmap->height * bytesPerLine);
		curBitmap++;
	}

	// Offset table follows
	curBitmap = _bitmaps;
	int32 offsetTableSize = READ_LE_UINT32(data);
	assert((bitmapNr + 1) * 14 <= offsetTableSize);
	data += 4;
	byte *dataOffsetTable = data + 14; // we skip first bitmap offsets
	for (bitmapNr = 0; bitmapNr < _bitmapCount; bitmapNr++) {
		curBitmap->displaceX = READ_LE_UINT16(dataOffsetTable);
		curBitmap->displaceY = READ_LE_UINT16(dataOffsetTable + 2);
		dataOffsetTable += 14;
		curBitmap++;
	}
	data += offsetTableSize;

	// raw lip-sync data follows
}

void Portrait::doit(Common::Point position, uint16 resourceId, uint16 noun, uint16 verb, uint16 cond, uint16 seq) {
	_position = position;

	// Now init audio and sync resource
	uint32 audioNumber = ((noun & 0xff) << 24) | ((verb & 0xff) << 16) | ((cond & 0xff) << 8) | (seq & 0xff);
	ResourceId syncResourceId = ResourceId(kResourceTypeSync36, resourceId, noun, verb, cond, seq);
	Resource *syncResource = _resMan->findResource(syncResourceId, true);
	uint syncOffset = 0;

	// Set the portrait palette
	_palette->set(&_portraitPalette, false, true);

	// Draw base bitmap
	drawBitmap(0);
	bitsShow();

	// Start playing audio...
	_audio->stopAudio();
	_audio->startAudio(resourceId, audioNumber);

	if (!syncResource) {
		// Getting the book in the book shop calls kPortrait where no sync exists
		// TODO: find out what to do then
		warning("kPortrait: no sync resource %d %X", resourceId, audioNumber);
		return;
	}

	// Do animation depending on sync resource till audio is done playing
	uint16 syncCue;
	int timerPosition, curPosition;
	sciEvent curEvent;
	bool userAbort = false;

	while ((syncOffset < syncResource->size - 2) && (!userAbort)) {
		timerPosition = (int16)READ_LE_UINT16(syncResource->data + syncOffset);
		syncOffset += 2;
		if (syncOffset < syncResource->size - 2) {
			syncCue = READ_LE_UINT16(syncResource->data + syncOffset);
			syncOffset += 2;
		} else {
			syncCue = 0xFFFF;
		}

		// Wait till syncTime passed, then show specific animation bitmap
		do {
			_gui->wait(1);
			curEvent = _event->get(SCI_EVENT_ANY);
			if (curEvent.type == SCI_EVENT_MOUSE_PRESS ||
				(curEvent.type == SCI_EVENT_KEYBOARD && curEvent.data == SCI_KEY_ESC) ||
				g_engine->shouldQuit())
				userAbort = true;
			curPosition = _audio->getAudioPosition();
		} while ((curPosition != -1) && (curPosition < timerPosition) && (!userAbort));

		if (syncCue != 0xFFFF) {
			// Display animation bitmap
			if (syncCue < _bitmapCount) {
				if (syncCue)
					drawBitmap(0); // Draw base bitmap first to get valid animation frame
				drawBitmap(syncCue);
				bitsShow();
			} else {
				warning("kPortrait: sync information tried to draw non-existant %d", syncCue);
			}
		}
	}

	if (userAbort)
		_audio->stopAudio();

	_resMan->unlockResource(syncResource);
}

void Portrait::drawBitmap(uint16 bitmapNr) {
	byte *data = _bitmaps[bitmapNr].rawBitmap;
	uint16 bitmapHeight = _bitmaps[bitmapNr].height;
	uint16 bitmapWidth = _bitmaps[bitmapNr].width;
	Common::Point bitmapPosition = _position;

	bitmapPosition.x += _bitmaps[bitmapNr].displaceX;
	bitmapPosition.y += _bitmaps[bitmapNr].displaceY;

	for (int y = 0; y < bitmapHeight; y++) {
		for (int x = 0; x < bitmapWidth; x++) {
			_screen->putPixelOnDisplay(bitmapPosition.x + x, bitmapPosition.y + y, _portraitPalette.mapping[*data++]);
		}
		data += _bitmaps[bitmapNr].extraBytesPerLine;
	}
}

void Portrait::bitsShow() {
	Common::Rect bitmapRect = Common::Rect(_width, _height);
	bitmapRect.moveTo(_position.x, _position.y);
	_screen->copyDisplayRectToScreen(bitmapRect);
	g_system->updateScreen();
}

} // End of namespace Sci