aboutsummaryrefslogtreecommitdiff
path: root/engines/neverhood/smackerplayer.cpp
blob: 799f04f09a8051ec72143902b4cc462ce2fb6c7f (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
/* 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 "graphics/palette.h"
#include "neverhood/smackerplayer.h"
#include "neverhood/palette.h"
#include "neverhood/resourceman.h"
#include "neverhood/scene.h"

namespace Neverhood {

// SmackerSurface

SmackerSurface::SmackerSurface(NeverhoodEngine *vm)
	: BaseSurface(vm, 0, 0, 0), _smackerFrame(NULL) {
}

void SmackerSurface::draw() {
	if (_smackerFrame && _visible && _drawRect.width > 0 && _drawRect.height > 0)
		_vm->_screen->drawSurface2(_smackerFrame, _drawRect, _clipRect, false);
}

void SmackerSurface::setSmackerFrame(const Graphics::Surface *smackerFrame) {
	_drawRect.x = 0;
	_drawRect.y = 0;
	_drawRect.width = smackerFrame->w;
	_drawRect.height = smackerFrame->h;
	// TODO: Check if _sysRect is needed at all in the reimplementation...
	_sysRect.x = 0;
	_sysRect.y = 0;
	_sysRect.width = (smackerFrame->w + 3) & 0xFFFC; // align by 4 bytes
	_sysRect.height = smackerFrame->h;
	_smackerFrame = smackerFrame;
}

// SmackerDoubleSurface

SmackerDoubleSurface::SmackerDoubleSurface(NeverhoodEngine *vm)
	: SmackerSurface(vm) {
}

void SmackerDoubleSurface::draw() {
	if (_smackerFrame && _visible && _drawRect.width > 0 && _drawRect.height > 0)
		_vm->_screen->drawDoubleSurface2(_smackerFrame, _drawRect);
}

// SmackerPlayer

SmackerPlayer::SmackerPlayer(NeverhoodEngine *vm, Scene *scene, uint32 fileHash, bool doubleSurface, bool flag)
	: Entity(vm, 0), _scene(scene), _doubleSurface(doubleSurface), _dirtyFlag(false), _videoDone(false),
	_palette(NULL), _smackerDecoder(NULL), _smackerSurface(NULL), _stream(NULL), _smackerFirst(true),
	_drawX(-1), _drawY(-1) {

	SetUpdateHandler(&SmackerPlayer::update);
	open(fileHash, flag);
}

SmackerPlayer::~SmackerPlayer() {
	close();
}

void SmackerPlayer::open(uint32 fileHash, bool keepLastFrame) {
	debug("SmackerPlayer::open(%08X)", fileHash);
	
	_fileHash = fileHash;
	_keepLastFrame = keepLastFrame;

	close();

	if (_doubleSurface) {
		_smackerSurface = new SmackerDoubleSurface(_vm);
	} else {
		_smackerSurface = new SmackerSurface(_vm);
	}

	_smackerFirst = true;

	_stream = _vm->_res->createStream(fileHash);

	// TODO: _keepLastFrame stuff

	_smackerDecoder = new Video::SmackerDecoder();
	_smackerDecoder->loadStream(_stream);
	
	_palette = new Palette(_vm);
	_palette->usePalette();
	
}

void SmackerPlayer::close() {
	delete _smackerDecoder;
	delete _palette;
	// NOTE: The SmackerDecoder deletes the _stream
	delete _smackerSurface;
	_smackerDecoder = NULL;
	_palette = NULL;
	_stream = NULL;
	_smackerSurface = NULL;
}

void SmackerPlayer::gotoFrame(uint frameNumber) {
	// TODO?
}

uint32 SmackerPlayer::getFrameCount() {
	return _smackerDecoder ? _smackerDecoder->getFrameCount() : 0;
}

uint32 SmackerPlayer::getFrameNumber() {
	return _smackerDecoder ? _smackerDecoder->getCurFrame() : 0;
}

uint SmackerPlayer::getStatus() {
	return 0;
}

void SmackerPlayer::setDrawPos(int16 x, int16 y) {
	_drawX = x;
	_drawY = y;
	if (_smackerSurface) {
		_smackerSurface->getDrawRect().x = _drawX;
		_smackerSurface->getDrawRect().y = _drawY;
	}
}

void SmackerPlayer::rewind() {

	// TODO Quite dirty, try to implement this in the decoder

	delete _smackerDecoder;
	_smackerDecoder = NULL;
	_stream = NULL;

	_smackerFirst = true;

	_stream = _vm->_res->createStream(_fileHash);

	_smackerDecoder = new Video::SmackerDecoder();
	_smackerDecoder->loadStream(_stream);
	
}

void SmackerPlayer::update() {
	debug(8, "SmackerPlayer::update()");

	if (!_smackerDecoder)
		return;

	if (_dirtyFlag) {
		// TODO _vm->_screen->resetDirtyRects();
		_dirtyFlag = false;
	}

	if (!_smackerDecoder->endOfVideo()) {
		updateFrame();
	} else if (!_keepLastFrame) {
		// Inform the scene about the end of the video playback
		if (_scene)
			sendMessage(_scene, 0x3002, 0);
		_videoDone = true;
	} else {
		rewind();
		updateFrame();
		_videoDone = false;
	}
	
}

void SmackerPlayer::updateFrame() {
	const Graphics::Surface *smackerFrame = _smackerDecoder->decodeNextFrame();

	if (_smackerFirst) {
		_smackerSurface->setSmackerFrame(smackerFrame);
		if (_drawX < 0 || _drawY < 0) {
			if (_doubleSurface) {
				_drawX = 320 - _smackerDecoder->getWidth();
				_drawY = 240 - _smackerDecoder->getHeight();
			} else {
				_drawX = (640 - _smackerDecoder->getWidth()) / 2;
				_drawY = (480 - _smackerDecoder->getHeight()) / 2;
			}
		}
		_smackerSurface->getDrawRect().x = _drawX;
		_smackerSurface->getDrawRect().y = _drawY;
		_smackerFirst = false;
	}
	
	if (_doubleSurface) {
		// TODO
	}

	// TODO _vm->_screen->_skipUpdate = true;
	_dirtyFlag = true;

	if (_smackerDecoder->hasDirtyPalette())
		updatePalette();
		
}

void SmackerPlayer::updatePalette() {
	byte tempPalette[1024];
	const byte *smackerPalette = _smackerDecoder->getPalette();
	for (int i = 0; i < 256; i++) {
		tempPalette[i * 4 + 0] = smackerPalette[i * 3 + 0];
		tempPalette[i * 4 + 1] = smackerPalette[i * 3 + 1];
		tempPalette[i * 4 + 2] = smackerPalette[i * 3 + 2];
	}
	_palette->copyPalette(tempPalette, 0, 256, 0);
}

} // End of namespace Neverhood