aboutsummaryrefslogtreecommitdiff
path: root/scumm/smush/scumm_renderer.cpp
blob: e26ec6d45de33ae5a1092646c28ad6e46c646f55 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2001/2002 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include <stdafx.h>
#include "common/util.h"
#include "scumm_renderer.h"
#include "channel.h"
#include "mixer.h"
#include "sound/mixer.h"
#include "scumm/scumm.h"
#include "scumm/sound.h"
#include "scumm/imuse.h"
#include "scumm/actor.h"

class ScummMixer : public Mixer {
private:
	SoundMixer *_mixer; //!< pointer to the SoundMixer instance
	struct {
		int id;
		_Channel *chan;
		bool first;
		int mixer_index;
	} _channels[SoundMixer::NUM_CHANNELS];		//!< The map of track and channels
	int _nextIndex;
public:
	ScummMixer(SoundMixer *);
	virtual ~ScummMixer();
	bool init();
	_Channel *findChannel(int32 track);
	bool addChannel(_Channel *c);
	bool handleFrame();
	bool stop();
	bool update();
	bool _silentMixer;
};

ScummMixer::ScummMixer(SoundMixer *m) : _mixer(m), _nextIndex(_mixer->_beginSlots) {
	for(int32 i = _mixer->_beginSlots; i < SoundMixer::NUM_CHANNELS; i++) {
		_channels[i].id = -1;
		_channels[i].chan = 0;
		_channels[i].first = true;
	}
}

ScummMixer::~ScummMixer() {
}

bool ScummMixer::init() {
	debug(9, "ScummMixer::init()");
	return true;
}

_Channel *ScummMixer::findChannel(int32 track) {
	debug(9, "scumm_mixer::findChannel(%d)", track);
	for(int32 i = _mixer->_beginSlots; i < SoundMixer::NUM_CHANNELS; i++) {
		if(_channels[i].id == track)
			return _channels[i].chan;
	}
	return 0;
}

bool ScummMixer::addChannel(_Channel *c) {
	int32 track = c->getTrackIdentifier();
	int i;

	debug(9, "ScummMixer::addChannel(%d)", track);

	for(i = _mixer->_beginSlots; i < SoundMixer::NUM_CHANNELS; i++) {
		if(_channels[i].id == track)
			warning("mixer::addChannel(%d) : channel already exist !", track);
	}
	if(_nextIndex >= SoundMixer::NUM_CHANNELS) _nextIndex = _mixer->_beginSlots;

	for(i = _nextIndex; i < SoundMixer::NUM_CHANNELS; i++) {
		if(_channels[i].chan == 0 || _channels[i].id == -1) {
			_channels[i].chan = c;
			_channels[i].id = track;
			_channels[i].first = true;
			_nextIndex = i + 1;
			return true;
		}
	}

	for(i = _mixer->_beginSlots; i < _nextIndex; i++) {
		if(_channels[i].chan == 0 || _channels[i].id == -1)	{
			_channels[i].chan = c;
			_channels[i].id = track;
			_channels[i].first = true;
			_nextIndex = i + 1;
			return true;
		}
	}

	fprintf(stderr, "_nextIndex == %d\n", _nextIndex);

	for(i = _mixer->_beginSlots; i < SoundMixer::NUM_CHANNELS; i++) {
		fprintf(stderr, "channel %d : %p(%d, %d) %d %d\n", i, (void *)_channels[i].chan, 
			_channels[i].chan ? _channels[i].chan->getTrackIdentifier() : -1, 
			_channels[i].chan ? _channels[i].chan->isTerminated() : 1, 
			_channels[i].first, _channels[i].mixer_index);
	}

	error("mixer::add_channel() : no more channel available");
	return false;
}

bool ScummMixer::handleFrame() {
	debug(9, "ScummMixer::handleFrame()");
	for(int i = _mixer->_beginSlots; i < SoundMixer::NUM_CHANNELS; i++) {
		if(_channels[i].id != -1) {
			debug(9, "updating channel %d (%p)", _channels[i].id, _channels[i].chan);
			if(_channels[i].chan->isTerminated()) {
				debug(9, "channel %d has terminated (%p)", _channels[i].id, _channels[i].chan);
				delete _channels[i].chan;
				_channels[i].id = -1;
				_channels[i].chan = 0;
			} else {
				int32 rate;
				bool stereo, is_short;

				_channels[i].chan->getParameters(rate, stereo, is_short);
				int32 size = _channels[i].chan->availableSoundData();
				debug(9, "channel %d : %d, %s, %d bits, %d", _channels[i].id, rate, stereo ? "stereo" : "mono", is_short ? 16 : 8, size);
				int32 flags = stereo ? SoundMixer::FLAG_STEREO : 0;

				if(is_short) {
					// FIXME this is one more data copy... we could get rid of it...
					short *data = new int16[size * (stereo ? 2 : 1) * 2];
					_channels[i].chan->getSoundData(data, size);
					if(_channels[i].chan->getRate() == 11025) size *= 2;
					size *= stereo ? 4 : 2;

					if(_silentMixer == false) {
						// append to _sound
						if(_channels[i].first) {
							_channels[i].mixer_index = _mixer->playStream(NULL, -1, data, size, rate, flags | SoundMixer::FLAG_16BITS);
							debug(5, "channel %d bound to mixer_index %d", _channels[i].id, _channels[i].mixer_index);
							_channels[i].first = false;
						} else {
							_mixer->append(_channels[i].mixer_index, data, size, rate, flags | SoundMixer::FLAG_16BITS);
						}
					}

					delete []data;
				} else {
					int8 *data = new int8[size * (stereo ? 2 : 1) * 2];
					_channels[i].chan->getSoundData(data, size);
					if(_channels[i].chan->getRate() == 11025) size *= 2;
					size *= stereo ? 2 : 1;

					if(_silentMixer == false) {
						// append to _sound
						if(_channels[i].first) {
							_channels[i].mixer_index = _mixer->playStream(NULL, -1, data, size, rate, flags | SoundMixer::FLAG_UNSIGNED);
							_channels[i].first = false;
						} else {
							_mixer->append(_channels[i].mixer_index, data, size, rate, flags | SoundMixer::FLAG_UNSIGNED);
						}
					}

					delete []data;
				}
			}
		}
	}
	return true;
}

