aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/graphics/robot.cpp
blob: 8cd82a87723a264445ba280589f0aaf063b66c48 (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
/* 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$
 *
 */

// fopen() and friends so we can dump frames and stuff to disk, for debugging
// #define FORBIDDEN_SYMBOL_ALLOW_ALL 

#include "sci/sci.h"
#include "sci/engine/state.h"
#include "sci/graphics/screen.h"
#include "sci/graphics/robot.h"
#include "sci/graphics/palette.h"
#include "sci/sound/audio.h"

#include "graphics/surface.h"

#include "sound/audiostream.h"
#include "sound/mixer.h"

#include "common/file.h"
#include "common/system.h"
#include "common/memstream.h"

namespace Sci {

// Some non technical information on robot files, from an interview with
// Greg Tomko-Pavia of Sierra On-Line
// Taken from http://anthonylarme.tripod.com/phantas/phintgtp.html
//
// (...) What we needed was a way of playing video, but have it blend into
// normal room art instead of occupying its own rectangular area. Room art 
// consists of a background pic overlaid with various animating cels 
// (traditional lingo: sprites). The cels each have a priority that determines 
// who is on top and who is behind in the drawing order. Cels are read from 
// *.v56 files (another proprietary format). A Robot is video frames with 
// transparent background including priority and x,y information. Thus, it is 
// like a cel, except it comes from an RBT - not a v56. Because it blends into
// our graphics engine, it looks just like a part of the room. A RBT can move 
// around the screen and go behind other objects. (...)

#ifdef ENABLE_SCI32
GfxRobot::GfxRobot(ResourceManager *resMan, GfxScreen *screen, GfxPalette *palette)
	: _resMan(resMan), _screen(screen), _palette(palette) {
	_resourceId = -1;
	_x = _y = 0;
}

GfxRobot::~GfxRobot() {
	delete[] _resourceData;
	delete[] _imageStart;
	delete[] _audioStart;
	delete[] _audioLen;
}

void GfxRobot::init(GuiResourceId resourceId, uint16 x, uint16 y) {
	char fileName[10];
	uint32 fileSize;

	//	resourceId = 1305;	// debug

	_resourceId = resourceId;
	_x = x;
	_y = y;
	_curFrame = 0;
	sprintf(fileName, "%d.rbt", _resourceId);

	Common::File robotFile;
	if (robotFile.open(fileName)) {
		_resourceData = new byte[robotFile.size()];
		robotFile.read(_resourceData, robotFile.size());
		fileSize = robotFile.size();
		robotFile.close();
	} else {
		warning("Unable to open robot file %s", fileName);
		return;
	}

	_frameCount = READ_LE_UINT16(_resourceData + 14);

	// There is another value in the header, at offset 0x12, which
	// equals this value plus 14 (audio header size) in the cases
	// I've seen so far. Dunno which one to use, really.
	_audioSize = READ_LE_UINT16(_resourceData + 60);

	//_frameSize = READ_LE_UINT32(_resourceData + 34);
	_hasSound = (_resourceData[25] != 0);

	_palOffset = 60;

	// Some robot files have sound, which doesn't start from frame 0
	// (e.g. Phantasmagoria, robot 1305)
	// Yes, strangely, the word at offset 10 is non-zero if it DOESN'T have a preload
	// in that case, the word is equal to that value which would otherwise be stored
	// in the audio header (i.e. _audioSize above)
	if (_hasSound && READ_LE_UINT16(_resourceData + 10) == 0)
		_palOffset += READ_LE_UINT32(_resourceData + 60) + 14;

	getFrameOffsets();
	assert(_imageStart[_frameCount] == fileSize);

	setPalette();

	debug("Robot %d, %d frames, sound: %s\n", resourceId, _frameCount, _hasSound ? "yes" : "no");
}

void GfxRobot::setPalette() {
	byte *paletteData = _resourceData + _palOffset;
	uint16 paletteSize = READ_LE_UINT16(_resourceData + 16);

	Palette resourcePal;

	byte robotPal[256 * 4];
	int startIndex = READ_LE_UINT16(paletteData + 25);
	int colorCount = READ_LE_UINT16(paletteData + 29);

	warning("%d palette entries starting at %d", colorCount, startIndex);
	
	_palette->createFromData(paletteData, paletteSize, &resourcePal);

	for (int i = 0; i < 256; ++i) {
		_savedPal[i * 4 + 0] = _palette->_sysPalette.colors[i].r;
		_savedPal[i * 4 + 1] = _palette->_sysPalette.colors[i].g;
		_savedPal[i * 4 + 2] = _palette->_sysPalette.colors[i].b;
		_savedPal[i * 4 + 3] = 0;
	}
	
	memcpy(robotPal, _savedPal, sizeof(_savedPal));

	for (int i = 0; i < colorCount; ++i) {
		int index = i + startIndex;
		robotPal[index * 4 + 0] = resourcePal.colors[index].r;
		robotPal[index * 4 + 1] = resourcePal.colors[index].g;
		robotPal[index * 4 + 2] = resourcePal.colors[index].b;
		robotPal[index * 4 + 3] = 0;
	}

	g_system->setPalette(robotPal, 0, 256);
}

void GfxRobot::drawNextFrame() {
	int width, height;

	byte *pixels = assembleVideoFrame(_curFrame);
	getFrameDimensions(_curFrame, width, height);
	g_system->copyRectToScreen(pixels, width, _x, _y, width, height * getFrameScale(_curFrame) / 100);
	g_system->updateScreen();
	g_system->delayMillis(100);
	delete[] pixels;

	_curFrame++;

	if (_curFrame == _frameCount) {
		// End of robot video, restore palette
		g_system->setPalette(_savedPal, 0, 256);
		_resourceId = -1;
	}
}

void GfxRobot::getFrameOffsets() {
	int *audioEnd = new int[_frameCount];
	int *videoEnd = new int[_frameCount];

	for (int i = 0; i < _frameCount; ++i) {
		videoEnd[i] = READ_LE_UINT16(_resourceData + _palOffset + 1200 + i * 2);
		audioEnd[i] = READ_LE_UINT16(_resourceData + _palOffset + 1200 + _frameCount * 2 + i * 2);
	}

	int frameDataOffset = _palOffset + 0x4b0 + 0x400 + 0x200 + _frameCount * 4;
	
	// Pad to nearest 2 kilobytes
	if (frameDataOffset & 0x7ff)
		frameDataOffset = (frameDataOffset & ~0x7ff)+0x800;

	_imageStart = new uint32[_frameCount + 1];
	_audioStart = new uint32[_frameCount];
	_audioLen = new uint32[_frameCount];

	_imageStart[0] = frameDataOffset;

	// Plus one so we can assert on this in the calling routine
	// The last one should point to end-of-file, unless I'm misunderstanding something
	for (int i = 1; i < _frameCount + 1; ++i) 
		_imageStart[i] = _imageStart[i - 1] + audioEnd[i - 1];
	for (int i = 0; i < _frameCount; ++i)
		_audioStart[i] = _imageStart[i] + videoEnd[i];
	for (int i = 0; i < _frameCount; ++i)
		_audioLen[i] = _imageStart[i + 1] - _audioStart[i];

	delete[] audioEnd;
	delete[] videoEnd;
}

byte *GfxRobot::assembleVideoFrame(int frame) {
	byte *videoData = _resourceData + _imageStart[frame];
	int frameWidth = READ_LE_UINT16(videoData + 4);
	int frameHeight = READ_LE_UINT16(videoData + 6);
	int frameFragments = READ_LE_UINT16(videoData + 18);

	int decompressedSize = 0;

	DecompressorLZS lzs;

	videoData = _resourceData + _imageStart[frame] + 24;
	
	for (int i = 0; i < frameFragments; ++i) {
		int fragmentCompressed = READ_LE_UINT32(videoData);
		int fragmentUncompressed = READ_LE_UINT32(videoData + 4);

		decompressedSize += fragmentUncompressed;
		videoData += 10 + fragmentCompressed;
	}

	assert(decompressedSize == (frameWidth * frameHeight) * getFrameScale(frame) / 100);

	byte *output = new byte[decompressedSize];
	int assemblePtr = 0;

	videoData = _resourceData + _imageStart[frame] + 24;

	for (int i = 0; i < frameFragments; ++i) {
		int fragmentCompressed = READ_LE_UINT32(videoData);
		int fragmentUncompressed = READ_LE_UINT32(videoData + 4);
		uint16 compressionType = READ_LE_UINT16(videoData + 8);

		switch (compressionType) {
		case 0: {
			Common::MemoryReadStream compressedFrame(videoData + 10, fragmentCompressed, DisposeAfterUse::NO);

			lzs.unpack(&compressedFrame, output + assemblePtr, fragmentCompressed, fragmentUncompressed);
			assemblePtr += fragmentUncompressed;
			break;
		}
		case 2: // Untested
			memcpy(output + assemblePtr, videoData + 10, fragmentCompressed);
			assemblePtr += fragmentUncompressed;
			break;
		}
		
		videoData += 10 + fragmentCompressed;
	}

	assert(assemblePtr == decompressedSize);

	//	FILE *f = fopen("/tmp/flap", "w");
	//      fwrite(output, 1, decompressedSize, f);
	//      fclose(f);
	//      exit(1);

	return output;
}

void GfxRobot::getFrameDimensions(int frame, int &width, int &height) {
	byte *videoData = _resourceData + _imageStart[frame];

	width = READ_LE_UINT16(videoData + 4);
	height = READ_LE_UINT16(videoData + 6);
}

void GfxRobot::getFrameRect(int frame, Common::Rect &rect) {
	byte *videoData = _resourceData + _imageStart[frame];

	int x = READ_LE_UINT16(videoData + 8);
	int y = READ_LE_UINT16(videoData + 10);
	int w = READ_LE_UINT16(videoData + 12);
	int h = READ_LE_UINT16(videoData + 14);

	rect = Common::Rect(x, y, x + w, y + h); 
}

int GfxRobot::getFrameScale(int frame) {
	byte *videoData = _resourceData + _imageStart[frame];
	return videoData[3];
}

void GfxRobot::playAudio() {
	if (_hasSound) {
		Audio::SoundHandle _audioHandle;
		Audio::AudioStream *audioStream = g_sci->_audio->getRobotAudioStream(_resourceData);
		g_system->getMixer()->playStream(Audio::Mixer::kSpeechSoundType, &_audioHandle, audioStream);
	}
}

	
#endif

} // End of namespace Sci