File: preagi.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 (356 lines) | stat: -rw-r--r-- 8,629 bytes parent folder | download
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* 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 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 "audio/mixer.h"
#include "audio/softsynth/pcspk.h"

#include "common/debug-channels.h"
#include "common/events.h"
#include "common/random.h"

#include "agi/preagi/preagi.h"
#include "agi/graphics.h"

namespace Agi {

PreAgiEngine::PreAgiEngine(OSystem *syst, const AGIGameDescription *gameDesc) : AgiBase(syst, gameDesc) {

	// Setup mixer
	syncSoundSettings();

	memset(&_debug, 0, sizeof(struct AgiDebug));

	_speakerHandle = new Audio::SoundHandle();
}

void PreAgiEngine::initialize() {
	initRenderMode();

	_font = new GfxFont(this);
	_gfx = new GfxMgr(this, _font);

	_font->init();

	//_game._vm->_text->charAttrib_Set(15, 0);

	_defaultColor = IDA_DEFAULT;

	//_game._vm->_text->configureScreen(0); // hardcoded

	_gfx->initVideo();

	_speakerStream = new Audio::PCSpeaker(_mixer->getOutputRate());
	_mixer->playStream(Audio::Mixer::kSFXSoundType, _speakerHandle,
	                   _speakerStream, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);

	debugC(2, kDebugLevelMain, "Detect game");

	// clear all resources and events
	for (int i = 0; i < MAX_DIRECTORY_ENTRIES; i++) {
		_game.pictures[i].reset();
		_game.sounds[i] = nullptr; // _game.sounds contains pointers now
		_game.dirPic[i].reset();
		_game.dirSound[i].reset();
	}
}

PreAgiEngine::~PreAgiEngine() {
	_mixer->stopHandle(*_speakerHandle);
	delete _speakerStream;
	delete _speakerHandle;

	delete _gfx;
	delete _font;
}

int PreAgiEngine::rnd(int max) {
	return (_rnd->getRandomNumber(max - 1) + 1);
}

// Screen functions
void PreAgiEngine::clearScreen(int attr, bool overrideDefault) {
	if (overrideDefault)
		_defaultColor = attr;

	_gfx->clearDisplay((attr & 0xF0) / 0x10);
}

void PreAgiEngine::clearGfxScreen(int attr) {
	_gfx->drawDisplayRect(0, 0, DISPLAY_DEFAULT_WIDTH - 1, IDI_MAX_ROW_PIC * 8 - 1, (attr & 0xF0) / 0x10);
}

byte PreAgiEngine::getWhite() const {
	switch (_renderMode) {
	case Common::kRenderCGA:
		return 3;
	case Common::kRenderHercA:
	case Common::kRenderHercG:
		return 1;
	default:
		return 15;
	}
}

// String functions

void PreAgiEngine::drawStr(int row, int col, int attr, const char *buffer) {
	if (attr == kColorDefault)
		attr = _defaultColor;

	byte foreground = attr & 0x0f;
	byte background = attr >> 4;

	// Simplistic CGA and Hercules mapping that handles all text
	// in Mickey and Winnie. Troll text is handled correctly in
	// graphics mode, but the original switched to CGA 16 color
	// text mode for some parts, and we are not doing that.
	switch (_renderMode) {
	case Common::kRenderCGA:
		// Map non-black text to white
		if (foreground != 0) {
			foreground = 3;
		}
		// Map white background to white
		if (background == 15) {
			background = 3;
		}
		break;
	case Common::kRenderHercA:
	case Common::kRenderHercG:
		// Map non-black text to amber/green
		if (foreground != 0) {
			foreground = 1;
		}
		// Map white background to amber/green,
		// all others to black
		if (background == 0x0f) {
			background = 1;
		} else {
			background = 0;
		}
		break;
	default:
		break;
	}

	const int stringLength = (int)strlen(buffer);
	for (int iChar = 0; iChar < stringLength; iChar++) {
		int code = buffer[iChar];

		switch (code) {
		case '\n':
		case '\r': // winnie
		case 0x8D:
			if (++row == 200 / 8) return;
			col = 0;
			break;

		case '|':
			// swap attribute nibbles
			break;

		default:
			_gfx->drawCharacter(row, col, code, foreground, background, false);

			if (++col == 320 / 8) {
				col = 0;
				if (++row == 200 / 8) return;
			}
		}
	}
}

void PreAgiEngine::clearTextArea() {
	int start = IDI_MAX_ROW_PIC;

	if (getGameID() == GID_TROLL)
		start = 21;

	for (int row = start; row < 200 / 8; row++) {
		clearRow(row);
	}
}

void PreAgiEngine::clearRow(int row) {
	drawStr(row, 0, IDA_DEFAULT, "                                        ");   // 40 spaces
}

void PreAgiEngine::printStr(const char *szMsg) {
	clearTextArea();
	drawStr(21, 0, IDA_DEFAULT, szMsg);
	_system->updateScreen();
}

void PreAgiEngine::XOR80(char *buffer) {
	for (size_t i = 0; i < strlen(buffer); i++)
		if (buffer[i] & 0x80)
			buffer[i] ^= 0x80;
}

void PreAgiEngine::printStrXOR(char *szMsg) {
	XOR80(szMsg);
	printStr(szMsg);
}

// Input functions

int PreAgiEngine::getSelection(SelectionTypes type) {
	Common::Event event;

	while (!shouldQuit()) {
		while (_eventMan->pollEvent(event)) {
			switch (event.type) {
			case Common::EVENT_RETURN_TO_LAUNCHER:
			case Common::EVENT_QUIT:
				return 0;
			case Common::EVENT_RBUTTONUP:
				return 0;
			case Common::EVENT_LBUTTONUP:
				if (type == kSelYesNo || type == kSelAnyKey || type == kSelBackspace)
					return 1;
				break;
			case Common::EVENT_KEYDOWN:
				if (event.kbd.flags & Common::KBD_NON_STICKY) {
					break;
				}
				switch (event.kbd.keycode) {
				case Common::KEYCODE_y:
					if (type == kSelYesNo)
						return 1;
					break;
				case Common::KEYCODE_n:
					if (type == kSelYesNo)
						return 0;
					break;
				case Common::KEYCODE_ESCAPE:
					if (type == kSelNumber || type == kSelAnyKey)
						return 0;
					break;
				case Common::KEYCODE_1:
				case Common::KEYCODE_2:
				case Common::KEYCODE_3:
				case Common::KEYCODE_4:
				case Common::KEYCODE_5:
				case Common::KEYCODE_6:
				case Common::KEYCODE_7:
				case Common::KEYCODE_8:
				case Common::KEYCODE_9:
					if (type == kSelNumber)
						return event.kbd.keycode - Common::KEYCODE_1 + 1;
					break;
				case Common::KEYCODE_SPACE:
					if (type == kSelSpace)
						return 1;
					break;
				case Common::KEYCODE_BACKSPACE:
					if (type == kSelBackspace)
						return 0;
					break;
				default:
					break;
				}
				if (type == kSelYesNo) {
					return 2;
				} else if (type == kSelNumber) {
					return 10;
				} else if (type == kSelAnyKey || type == kSelBackspace) {
					return 1;
				}
				break;
			default:
				break;
			}
		}
		_system->updateScreen();
		_system->delayMillis(10);
	}
	return 0;
}

bool PreAgiEngine::playSpeakerNote(int16 frequency, int32 length, WaitOptions options) {
	// play note, unless this is a pause
	if (frequency != 0) {
		_speakerStream->play(Audio::PCSpeaker::kWaveFormSquare, frequency, length);
	}

	// wait for note length
	bool completed = wait(length, options);

	// stop note if the wait was interrupted
	if (!completed) {
		if (frequency != 0) {
			_speakerStream->stop();
		}
	}

	return completed;
}

// A wait function that updates the screen, optionally allows events to be
// processed, and optionally allows keyboard and mouse events to interrupt
// the wait. Processing events keeps the program window responsive, but for
// very short delays it may be better to not process events so that they
// are buffered and not lost.
bool PreAgiEngine::wait(uint32 delay, WaitOptions options) {
	Common::Event event;
	uint32 startTime = _system->getMillis();

	bool processEvents = (options & kWaitProcessEvents) != 0;
	bool allowInterrupt = (options == kWaitAllowInterrupt);

	while (!shouldQuit()) {
		// process events
		if (processEvents) {
			while (_eventMan->pollEvent(event)) {
				switch (event.type) {
				case Common::EVENT_KEYDOWN:
					// don't interrupt if a modifier is pressed
					if (event.kbd.flags & Common::KBD_NON_STICKY) {
						continue;
					}
					// fall through
				case Common::EVENT_LBUTTONUP:
				case Common::EVENT_RBUTTONUP:
					if (!allowInterrupt) {
						continue;
					}
					// fall through
				case Common::EVENT_RETURN_TO_LAUNCHER:
				case Common::EVENT_QUIT:
					return false; // interrupted by quit or input
				default:
					break;
				}
			}
		}

		if (_system->getMillis() - startTime >= delay) {
			return true; // delay completed
		}

		_system->updateScreen();
		_system->delayMillis(10);
	}

	return false; // interrupted by quit
}

} // End of namespace Agi