bool ScummMixer::stop() {
	debug(9, "ScummMixer::stop()");
	for(int i = _mixer->_beginSlots; i < SoundMixer::NUM_CHANNELS; i++) {
		if(_channels[i].id != -1) {
				delete _channels[i].chan;
				_channels[i].id = -1;
				_channels[i].chan = 0;
		}
	}
	return true;
}

ScummRenderer::ScummRenderer(Scumm *scumm, uint32 speed) :
	_scumm(scumm),
	_smixer(0),
	_insaneSpeed(speed),
	_pending_updates(0) {
}

static ScummRenderer *s_renderer;

static void smush_handler(void *engine) {
	s_renderer->update();
}

Mixer *ScummRenderer::getMixer() {
	if(_smixer == 0) {
		_smixer = new ScummMixer(_scumm->_mixer);
		if(!_smixer) error("unable to allocate a smush mixer");
		_smixer->_silentMixer = _scumm->_silentDigitalImuse;
		s_renderer = this;
		_scumm->_timer->installProcedure(&smush_handler, _insaneSpeed);
	}
	return _smixer;
}

ScummRenderer::~ScummRenderer() {
	_scumm->_insaneState = false;
	_scumm->exitCutscene();
	if(_smixer) {
		_scumm->_timer->releaseProcedure(&smush_handler);
		delete _smixer;
		_smixer = 0;
	}
	if (_scumm->_imuseDigital) {
		_scumm->_imuseDigital->pause(false);
	}
	_scumm->_sound->pauseBundleMusic(false);
#if 0
	// FIXME - enabling this breaks the COMI demo. OTOH I am not aware
	// what disabling this breaks... if anybody knows of any regressions
	// turning this off causes, please tell me.
	_scumm->_fullRedraw = 1;
	_scumm->redrawBGAreas();
	for (int32 i = 0; i < _scumm->NUM_ACTORS; i++)
		_scumm->derefActor(i)->needRedraw = true;
	_scumm->processActors();
#endif
}

bool ScummRenderer::wait(int32 ms) {
	// Because waitForTimer() also is the function that checks for user
	// input we always want to call it at least once between frames, or
	// the user may become unable to interrupt the movie.
	do {
 		_scumm->waitForTimer(1);
	} while(_pending_updates <= 0);
	return true; 
}

bool ScummRenderer::startDecode(const char *fname, int32 version, int32 nbframes) {
	if (_scumm->_imuseDigital) {
		_scumm->_imuseDigital->pause(true);
	}
	_scumm->_sound->pauseBundleMusic(true);
	_scumm->_videoFinished = false;
	_scumm->_insaneState = true;
	return true;
}

bool ScummRenderer::setPalette(const Palette &pal) {
	int i;
	byte palette_colors[1024];
	byte *p = palette_colors;

	for (i = 0; i < 256; i++, p += 4) {
		p[0] = pal[i].red();
		p[1] = pal[i].green();
		p[2] = pal[i].blue();
		p[3] = 0;
	}

	_scumm->_system->set_palette(palette_colors, 0, 256);
	_scumm->setDirtyColors(0, 255);
	return BaseRenderer::setPalette(pal); // For compatibility with possible subclass...
}

void ScummRenderer::save(int32 frame) {
	int width = MIN(getWidth(), _scumm->_realWidth); 
	int height = MIN(getHeight(), _scumm->_realHeight);

	// In theory, this will always be true. In reality, there may be
	// several pending updates because the computer wasn't fast enough to
	// process them all. In that case, skip the frame to catch up.
	if (--_pending_updates <= 0) {
		_scumm->_system->copy_rect((const byte *)data(), getWidth(), 0, 0, width, height);
		_scumm->_system->update_screen();
	} else {
		warning("ScummRenderer: Skipping frame %d to catch up", getFrame());
	}
	_scumm->processKbd();
}

bool ScummRenderer::prematureClose() { 
	return _scumm->_videoFinished || _scumm->_saveLoadFlag;
}

bool ScummRenderer::update() {
	_pending_updates++;
	return true;
}