aboutsummaryrefslogtreecommitdiff
path: root/engines/gob/init.cpp
blob: 2f1dbe5326858df631de5a1c00d05bd5bb1c0fc8 (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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"
#include "common/endian.h"

#include "gob/gob.h"
#include "gob/init.h"
#include "gob/global.h"
#include "gob/util.h"
#include "gob/dataio.h"
#include "gob/cdrom.h"
#include "gob/draw.h"
#include "gob/game.h"
#include "gob/palanim.h"
#include "gob/sound.h"
#include "gob/video.h"
#include "gob/imd.h"

namespace Gob {

const char *Init::_fontNames[] = { "jeulet1.let", "jeulet2.let", "jeucar1.let", "jeumath.let" };

Init::Init(GobEngine *vm) : _vm(vm) {
	_palDesc = 0;
}

void Init::cleanup(void) {
	_vm->_video->freeDriver();
	_vm->_global->_primarySurfDesc = 0;

	_vm->_snd->speakerOff();
	_vm->_snd->stopSound(0);
	_vm->_dataIO->closeDataFile();
}

void Init::initGame(const char *totName) {
	int16 handle2;
	int16 handle;
	int16 imdHandle;
	char *infBuf;
	char *infPtr;
	char *infEnd;
	char buffer[20];
	int32 varsCount;

	initVideo();

	handle2 = _vm->_dataIO->openData("intro.stk");
	if (handle2 >= 0) {
		_vm->_dataIO->closeData(handle2);
		_vm->_dataIO->openDataFile("intro.stk");
	}

	_vm->_util->initInput();

	_vm->_video->initPrimary(_vm->_global->_videoMode);
	_vm->_global->_mouseXShift = 1;
	_vm->_global->_mouseYShift = 1;

	_vm->_game->_totTextData = 0;
	_vm->_game->_totFileData = 0;
	_vm->_game->_totResourceTable = 0;
	_vm->_global->_inter_variables = 0;
	_vm->_global->_inter_variablesSizes = 0;
	_palDesc = new Video::PalDesc;

	_vm->validateVideoMode(_vm->_global->_videoMode);

	_vm->_global->_setAllPalette = true;
	_palDesc->vgaPal = _vm->_draw->_vgaPalette;
	_palDesc->unused1 = _vm->_draw->_unusedPalette1;
	_palDesc->unused2 = _vm->_draw->_unusedPalette2;
	_vm->_video->setFullPalette(_palDesc);

	for (int i = 0; i < 8; i++)
		_vm->_draw->_fonts[i] = 0;

	handle = _vm->_dataIO->openData("intro.inf");

	if (handle < 0) {
		for (int i = 0; i < 4; i++) {
			handle2 = _vm->_dataIO->openData(_fontNames[i]);
			if (handle2 >= 0) {
				_vm->_dataIO->closeData(handle2);
				_vm->_draw->_fonts[i] = _vm->_util->loadFont(_fontNames[i]);
			}
		}
	} else {
		_vm->_dataIO->closeData(handle);

		infPtr = (char *) _vm->_dataIO->getData("intro.inf");
		infBuf = infPtr;

		infEnd = infBuf + _vm->_dataIO->getDataSize("intro.inf");

		for (int i = 0; i < 4; i++, infPtr++) {
			int j;

			for (j = 0; infPtr < infEnd && *infPtr >= ' '; j++, infPtr++)
				buffer[j] = *infPtr;
			buffer[j] = 0;

			strcat(buffer, ".let");
			handle2 = _vm->_dataIO->openData(buffer);
			if (handle2 >= 0) {
				_vm->_dataIO->closeData(handle2);
				_vm->_draw->_fonts[i] = _vm->_util->loadFont(buffer);
			}

			if ((infPtr + 1) >= infEnd)
				break;

			infPtr++;
		}
		delete[] infBuf;
	}

	if (totName) {
		strncpy0(buffer, totName, 15);
		strcat(buffer, ".tot");
	} else
		strncpy0(buffer, _vm->_startTot, 19);

	handle = _vm->_dataIO->openData(buffer);

	if (handle >= 0) {
		// Get variables count
		_vm->_dataIO->seekData(handle, 0x2C, SEEK_SET);
		varsCount = _vm->_dataIO->readUint16(handle);
		_vm->_dataIO->closeData(handle);

		_vm->_global->_inter_variables = new byte[varsCount * 4];
		_vm->_global->_inter_variablesSizes = new byte[varsCount * 4];
		_vm->_global->clearVars(varsCount);

		strcpy(_vm->_game->_curTotFile, buffer);

		_vm->_cdrom->testCD(1, "GOB");
		_vm->_cdrom->readLIC("gob.lic");

		// Search for a Coktel logo animation or image to display
		imdHandle = _vm->_dataIO->openData("coktel.imd");
		if (imdHandle >= 0) {
			_vm->_dataIO->closeData(imdHandle);
			_vm->_draw->initScreen();
			_vm->_draw->_cursorIndex = -1;
			_vm->_util->longDelay(200); // Letting everything settle
			_vm->_imdPlayer->play("coktel", -1, -1, true);
			_vm->_draw->closeScreen();
		} else if ((imdHandle = _vm->_dataIO->openData("coktel.clt")) >= 0) {
			_vm->_draw->initScreen();
			_vm->_util->clearPalette();
			_vm->_dataIO->readData(imdHandle, (byte *) _vm->_draw->_vgaPalette, 768);
			_vm->_dataIO->closeData(imdHandle);
			imdHandle = _vm->_dataIO->openData("coktel.ims");
			if (imdHandle >= 0) {
				byte *sprBuf;

				_vm->_dataIO->closeData(imdHandle);
				sprBuf = _vm->_dataIO->getData("coktel.ims");
				_vm->_video->drawPackedSprite(sprBuf, 320, 200, 0, 0, 0,
						_vm->_draw->_frontSurface);
				_vm->_palAnim->fade(_palDesc, 0, 0);
				_vm->_util->delay(500);

				delete[] sprBuf;
			}
			_vm->_draw->closeScreen();
		}

		_vm->_game->start();

		_vm->_cdrom->stopPlaying();
		_vm->_cdrom->freeLICbuffer();

		delete[] _vm->_global->_inter_variables;
		delete[] _vm->_global->_inter_variablesSizes;
		delete[] _vm->_game->_totFileData;
		if (_vm->_game->_totTextData) {
			if (_vm->_game->_totTextData->items)
				delete[] _vm->_game->_totTextData->items;
			delete _vm->_game->_totTextData;
		}
		if (_vm->_game->_totResourceTable) {
			delete[] _vm->_game->_totResourceTable->items;
			delete _vm->_game->_totResourceTable;
		}
	}

	for (int i = 0; i < 4; i++) {
		if (_vm->_draw->_fonts[i] != 0)
			_vm->_util->freeFont(_vm->_draw->_fonts[i]);
	}

	delete _palDesc;
	_vm->_dataIO->closeDataFile();
	_vm->_video->initPrimary(-1);
	cleanup();
}

} // End of namespace Gob