-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGame.cpp
More file actions
executable file
·366 lines (315 loc) · 10.1 KB
/
Game.cpp
File metadata and controls
executable file
·366 lines (315 loc) · 10.1 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
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
//*****************************************************************************
// FILE NAME: Game.cpp
//
//*****************************************************************************
#include "Game.h"
#include "ViewController.h"
#include "MouseListener.h"
#include "KeyListener.h"
// fife includes
#include "controller/engine.h"
#include "controller/enginesettings.h"
#include "util/log/logger.h"
#include "loaders/native/map/maploader.h"
#include "model/structures/map.h"
#include "model/structures/layer.h"
#include "view/camera.h"
#include "eventchannel/eventmanager.h"
#include "gui/guimanager.h"
#include "gui/fifechan/fifechanmanager.h"
#include "gui/fifechan/console/console.h"
#include "util/time/timemanager.h"
#include "video/renderbackend.h"
// 3rd party includes
#include "boost/filesystem.hpp"
#include "SDL.h"
// standard includes
#include <cassert>
namespace fs = boost::filesystem;
//!***************************************************************
//! @details:
//! constructor
//!
//!***************************************************************
Game::Game()
: m_map(0), m_mainCamera(0), m_mouseListener(0), m_keyListener(0), m_player(0), m_quit(false)
{
// create the engine
m_engine = new FIFE::Engine();
// create view controller
m_viewController = new ViewController();
// apply game settings
InitSettings();
// initialize the engine
m_engine->init();
// create default gui
FIFE::FifechanManager* guiManager = new FIFE::FifechanManager();
// setup the gui
guiManager->setDefaultFont(
m_engine->getSettings().getDefaultFontPath(),
m_engine->getSettings().getDefaultFontSize(),
m_engine->getSettings().getDefaultFontGlyphs()
);
guiManager->init(
m_engine->getRenderBackend()->getName(),
m_engine->getRenderBackend()->getScreenWidth(),
m_engine->getRenderBackend()->getScreenHeight()
);
m_engine->setGuiManager(guiManager);
m_engine->getEventManager()->addSdlEventListener(guiManager);
}
//!***************************************************************
//! @details:
//! destructor
//!
//!***************************************************************
Game::~Game()
{
// clean up our resources
delete m_viewController;
m_viewController = 0;
delete m_mouseListener;
m_mouseListener = 0;
delete m_keyListener;
m_keyListener = 0;
// the engine will clean up its resources
delete m_engine;
m_engine = 0;
}
//!***************************************************************
//! @details:
//! initializes the game and subsystems
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::Init()
{
// load the game map
CreateMap();
// initialize the cameras and view
InitView();
// initialize the user input
CreateInput();
// prep the engine for running
m_engine->initializePumping();
}
//!***************************************************************
//! @details:
//! this is the game loop
//! internally calls the engine tick to make sure everything
//! updates
//!
//! @return:
//! bool
//!
//!***************************************************************
void Game::Run()
{
int lastTime = 0;
int currTime = 0;
while (!m_quit)
{
// update fps reading approx. every second
if ((currTime > 0) && (currTime - lastTime >= 1e3))
{
// create FPS string
std::ostringstream oss;
oss << " [FPS: " << static_cast<int>(1e3/m_engine->getTimeManager()->getAverageFrameTime()) << "]";
// show fps in title, and keep it updated
FIFE::EngineSettings& settings = m_engine->getSettings();
std::string windowTitle = settings.getWindowTitle();
windowTitle += oss.str();
// this functionality should be part of the engine
// it will be migrated there soon, so the client
// does not have to call SDL directly
SDL_SetWindowTitle(m_engine->getRenderBackend()->getWindow(), windowTitle.c_str());
// update the last time fps was calculated
lastTime = m_engine->getTimeManager()->getTime();
}
// engine timer tick
m_engine->pump();
// update the current run time
currTime = m_engine->getTimeManager()->getTime();
}
}
//!***************************************************************
//! @details:
//! signal to stop the game loop
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::Quit()
{
m_quit = true;
}
//!***************************************************************
//! @details:
//! toggle the console during game time
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::toggleConsole()
{
// get the engine's GUI manager
FIFE::FifechanManager* guiManager = static_cast<FIFE::FifechanManager*>(m_engine->getGuiManager());
guiManager->getConsole()->toggleShowHide();
}
//!***************************************************************
//! @details:
//! accessor for the game's view controller
//!
//! @return:
//! ViewController*
//!
//!***************************************************************
ViewController* Game::GetViewController()
{
return m_viewController;
}
//!***************************************************************
//! @details:
//! initialize the engine settings
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::InitSettings()
{
// grab the current settings from the engine
FIFE::EngineSettings& settings = m_engine->getSettings();
// get the path to the font we are going to use
fs::path defaultFontPath("assets/fonts/FreeSans.ttf");
// change the engine settings to suite our game
settings.setRenderBackend("OpenGL");
settings.setScreenHeight(600);
settings.setScreenWidth(800);
settings.setBitsPerPixel(0);
settings.setFullScreen(false);
settings.setInitialVolume(5.0);
settings.setWindowTitle("FIFE - Tutorials");
settings.setDefaultFontGlyphs("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\"");
settings.setDefaultFontPath(defaultFontPath.string());
FIFE::LogManager* logManager = m_engine->getLogManager();
if (logManager)
{
// disable logging
logManager->setLogToFile(false);
logManager->setLogToPrompt(false);
}
}
//!***************************************************************
//! @details:
//! attaches the correct map loader to the engine and loads the
//! map file
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::CreateMap()
{
if (m_engine->getModel() && m_engine->getVFS() && m_engine->getImageManager() &&
m_engine->getRenderBackend())
{
// create the default loader for the FIFE map format
//FIFE::DefaultMapLoader* mapLoader = FIFE::createDefaultMapLoader(m_engine->getModel(), m_engine->getVFS(),
// m_engine->getImagePool(), m_engine->getAnimationPool(), m_engine->getRenderBackend());
FIFE::MapLoader* mapLoader = new FIFE::MapLoader(m_engine->getModel(), m_engine->getVFS(),
m_engine->getImageManager(), m_engine->getRenderBackend());
fs::path mapPath("assets/maps/shrine.xml");
if (mapLoader) {
// load the map
m_map = mapLoader->load(mapPath.string());
}
// done with map loader safe to delete
delete mapLoader;
mapLoader = 0;
}
}
//!***************************************************************
//! @details:
//! create the user input devices and attach to engine
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::CreateInput()
{
if (m_engine->getEventManager() && m_engine->getModel())
{
// attach our key listener to the engine
m_keyListener = new KeyListener(this);
m_engine->getEventManager()->addKeyListener(m_keyListener);
// attach our mouse listener to the engine
m_mouseListener = new MouseListener(this, m_mainCamera, m_engine->getEventManager(), m_engine->getTimeManager());
m_engine->getEventManager()->addMouseListener(m_mouseListener);
// grab the layer that has our main character
FIFE::Layer* layer = m_map->getLayer("TechdemoMapGroundObjectLayer");
if (layer)
{
// query the layer for our main character
m_player = layer->getInstance("PC");
if (m_player)
{
// set the main characters default action to standing
m_player->actRepeat("stand", m_player->getLocationRef());
// attach the mouse controller to our main character
// to control the player
m_mouseListener->SetController(m_player);
}
// query the layer for the other character we are interested in
FIFE::Instance* m_npc = layer->getInstance("NPC:girl");
if (m_npc)
{
// set this character's action to standing as well
m_npc->actRepeat("stand", m_npc->getLocationRef());
}
}
}
}
//!***************************************************************
//! @details:
//! initialize the camera and view
//!
//! @return:
//! void
//!
//!***************************************************************
void Game::InitView()
{
if (m_map)
{
// get the main camera for this map
m_mainCamera = m_map->getCamera("main");
if (m_mainCamera)
{
// attach the controller to the camera
m_viewController->AttachCamera(m_mainCamera);
m_viewController->EnableCamera(true);
// get the renderer associated with viewing objects on the map
FIFE::RendererBase* renderer = m_mainCamera->getRenderer("InstanceRenderer");
if (renderer)
{
// activate all layers associated with the renderer
// for this map, this must be done to see anything
renderer->activateAllLayers(m_map);
}
// get the mini camera attached to the map
FIFE::Camera* miniCamera = m_map->getCamera("small");
// default the small camera to off, we will revisit the
// mini camera in a later demo
if (miniCamera)
{
miniCamera->setEnabled(false);
}
}
}
}