aboutsummaryrefslogtreecommitdiff
path: root/scumm/smush/scumm_renderer.cpp
blob: 3e357091c2bba5d0700afb7022fea0d387b7bbcd (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
/* 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"

class scumm_mixer : 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:
	scumm_mixer(SoundMixer *);
	virtual ~scumm_mixer();
	bool init();
	_Channel * findChannel(int track);
	bool addChannel(_Channel * c);
	bool handleFrame();
	bool stop();
	bool update();
};

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

scumm_mixer::~scumm_mixer() {
}

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

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

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

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

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

	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 = 0; 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 = 0; i < SoundMixer::NUM_CHANNELS; i++) {
		fprintf(stderr, "channel %d : %p(%d, %d) %d %d\n", i, _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 scumm_mixer::handleFrame() {
	debug(9, "scumm_mixer::handleFrame()");
	for(int i = 0; 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 {
				int rate;
				bool stereo, is_short;

				_channels[i].chan->getParameters(rate, stereo, is_short);
				int 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);
				int 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 short[size * (stereo ? 2 : 1)];
					_channels[i].chan->getSoundData(data, size);
					size *= stereo ? 4 : 2;

					// 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 {
					char * data = new char[size*(stereo ? 2 : 1)];
					_channels[i].chan->getSoundData(data, size);
					size *= stereo ? 2 : 1;

					// 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 scumm_mixer::stop() {
	debug(9, "scumm_mixer::stop()");
	for(int i = 0; i < SoundMixer::NUM_CHANNELS; i++) {
		if(_channels[i].id != -1) {
				delete _channels[i].chan;
				_channels[i].id = -1;
				_channels[i].chan = 0;
		}
	}
	//~ _mixer->stopAll();
	return true;
}

ScummRenderer::ScummRenderer(Scumm * scumm) : _scumm(scumm), _smixer(0) {
}

static ScummRenderer * s_renderer;

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

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

ScummRenderer::~ScummRenderer() {
	_scumm->_insaneState = 0;
	_scumm->exitCutscene();
	if(_smixer) {
		_scumm->_timer->releaseProcedure(&smush_handler);
		delete _smixer;
		_smixer = 0;
	}
	_scumm->_sound->pauseBundleMusic(false);
}

bool ScummRenderer::wait(int ms) {
	while(_wait) {
		_scumm->waitForTimer(1);
	}
	return true; 
}

bool ScummRenderer::startDecode(const char * fname, int version, int nbframes) {
	_scumm->_sound->pauseBundleMusic(true);
	_scumm->videoFinished = 0;
	_scumm->_insaneState = 1;
	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(int frame) {
	int width = MIN(getWidth(), _scumm->_realWidth); 
	int height = MIN(getHeight(), _scumm->_realHeight);
	
	_scumm->_system->copy_rect((const byte *)data(), getWidth(), 0, 0, width, height);
	_scumm->_system->update_screen();
	_scumm->processKbd();
	_wait = true;
}

bool ScummRenderer::prematureClose() { 
	return _scumm->videoFinished; 
}

bool ScummRenderer::update() {
	_wait = false;
	return true;
}