aboutsummaryrefslogtreecommitdiff
path: root/engines/prince/prince.cpp
blob: fdd9219b13d4da607d460d42747a2cfeecf81cf0 (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* 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 "common/scummsys.h"

#include "common/config-manager.h"
#include "common/debug-channels.h"
#include "common/debug.h"
#include "common/events.h"
#include "common/file.h"
#include "common/random.h"
#include "common/fs.h"
#include "common/keyboard.h"
#include "common/substream.h"

#include "graphics/cursorman.h"
#include "graphics/surface.h"
#include "graphics/palette.h"
#include "graphics/pixelformat.h"

#include "engines/util.h"
#include "engines/advancedDetector.h"

#include "audio/audiostream.h"

#include "prince/prince.h"
#include "prince/font.h"
#include "prince/graphics.h"
#include "prince/script.h"
#include "prince/debugger.h"
#include "prince/object.h"
#include "prince/mob.h"
#include "prince/sound.h"
#include "prince/variatxt.h"
#include "prince/flags.h"
#include "prince/font.h"
#include "prince/mhwanh.h"
#include "prince/cursor.h"
#include "prince/archive.h"

namespace Prince {

void PrinceEngine::debugEngine(const char *s, ...) {
	char buf[STRINGBUFLEN];
	va_list va;

    va_start(va, s);
    vsnprintf(buf, STRINGBUFLEN, s, va);
    va_end(va);

	debug("Prince::Engine frame %08ld %s", _frameNr, buf);
}

PrinceEngine::PrinceEngine(OSystem *syst, const PrinceGameDescription *gameDesc) : 
	Engine(syst), _gameDescription(gameDesc), _graph(NULL), _script(NULL),
	_locationNr(0), _debugger(NULL), _midiPlayer(NULL),
	_cameraX(0), _newCameraX(0), _frameNr(0), _cursor1(NULL), _cursor2(NULL), _font(NULL),
	_walizkaBmp(NULL), _roomBmp(NULL), _voiceStream(NULL) {

	// Debug/console setup
	DebugMan.addDebugChannel(DebugChannel::kScript, "script", "Prince Script debug channel");
	DebugMan.addDebugChannel(DebugChannel::kEngine, "engine", "Prince Engine debug channel");

	DebugMan.enableDebugChannel("script");

	gDebugLevel = 10;
}

PrinceEngine::~PrinceEngine() {
	DebugMan.clearAllDebugChannels();

	delete _rnd;
	delete _debugger;
	delete _cursor1;
	delete _cursor2;
	delete _midiPlayer;
	delete _script;
	delete _font;
	delete _roomBmp;
	delete _walizkaBmp;
	delete _variaTxt;
	delete[] _talkTxt;
	delete _graph;
}

GUI::Debugger *PrinceEngine::getDebugger() {
	return _debugger;
}

template <typename T>
bool loadFromStream(T &resource, Common::SeekableReadStream &stream) {
	return resource.loadFromStream(stream);
}

template <>
bool loadFromStream<MhwanhDecoder>(MhwanhDecoder &image, Common::SeekableReadStream &stream) {
	return image.loadStream(stream);
}

template <>
bool loadFromStream<Graphics::BitmapDecoder>(Graphics::BitmapDecoder &image, Common::SeekableReadStream &stream) {
	return image.loadStream(stream);
}

template<typename T>
bool loadResource(T *resource, const char *resourceName, bool required = true) {
	Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(resourceName);
	if (!stream) {
		if (required) 
			error("Can't load %s", resourceName);
		return false;
	}

	bool ret = loadFromStream(*resource, *stream);

	delete stream;

	return ret;
} 

template <typename T>
bool loadResource(Common::Array<T> &array, const char *resourceName, bool required = true) {
	Common::SeekableReadStream *stream = SearchMan.createReadStreamForMember(resourceName);
	if (!stream) {
		if (required) {
			error("Can't load %s", resourceName);
		}
		return false;
	}

	typename Common::Array<T>::value_type t;
	while (t.loadFromStream(*stream))
		array.push_back(t);

	delete stream;
	return true;
}

void PrinceEngine::init() {

	const Common::FSNode gameDataDir(ConfMan.get("path"));
	
	debugEngine("Adding all path: %s", gameDataDir.getPath().c_str());

	PtcArchive *all = new PtcArchive();
	if (!all->open("all/databank.ptc")) 
		error("Can't open all/databank.ptc");

	PtcArchive *voices = new PtcArchive();
	if (!voices->open("data/voices/databank.ptc"))
		error("Can't open data/voices/databank.ptc");

	SearchMan.add("all", all);
	SearchMan.add("data/voices", voices);

	_graph = new GraphicsMan(this);

	_rnd = new Common::RandomSource("prince");
	_debugger = new Debugger(this);

	SearchMan.addSubDirectoryMatching(gameDataDir, "all", 0, 2);
	SearchMan.addSubDirectoryMatching(gameDataDir, "data/voices", 0, 2);

	_midiPlayer = new MusicPlayer(this);
	 
	_font = new Font();
	loadResource(_font, "all/font1.raw");
	_walizkaBmp = new MhwanhDecoder();
	loadResource(_walizkaBmp, "all/walizka");

	_script = new Script(this);
	loadResource(_script, "all/skrypt.dat");

	_variaTxt = new VariaTxt();
	loadResource(_variaTxt, "all/variatxt.dat");
	
	_cursor1 = new Cursor();
	loadResource(_cursor1, "all/mouse1.cur");

	_cursor2 = new Cursor();
	loadResource(_cursor2, "all/mouse2.cur");

	Common::SeekableReadStream *talkTxtStream = SearchMan.createReadStreamForMember("all/talktxt.dat");
	if (!talkTxtStream) {
		error("Can't load talkTxtStream");
		return;
	}
	_talkTxtSize = talkTxtStream->size();
	_talkTxt = new byte[_talkTxtSize];
	talkTxtStream->read(_talkTxt, _talkTxtSize);

	delete talkTxtStream;
}

void PrinceEngine::showLogo() {
	MhwanhDecoder logo;
	if (loadResource(&logo, "logo.raw")) {
		_graph->setPalette(logo.getPalette());
		_graph->draw(0, 0, logo.getSurface());
		_graph->update();
		_system->delayMillis(700);
	}
}

Common::Error PrinceEngine::run() {

	init();

	showLogo();

//	return Common::kNoError;

	mainLoop();

	return Common::kNoError;
}

bool PrinceEngine::loadLocation(uint16 locationNr) {
	_cameraX = 0;
	_newCameraX = 0;
	_debugger->_locationNr = locationNr;
	debugEngine("PrinceEngine::loadLocation %d", locationNr);
	const Common::FSNode gameDataDir(ConfMan.get("path"));
	SearchMan.remove(Common::String::format("%02d", _locationNr));
	_locationNr = locationNr;

	const Common::String locationNrStr = Common::String::format("%02d", _locationNr);
	debugEngine("loadLocation %s", locationNrStr.c_str());

	PtcArchive *locationArchive = new PtcArchive();
	if (!locationArchive->open(locationNrStr + "/databank.ptc"))
		error("Can't open location %s", locationNrStr.c_str());

	SearchMan.add(locationNrStr, locationArchive);

	delete _roomBmp;
	// load location background
	_roomBmp = new Graphics::BitmapDecoder();
	loadResource(_roomBmp, "room");
	if (_roomBmp->getSurface()) {
		_sceneWidth = _roomBmp->getSurface()->w;
	}

	_mobList.clear();
	loadResource(_mobList, "mob.lst", false);

	const char *musName = MusicPlayer::_musTable[MusicPlayer::_musRoomTable[locationNr]];
	_midiPlayer->loadMidi(musName);

	return true;
}

void PrinceEngine::changeCursor(uint16 curId) {
	_debugger->_cursorNr = curId;

	Graphics::Surface *curSurface = NULL;

	uint16 hotspotX = 0;
	uint16 hotspotY = 0;

	switch(curId) {
		case 0:
			CursorMan.showMouse(false);
			return;
		case 1:
			curSurface = _cursor1->getSurface();
			break;
		case 2:
			curSurface = _cursor2->getSurface();
			hotspotX = curSurface->w >> 1;
			hotspotY = curSurface->h >> 1;
			break;
	}

	CursorMan.replaceCursorPalette(_roomBmp->getPalette(), 0, 255);
	CursorMan.replaceCursor(
		curSurface->getBasePtr(0, 0), 
		curSurface->w, curSurface->h, 
		hotspotX, hotspotY, 
		255, false,
		&curSurface->format
	);
	CursorMan.showMouse(true);
}

bool PrinceEngine::playNextFrame() {
	if (!_flicPlayer.isVideoLoaded())
		return false;

	const Graphics::Surface *s = _flicPlayer.decodeNextFrame();
	if (s) {
		_graph->drawTransparent(s);
		_graph->change();
	} else if (_flicLooped) {
        _flicPlayer.rewind();
        playNextFrame();
    }

	return true;
}

void PrinceEngine::playSample(uint16 sampleId, uint16 loopType) {
	if (_voiceStream) {

		Audio::RewindableAudioStream *audioStream = Audio::makeWAVStream(_voiceStream, DisposeAfterUse::YES);
		_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, audioStream, sampleId);
	}
}

void PrinceEngine::stopSample(uint16 sampleId) {
	_mixer->stopID(sampleId);
	_voiceStream = NULL;
}

bool PrinceEngine::loadVoice(uint32 slot, const Common::String &streamName) {
	debugEngine("Loading wav %s slot %d", streamName.c_str(), slot);

	if (slot > MAXTEXTS) {
		error("Text slot bigger than MAXTEXTS %d", MAXTEXTS);
		return false;
	}

	_voiceStream = SearchMan.createReadStreamForMember(streamName);
	if (!_voiceStream) {
		error("Can't open %s", streamName.c_str());
		return false;
	}

	uint32 id = _voiceStream->readUint32LE();
	if (id != 0x46464952) {
		error("It's not RIFF file %s", streamName.c_str());
		return false;
	}

	_voiceStream->skip(0x20);
	id = _voiceStream->readUint32LE();
	if (id != 0x61746164) {
		error("No data section in %s id %04x", streamName.c_str(), id);
		return false;
	}

	id = _voiceStream->readUint32LE();
	debugEngine("SetVoice slot %d time %04x", slot, id); 
	id <<= 3;
	id /= 22050;
	id += 2;

	_textSlots[slot]._time = id;

	debugEngine("SetVoice slot %d time %04x", slot, id); 
	_voiceStream->seek(0);

	return true;
}

bool PrinceEngine::loadAnim(uint16 animNr, bool loop) {
	Common::String streamName = Common::String::format("AN%02d", animNr);
	Common::SeekableReadStream * flicStream = SearchMan.createReadStreamForMember(streamName);

	if (!flicStream) {
		error("Can't open %s", streamName.c_str());
		return false;
	}

	if (!_flicPlayer.loadStream(flicStream)) {
		error("Can't load flic stream %s", streamName.c_str());
	}

	debugEngine("%s loaded", streamName.c_str());
    _flicLooped = loop;
	_flicPlayer.start();
	playNextFrame();
	return true;
}

void PrinceEngine::scrollCameraLeft(int16 delta) {
    if (_newCameraX > 0) {
        if (_newCameraX < delta)
            _newCameraX = 0;
        else
            _newCameraX -= delta;
    }   
}

void PrinceEngine::scrollCameraRight(int16 delta) {
    if (_newCameraX != _sceneWidth - 640) {
        if (_sceneWidth - 640 < delta + _newCameraX)
            delta += (_sceneWidth - 640) - (delta + _newCameraX);
        _newCameraX += delta;
        debugEngine("PrinceEngine::scrollCameraRight() _newCameraX = %d; delta = %d", _newCameraX, delta);
    }   
}

void PrinceEngine::keyHandler(Common::Event event) {
	uint16 nChar = event.kbd.keycode;
	switch (nChar) {
	case Common::KEYCODE_d:
		if (event.kbd.hasFlags(Common::KBD_CTRL)) {
			getDebugger()->attach();
		}
		break;
	case Common::KEYCODE_LEFT:
		scrollCameraLeft(32);
		break;
	case Common::KEYCODE_RIGHT:
		scrollCameraRight(32);
		break;
	case Common::KEYCODE_ESCAPE:
		_script->setFlagValue(Flags::ESCAPED2, 1);
		break;
	}
}

void PrinceEngine::hotspot() {
	Common::Point mousepos = _system->getEventManager()->getMousePos();
	Common::Point mousePosCamera(mousepos.x + _cameraX, mousepos.y);

	for (Common::Array<Mob>::const_iterator it = _mobList.begin()
		; it != _mobList.end() ; ++it) {
		const Mob& mob = *it;
		if (mob._visible)
			continue;
		if (mob._rect.contains(mousePosCamera)) {
			uint16 textW = 0;
			for (uint16 i = 0; i < mob._name.size(); ++i)
				textW += _font->getCharWidth(mob._name[i]);

			uint16 x = mousepos.x - textW/2;
			if (x > _graph->_frontScreen->w)
				x = 0;

			if (x + textW > _graph->_frontScreen->w)
				x = _graph->_frontScreen->w - textW;

			uint16 y = mousepos.y - _font->getFontHeight();
			if (y > _graph->_frontScreen->h)
				y = _font->getFontHeight() - 2;

			_font->drawString(
				_graph->_frontScreen, 
				mob._name, 
				x,
				y,
				_graph->_frontScreen->w,
				216
			);
			break;
		}
	}
}

void PrinceEngine::printAt(uint32 slot, uint8 color, const char *s, uint16 x, uint16 y) {

	debugC(1, DebugChannel::kEngine, "PrinceEngine::printAt slot %d, color %d, x %02d, y %02d, str %s", slot, color, x, y, s);

	Text &text = _textSlots[slot];
	text._str = s;
	text._x = x;
	text._y = y;
	text._color = color;
}

uint32 PrinceEngine::getTextWidth(const char *s) {
    uint16 textW = 0;
	while (*s) {
        textW += _font->getCharWidth(*s) + _font->getKerningOffset(0, 0);
		++s;
    }
    return textW;
}

void PrinceEngine::showTexts() {
	for (uint32 slot = 0; slot < MAXTEXTS; ++slot) {
		Text& text = _textSlots[slot];
		if (!text._str && !text._time)
			continue;

		Common::Array<Common::String> lines;
		_font->wordWrapText(text._str, _graph->_frontScreen->w, lines);

		for (uint8 i = 0; i < lines.size(); ++i) {
			_font->drawString(
				_graph->_frontScreen,
				lines[i],
				text._x - getTextWidth(lines[i].c_str())/2,
				text._y - (lines.size() - i) * (_font->getFontHeight()),
				_graph->_frontScreen->w,
				text._color
			);
		}

		--text._time;
		if (text._time == 0) {
			text._str = NULL;
		}
	}
}

void PrinceEngine::drawScreen() {
	const Graphics::Surface *roomSurface = _roomBmp->getSurface();	
	if (roomSurface) {
		_graph->setPalette(_roomBmp->getPalette());
		const Graphics::Surface visiblePart = roomSurface->getSubArea(Common::Rect(_cameraX, 0, roomSurface->w, roomSurface->h));
		_graph->draw(0, 0, &visiblePart);
	}

	playNextFrame();

	//if (_objectList)
	//	  _graph->drawTransparent(_objectList->getSurface());

	hotspot();

	showTexts();

	getDebugger()->onFrame();

	_graph->update();
}

void PrinceEngine::mainLoop() {

	loadLocation(2);
	changeCursor(1);

	while (!shouldQuit()) {
		uint32 currentTime = _system->getMillis();

		Common::Event event;
		Common::EventManager *eventMan = _system->getEventManager();
		while (eventMan->pollEvent(event)) {
			switch (event.type) {
			case Common::EVENT_KEYDOWN:
				keyHandler(event);
				break;
			case Common::EVENT_KEYUP:
				break;
			case Common::EVENT_MOUSEMOVE:
				break;
			case Common::EVENT_LBUTTONDOWN:
			case Common::EVENT_RBUTTONDOWN:
				break;
			case Common::EVENT_LBUTTONUP:
			case Common::EVENT_RBUTTONUP:
				break;
			case Common::EVENT_QUIT:
				break;
			default:
				break;
			}
		}

		if (shouldQuit())
			return;

		//_script->step();
		drawScreen();

		// Calculate the frame delay based off a desired frame time
		int delay = 1000/15 - int32(_system->getMillis() - currentTime);
		// Ensure non-negative
		delay = delay < 0 ? 0 : delay;
		_system->delayMillis(delay);
		
		_cameraX = _newCameraX;
		++_frameNr;

		if (_debugger->_locationNr != _locationNr)
			loadLocation(_debugger->_locationNr);
		if (_debugger->_cursorNr != _cursorNr)
			changeCursor(_debugger->_cursorNr);
	}
}

} // End of namespace Prince

/* vim: set tabstop=4 expandtab!: */