Repository navigation
Expand file tree
/
Copy pathGame.cpp
More file actions
265 lines (235 loc) · 5.98 KB
/
Copy pathGame.cpp
File metadata and controls
265 lines (235 loc) · 5.98 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
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
/*
* Copyright 2010 Daniel Albano
*
* This file is part of OpenXcom.
*
* OpenXcom 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.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Game.h"
#include "SDL_mixer.h"
#include "State.h"
#include "Screen.h"
#include "../Interface/Cursor.h"
#include "../Resource/ResourcePack.h"
#include "../Ruleset/Ruleset.h"
#include "../Savegame/SavedGame.h"
#include "Palette.h"
/**
* Starts up SDL with all the subsystems and SDL_mixer for audio processing,
* creates the display screen and sets up the cursor.
* @param title Title of the game window.
* @param width Width of the display screen.
* @param height Height of the display screen.
* @param bpp Bits-per-pixel of the display screen.
* @warning Currently the game is designed for 8bpp, so there's no telling what'll
* happen if you use a different value.
*/
Game::Game(string title, int width, int height, int bpp) : _screen(0), _cursor(0), _states(), _deleted(), _res(0), _save(0), _rules(0), _quit(false)
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
throw SDL_GetError();
}
// Initialize SDL_mixer
if (Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 1024) != 0)
{
throw Mix_GetError();
}
Mix_AllocateChannels(16);
// Set the window caption
SDL_WM_SetCaption(title.c_str(), 0);
// Set up keyboard events
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
SDL_EnableUNICODE(1);
// Create display
_screen = new Screen(width, height, bpp);
// Create cursor
_cursor = new Cursor(9, 13);
_cursor->setColor(Palette::blockOffset(15)+12);
}
/**
* Deletes the display screen, cursor, states and shuts down all the SDL subsystems.
*/
Game::~Game()
{
Mix_HaltChannel(-1);
for (list<State*>::iterator i = _states.begin(); i != _states.end(); i++)
delete *i;
delete _cursor;
if (_res != 0)
delete _res;
if (_rules != 0)
delete _rules;
if (_save != 0)
delete _save;
delete _screen;
Mix_CloseAudio();
SDL_Quit();
}
/**
* The state machine takes care of passing all the events from SDL to the
* active state, running any code within and blitting all the states and
* cursor to the screen. This is run indefinitely until the game quits.
*/
void Game::run()
{
while (!_quit)
{
// Clean up states
while (!_deleted.empty())
{
delete _deleted.back();
_deleted.pop_back();
}
// Process events
while (SDL_PollEvent(&_event))
{
if (_event.type == SDL_QUIT)
{
_quit = true;
}
else
{
_cursor->handle(&_event, _screen->getScale());
_states.back()->handle(&_event, _screen->getScale());
}
}
// Process logic
_states.back()->think();
// Process rendering
_screen->clear();
list<State*>::iterator i = _states.end();
do
i--;
while(i != _states.begin() && !(*i)->isScreen());
for (; i != _states.end(); i++)
(*i)->blit();
_cursor->blit(_screen->getSurface());
_screen->flip();
SDL_Delay(0);
}
}
/**
* Stops the state machine and the game is shut down.
*/
void Game::quit()
{
_quit = true;
}
/**
* Returns the display screen used by the game.
* @return Pointer to the screen.
*/
Screen *Game::getScreen()
{
return _screen;
}
/**
* Replaces a certain amount of colors in the palettes of the game's
* screen and resources.
* @param colors Pointer to the set of colors.
* @param firstcolor Offset of the first color to replace.
* @param ncolors Amount of colors to replace.
*/
void Game::setPalette(SDL_Color *colors, int firstcolor, int ncolors)
{
_screen->setPalette(colors, firstcolor, ncolors);
_cursor->setPalette(colors, firstcolor, ncolors);
if (_res != 0)
_res->setPalette(colors, firstcolor, ncolors);
}
/**
* Pops all the states currently in stack and pushes in the new state.
* A shortcut for cleaning up all the old states when they're not necessary
* like in one-way transitions.
* @param state Pointer to the new state.
*/
void Game::setState(State *state)
{
while (!_states.empty())
popState();
pushState(state);
}
/**
* Pushes a new state into the top of the stack and initializes it.
* The new state will be used once the next game cycle starts.
* @param state Pointer to the new state.
*/
void Game::pushState(State *state)
{
_states.push_back(state);
state->init();
}
/**
* Pops the last state from the top of the stack. Since states
* can't actually be deleted mid-cycle, it's moved into a separate queue
* which is cleared at the start of every cycle, so the transition
* is seamless.
*/
void Game::popState()
{
_deleted.push_back(_states.back());
_states.pop_back();
if (!_states.empty())
_states.back()->init();
}
/**
* Returns the resource pack currently in use by the game.
* @return Pointer to the resource pack.
*/
ResourcePack *Game::getResourcePack()
{
return _res;
}
/**
* Sets a new resource pack for the game to use.
* @param res Pointer to the resource pack.
*/
void Game::setResourcePack(ResourcePack *res)
{
_res = res;
}
/**
* Returns the saved game currently in use by the game.
* @return Pointer to the saved game.
*/
SavedGame *Game::getSavedGame()
{
return _save;
}
/**
* Sets a new saved game for the game to use.
* @param save Pointer to the saved game.
*/
void Game::setSavedGame(SavedGame *save)
{
_save = save;
}
/**
* Returns the ruleset currently in use by the game.
* @return Pointer to the ruleset.
*/
Ruleset *Game::getRuleset()
{
return _rules;
}
/**
* Sets a new ruleset for the game to use.
* @param rules Pointer to the ruleset.
*/
void Game::setRuleset(Ruleset *rules)
{
_rules = rules;
}