-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
executable file
·148 lines (136 loc) · 3.95 KB
/
engine.cpp
File metadata and controls
executable file
·148 lines (136 loc) · 3.95 KB
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
/* 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 "systemstub.h"
Engine::Engine(SystemStub *stub, const char *dataDir, const char *saveDir)
: _stub(stub), _log(&_mix, &_res, &_ply, &_vid, _stub), _mix(_stub), _res(&_vid, dataDir),
_ply(&_mix, &_res, _stub), _vid(&_res, stub), _dataDir(dataDir), _saveDir(saveDir), _stateSlot(0) {
}
void Engine::run() {
_stub->init("Out Of This World");
setup();
_log.restartAt(0x3E80); // demo starts at 0x3E81
while (!_stub->_pi.quit) {
_log.setupScripts();
_log.inp_updatePlayer();
processInput();
_log.runScripts();
}
finish();
_stub->destroy();
}
void Engine::setup() {
_vid.init();
_res.allocMemBlock();
_res.readEntries();
_log.init();
_mix.init();
_ply.init();
}
void Engine::finish() {
_ply.free();
_mix.free();
_res.freeMemBlock();
}
void Engine::processInput() {
if (_stub->_pi.load) {
loadGameState(_stateSlot);
_stub->_pi.load = false;
}
if (_stub->_pi.save) {
saveGameState(_stateSlot, "quicksave");
_stub->_pi.save = false;
}
if (_stub->_pi.fastMode) {
_log._fastMode = !_log._fastMode;
_stub->_pi.fastMode = false;
}
if (_stub->_pi.stateSlot != 0) {
int8 slot = _stateSlot + _stub->_pi.stateSlot;
if (slot >= 0 && slot < MAX_SAVE_SLOTS) {
_stateSlot = slot;
debug(DBG_INFO, "Current game state slot is %d", _stateSlot);
}
_stub->_pi.stateSlot = 0;
}
}
void Engine::makeGameStateName(uint8 slot, char *buf) {
sprintf(buf, "raw.s%02d", slot);
}
void Engine::saveGameState(uint8 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);
_log.saveOrLoad(s);
_res.saveOrLoad(s);
_vid.saveOrLoad(s);
_ply.saveOrLoad(s);
_mix.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 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 id = f.readUint32BE();
if (id != 'AWSV') {
warning("Bad savegame format");
} else {
// mute
_ply.stop();
_mix.stopAll();
// header
uint16 ver = f.readUint16BE();
f.readUint16BE();
char hdrdesc[32];
f.read(hdrdesc, sizeof(hdrdesc));
// contents
Serializer s(&f, Serializer::SM_LOAD, _res._memPtrStart, ver);
_log.saveOrLoad(s);
_res.saveOrLoad(s);
_vid.saveOrLoad(s);
_ply.saveOrLoad(s);
_mix.saveOrLoad(s);
}
if (f.ioErr()) {
warning("I/O error when loading game state");
} else {
debug(DBG_INFO, "Loaded state from slot %d", _stateSlot);
}
}
}