-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.cpp
189 lines (154 loc) · 4.61 KB
/
engine.cpp
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
/* Raw - Another World Interpreter
* Copyright (C) 2004 Gregory Montoir
*
* 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.
*/
#include "engine.h"
#include "file.h"
#include "serializer.h"
#include "sys.h"
#include "parts.h"
Engine::Engine(System *paramSys, const char *dataDir, const char *saveDir)
: sys(paramSys), vm(&mixer, &res, &player, &video, sys), mixer(sys), res(&video, dataDir),
player(&mixer, &res, sys), video(&res, sys), _dataDir(dataDir), _saveDir(saveDir), _stateSlot(0) {
}
void Engine::run() {
while (!sys->input.quit) {
vm.checkThreadRequests();
vm.inp_updatePlayer();
processInput();
vm.hostFrame();
}
}
Engine::~Engine(){
finish();
sys->destroy();
}
void Engine::init() {
//Init system
sys->init("Out Of This World");
video.init();
res.allocMemBlock();
res.readEntries();
vm.init();
mixer.init();
player.init();
//Init virtual machine, legacy way
vm.initForPart(GAME_PART_FIRST); // This game part is the protection screen
// Try to cheat here. You can jump anywhere but the VM crashes afterward.
// Starting somewhere is probably not enough, the variables and calls return are probably missing.
//vm.initForPart(GAME_PART2); // Skip protection screen and go directly to intro
//vm.initForPart(GAME_PART3); // CRASH
//vm.initForPart(GAME_PART4); // Start directly in jail but then crash
//vm.initForPart(GAME_PART5); //CRASH
//vm.initForPart(GAME_PART6); // Start in the battlechar but CRASH afteward
//vm.initForPart(GAME_PART7); //CRASH
//vm.initForPart(GAME_PART8); //CRASH
//vm.initForPart(GAME_PART9); // Green screen not doing anything
}
void Engine::finish() {
player.free();
mixer.free();
res.freeMemBlock();
}
void Engine::processInput() {
if (sys->input.load) {
loadGameState(_stateSlot);
sys->input.load = false;
}
if (sys->input.save) {
saveGameState(_stateSlot, "quicksave");
sys->input.save = false;
}
if (sys->input.fastMode) {
vm._fastMode = !vm._fastMode;
sys->input.fastMode = false;
}
if (sys->input.stateSlot != 0) {
int8_t slot = _stateSlot + sys->input.stateSlot;
if (slot >= 0 && slot < MAX_SAVE_SLOTS) {
_stateSlot = slot;
debug(DBG_INFO, "Current game state slot is %d", _stateSlot);
}
sys->input.stateSlot = 0;
}
}
void Engine::makeGameStateName(uint8_t slot, char *buf) {
sprintf(buf, "raw.s%02d", slot);
}
void Engine::saveGameState(uint8_t slot, const char *desc) {
char stateFile[20];
makeGameStateName(slot, stateFile);
File f(true);
if (!f.open(stateFile, _saveDir, "wb")) {
warning("Unable to save state file '%s'", stateFile);
} else {
// header
f.writeUint32BE('AWSV');
f.writeUint16BE(Serializer::CUR_VER);
f.writeUint16BE(0);
char hdrdesc[32];
strncpy(hdrdesc, desc, sizeof(hdrdesc) - 1);
f.write(hdrdesc, sizeof(hdrdesc));
// contents
Serializer s(&f, Serializer::SM_SAVE, res._memPtrStart);
vm.saveOrLoad(s);
res.saveOrLoad(s);
video.saveOrLoad(s);
player.saveOrLoad(s);
mixer.saveOrLoad(s);
if (f.ioErr()) {
warning("I/O error when saving game state");
} else {
debug(DBG_INFO, "Saved state to slot %d", _stateSlot);
}
}
}
void Engine::loadGameState(uint8_t slot) {
char stateFile[20];
makeGameStateName(slot, stateFile);
File f(true);
if (!f.open(stateFile, _saveDir, "rb")) {
warning("Unable to open state file '%s'", stateFile);
} else {
uint32_t id = f.readUint32BE();
if (id != 'AWSV') {
warning("Bad savegame format");
} else {
// mute
player.stop();
mixer.stopAll();
// header
uint16_t ver = f.readUint16BE();
f.readUint16BE();
char hdrdesc[32];
f.read(hdrdesc, sizeof(hdrdesc));
// contents
Serializer s(&f, Serializer::SM_LOAD, res._memPtrStart, ver);
vm.saveOrLoad(s);
res.saveOrLoad(s);
video.saveOrLoad(s);
player.saveOrLoad(s);
mixer.saveOrLoad(s);
}
if (f.ioErr()) {
warning("I/O error when loading game state");
} else {
debug(DBG_INFO, "Loaded state from slot %d", _stateSlot);
}
}
}
const char* Engine::getDataDir()
{
return this->_dataDir;
}