File: movie_pc.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (275 lines) | stat: -rw-r--r-- 6,597 bytes parent folder | download | duplicates (2)
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
/* 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.
 *
 * Additional copyright for this file:
 * Copyright (C) 1999-2000 Revolution Software Ltd.
 * This code is based on source code created by Revolution Software,
 * used with permission.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/icb/movie_pc.h"
#include "engines/icb/direct_input.h"
#include "engines/icb/sound/music_manager.h"
#include "engines/icb/cluster_manager_pc.h"
#include "engines/icb/res_man_pc.h"
#include "engines/icb/global_objects.h"

#include "common/system.h"
#include "common/keyboard.h"

namespace ICB {

// Instance our global bink handler for movies
MovieManager *g_theSequenceManager;

MovieManager::MovieManager() {
	_binkDecoder = nullptr;
	_x = 0;
	_y = 0;
	_fadeCounter = 255;
	_fadeRate = 4;
	_haveFaded = false;
	_rater = true;
	_haveClearedScreen = false;
	_loop = FALSE8;
}

MovieManager::~MovieManager() {
	kill();
}

bool MovieManager::registerMovie(const char *fileName, bool8 fade, bool8 loop) {
	// Release any currently held sequence
	kill();
	_x = 0;
	_y = 0;

	if (g_theMusicManager)
		g_theMusicManager->StopMusic();

	_binkDecoder = new Video::BinkDecoder();

	Common::SeekableReadStream *stream = openDiskFileForBinaryStreamRead(fileName);
	if (!stream) {
		return false;
	}
	if (!_binkDecoder->loadStream(stream)) {
		return false;
	}

	_binkDecoder->setOutputPixelFormat(Graphics::PixelFormat(4, 8, 8, 8, 0, 16, 8, 0, 24));

	if (_binkDecoder->getWidth() != SCREEN_WIDTH) {
		_x = (SCREEN_WIDTH / 2) - (_binkDecoder->getWidth() / 2);
	}
	if (_binkDecoder->getHeight() != SCREEN_DEPTH) {
		_y = (SCREEN_DEPTH / 2) - (_binkDecoder->getHeight() / 2);
	}

	// Should we fade the screen out before playing the movie
	if (fade) {
		_fadeCounter = 1; // Yes we should
	} else {
		_fadeCounter = 255; // No thanks
	}

	_haveFaded = false;
	_loop = loop;

	return true;
}

uint32 MovieManager::getMovieHeight() {
	if (!busy())
		return 0;
	return _binkDecoder->getHeight();
}

uint32 MovieManager::getMovieWidth() {
	if (!busy())
		return 0;
	return _binkDecoder->getWidth();
}

uint32 MovieManager::getMovieFrames() {
	if (!busy())
		return 0;
	return _binkDecoder->getFrameCount();
}

bool MovieManager::busy() {
	return _binkDecoder != nullptr;
}

int32 MovieManager::getFrameNumber() {
	if (busy())
		return _binkDecoder->getCurFrame() + 1;

	return 0;
}

uint32 MovieManager::drawFrame(uint32 surface_id) {
	if (!_binkDecoder) {
		return FINISHED;
	}

	// Non-looping movies can be quit using the escape key
	if (Read_DI_once_keys(Common::KEYCODE_ESCAPE) && !_loop) {
		kill();
		return FINISHED;
	}

	// Don't do bink, fade the screen
	if (_fadeCounter < (255 / _fadeRate)) {
		fadeScreen(surface_id);
		_haveFaded = true;
		return FADING;
	}

	// Have we performed a fade
	if (_haveFaded) {
		// Reassign correct surface id
		surface_id = working_buffer_id;
		_haveFaded = false;
		if (getFrameNumber() == 0)
			_binkDecoder->start();
	} else {
		// Black out the screen before we play the movie
		if ((getFrameNumber() == 0) && !_haveClearedScreen) {
			surface_manager->Clear_surface(working_buffer_id);
			_haveClearedScreen = true;
			_binkDecoder->start();
		}
	}

	// Decompress a frame
	const Graphics::Surface *surface = _binkDecoder->decodeNextFrame();
	if (!surface || _binkDecoder->endOfVideo()) {
		if (_loop) {
			_binkDecoder->rewind();
			surface = _binkDecoder->decodeNextFrame();
		} else {
			kill();
			surface_manager->Clear_surface(working_buffer_id);

			return FINISHED;
		}
	}

	if (_rater) {
		uint32 waitTime = _binkDecoder->getTimeToNextFrame();
		g_system->delayMillis(waitTime);
	}

	// For access to buffer
	int32 pitch;
	uint8 *surface_address;

	// Lock the surface
	surface_address = surface_manager->Lock_surface(surface_id);
	pitch = surface_manager->Get_pitch(surface_id);
	uint32 height = surface_manager->Get_height(surface_id);

	for (int32 i = 0; i < surface->h; i++) {
		if (i + _y >= (int32)height) {
			break;
		}
		memcpy(surface_address + (i + _y) * pitch, surface->getBasePtr(0, i), MIN(surface->pitch, pitch));
	}

	// Unlock the buffer
	surface_manager->Unlock_surface(surface_id);
	return JUSTFINE;
}

void MovieManager::setRate() {
	// Toggle
	if (_rater)
		_rater = false;
	else
		_rater = true;

	if (_binkDecoder) {
		// Mute the sound if necessary
		if (_rater)
			setVolume(GetMusicVolume());
		else
			setVolume(0);
	}
}

void MovieManager::fadeScreen(uint32 surface_id) {
	// Pointer to subtractive table block
	uint8 subtractive[8];

	// Fade by table
	subtractive[4] = subtractive[0] = (uint8)_fadeRate;
	subtractive[5] = subtractive[1] = (uint8)_fadeRate;
	subtractive[6] = subtractive[2] = (uint8)_fadeRate;
	subtractive[7] = subtractive[3] = 0;

	// Lock the directdraw surface
	uint8 *surface_address = surface_manager->Lock_surface(surface_id);
	uint32 pitch = surface_manager->Get_pitch(surface_id);

	for (uint32 lines = 0; lines < SCREEN_DEPTH; lines++) {
		for (int32 xPos = 0; xPos < SCREEN_WIDTH; xPos++) {
			// 32-bit BGRA pixel
			uint8 *pixel = &surface_address[xPos * 4];
			// Subtract from RGB components
			for (int32 i = 0; i < 3; i++) {
				pixel[i] = MAX(0, pixel[i] - subtractive[i]);
			}
		}
		// Next line
		surface_address += pitch;
	}

	// Unlock the buffer
	surface_manager->Unlock_surface(surface_id);

	// Increment fade level (pixel shift right)
	_fadeCounter++;
}

void MovieManager::setVolume(int32 vol) {
	if (_binkDecoder) {
		float volumeConversion = Audio::Mixer::kMaxChannelVolume / 128.0f;
		_binkDecoder->setVolume(volumeConversion * vol);
	}
}

void MovieManager::kill() {
	if (_binkDecoder) {
		_binkDecoder->close();
		delete _binkDecoder;
		_binkDecoder = nullptr;
	}

	// Reset blitting coordinates
	_x = 0;
	_y = 0;

	_fadeCounter = 255;

	_haveClearedScreen = false;
}

} // End of namespace ICB