-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cpp
More file actions
286 lines (217 loc) · 6.43 KB
/
Game.cpp
File metadata and controls
286 lines (217 loc) · 6.43 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
/*
* Game.cpp
* Prototyp
*
* Created by Christian Pehle on 24.08.09.
* Copyright 2009 Universität Heidelberg. All rights reserved.
*
*/
#include "GameState.h"
#include "MenuState.h"
#include "Game.h"
#include "Input.h"
#include "Utils.h"
//#include <Ogre/OgreWindowEventUtilities.h>
#include <Ogre/Ogre.h>
namespace Orbifold {
Game::Game() :
running(false),
ogre(0),
window(0),
input(0),
state(0) {}
Game::~Game() {
if(this->instance) {
if(this->running)
Game::stop();
delete this->instance;
}
if(this->ogre)
delete this->ogre;
}
Game* Game::instance = 0;
Game* Game::getSingleton() {
if (!instance) {
instance = new Game();
}
return instance;
}
void Game::start(){
Game* game = Game::getSingleton();
game->initialise();
game->running = true;
//game->requestStateChange(PlayState::getSingleton());
game->requestStateChange(MenuState::getSingleton());
while(game->running) {
game->input->capture();
game->state->update();
game->ogre->renderOneFrame();
Ogre::WindowEventUtilities::messagePump();
}
game->shutdown();
if(game->ogre) OGRE_DELETE game->ogre;
}
// Initialisation ///////////////////////////////////////////////////////
void Game::initialise(){
this->initOgreRoot();
this->initPlugins();
// RenderWindow has to be initialised before Materialscripts are parsed (in initOgreResources)!
this->initRenderWindow();
this->initOgreResources();
this->initInput();
this->initState();
}
void Game::initOgreRoot() {
Ogre::String workDir = Ogre::StringUtil::BLANK;
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
workDir = macBundlePath() + "/Contents/Resources/";
#endif
//this->ogre = OGRE_NEW Ogre::Root(workDir + "plugins.cfg", workDir + "Ogre.cfg", workDir + "Ogre.log");
this->ogre = OGRE_NEW Ogre::Root();
#ifdef _DEBUG
this->ogre->loadPlugin("RenderSystem_GL_d");
#else
this->ogre->loadPlugin("RenderSystem_GL");
#endif
// this is version dependend !
Ogre::RenderSystemList rlist = this->ogre->getAvailableRenderers();
Ogre::RenderSystemList::iterator it = rlist.begin();
Ogre::RenderSystem *rSys = *it;
this->ogre->setRenderSystem(rSys);
}
void Game::initPlugins() {
// necessary for TerrainManager
this->ogre->loadPlugin("Plugin_OctreeSceneManager");
}
void Game::initRenderWindow() {
if(!this->ogre)
return;
this->ogre->initialise(false);
this->window = this->ogre->createRenderWindow("Prototyp", 800, 600, false, NULL);
// register game to receive window events.
Ogre::WindowEventUtilities::addWindowEventListener(this->window, this);
}
void Game::initOgreResources() {
if(!this->ogre)
return;
this->locateResources();
this->loadResources();
}
void Game::locateResources() {
Ogre::ConfigFile cf;
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
cf.load(macBundlePath()+"/Contents/Resources/resources.cfg");
#else
cf.load("resources.cfg");
#endif
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String sec, type, arch;
while (seci.hasMoreElements()) {
sec = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); i++) {
type = i->first;
arch = i->second;
#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
if (!Ogre::StringUtil::startsWith(arch, "/", false)) {
arch = Ogre::String(macBundlePath() + "/" + arch);
}
#endif
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(arch,type,sec);
}
}
}
void Game::loadResources() {
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
void Game::initInput() {
this->input = InputHandler::getSingleton();
if(!this->window)
return;
this->input->initialise(this->window);
this->input->addMouseListener(this, "Game");
this->input->addKeyListener(this, "Game");
}
void Game::initState(){}
// State Handling ///////////////////////////////////////////////////////
GameState* Game::getCurrentState() {
return this->state;
}
bool Game::requestStateChange(GameState* nstate) {
Game* game = Game::getSingleton();
GameState* pstate = game->state;
if(pstate) {
pstate->exit();
}
game->window->removeAllViewports();
if(nstate) {
nstate->enter();
}
game->state = nstate;
return true;
}
// Input Handling
bool Game::keyPressed(const OIS::KeyEvent &evt) {
//Call KeyPressed of CurrentState
this->state->keyPressed(evt);
return true;
}
bool Game::keyReleased(const OIS::KeyEvent &evt) {
//Call keyReleased of CurrentState
this->state->keyReleased(evt);
return true;
}
bool Game::mouseMoved(const OIS::MouseEvent &evt) {
// You get the picture
this->state->mouseMoved(evt);
return true;
}
bool Game::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id) {
this->state->mousePressed(evt, id);
return true;
}
bool Game::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id) {
this->state->mouseReleased(evt, id);
return true;
}
OIS::Mouse* Game::getMouse() {
return Game::getSingleton()->input->getMouse();
}
OIS::Keyboard* Game::getKeyboard() {
return Game::getSingleton()->input->getKeyboard();
}
// Window Handling ///////////////////////////////////////////////////////////////////
Ogre::RenderWindow* Game::getRenderWindow() {
return Game::getSingleton()->window;
}
void Game::windowResized(Ogre::RenderWindow* rw) {
//
if (this->state)
this->state->windowResized(rw);
this->input->updateWindowDimensions(rw->getWidth(),rw->getHeight());
}
void Game::windowMoved(Ogre::RenderWindow* rw) {
if (this->state)
this->state->windowMoved(rw);
}
bool Game::windowClosing(Ogre::RenderWindow* rw) {
this->running = false;
if (state)
return state->windowClosing(rw);
return true;
}
void Game::windowClosed(Ogre::RenderWindow* rw) {
if (state)
state->windowClosed(rw);
}
void Game::windowFocusChange(Ogre::RenderWindow* rw) {
if (state)
state->windowFocusChange(rw);
}
void Game::shutdown(){}
void Game::stop() {
Game* game = Game::getSingleton();
game->running = false;
}
}