forked from tstellar/cs510fly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
244 lines (196 loc) · 6.97 KB
/
Game.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
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
// Game.cpp
// @author Eric D. Wills
#include "Game.h"
#include "Airplane.h"
#include "ConfigReader.h"
#include "Display.h"
#include "InputListener.h"
#include "Level.h"
#include "World.h"
#ifdef LINUX
#include <AL/alc.h>
#include <AL/alure.h>
#else
#include <OpenAL/alc.h>
// Yeah, it's deprecated as hell and missing from the headers. But it's still in the binaries. So sue me.
extern "C" void alutLoadWAVFile(const char*, ALenum*, ALvoid*, ALsizei*, ALsizei*);
extern "C" void alutUnloadWAV(ALenum, ALvoid*, ALsizei, ALsizei);
#endif
// This function will locate the path to our application on OS X,
// unlike windows you can not rely on the curent working directory
// for locating your configuration files and resources.
// From ExampleApplication.h in Ogre samples
#ifndef LINUX
std::string macBundlePath()
{
char path[1024];
CFBundleRef mainBundle = CFBundleGetMainBundle();
assert(mainBundle);
CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
assert(mainBundleURL);
CFStringRef cfStringRef = CFURLCopyFileSystemPath( mainBundleURL, kCFURLPOSIXPathStyle);
assert(cfStringRef);
CFStringGetCString(cfStringRef, path, 1024, kCFStringEncodingASCII);
CFRelease(mainBundleURL);
CFRelease(cfStringRef);
return std::string(path);
}
#endif
Game::Game() : currentLevel(NULL), cameraNode(NULL), breaking(false) {
#ifndef LINUX
mResourcePath = macBundlePath() + "/Contents/Resources/";
levelPath = mResourcePath;
audioPath = mResourcePath;
Ogre::String pluginsPath = macBundlePath() + "/Contents/Resources/plugins.cfg";
root.reset(new Ogre::Root(pluginsPath, mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log"));
#else
levelPath = "data/levels/";
audioPath = "data/audio/";
root.reset(new Ogre::Root("plugins-linux.cfg","ogre.cfg","Ogre.log"));
#endif
}
void Game::init() {
if (setup()){
root->startRendering();
}
}
bool Game::setup() {
Ogre::ConfigFile cf;
cf.load(mResourcePath + "resources.cfg");
// iterate through resources.cfg and register resource groups
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
while (seci.hasMoreElements()) {
std::string secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i){
Ogre::ResourceGroupManager::getSingleton().
addResourceLocation(mResourcePath + i->second,
i->first, secName);
}
}
// create render window if user completes config dialog
if (root->showConfigDialog()){
renderWindow = root->initialise(true);
}
else{
return false;
}
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
// craete scene manager
sceneManager = root->createSceneManager("DefaultSceneManager");
Ogre::ColourValue fogColor(0.93f, 0.86f, 0.76f);
// create camera
camera = sceneManager->createCamera("PrimaryCamera");
camera->setNearClipDistance(1.0f);
camera->setFarClipDistance(10000.0f);
// create viewport
Ogre::Viewport* vp = renderWindow->addViewport(camera);
vp->setBackgroundColour(fogColor);
camera->setAspectRatio(vp->getActualWidth()/(float)vp->getActualHeight());
camera->setFOVy(Ogre::Radian(65.0f*Ogre::Math::PI/180.0f));
// set up lighting
sceneManager->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
Ogre::Light* light = sceneManager->createLight("PrimaryLight");
light->setPosition(20.0f, 80.0f, 50.0f);
inputListener.reset(new InputListener(this, renderWindow));
root->addFrameListener(inputListener.get());
// init sound
ALCdevice * device = alcOpenDevice(NULL);
if(device == NULL){
fprintf(stderr, "Unable to open default audio device.\n");
return false;
}
ALCcontext * context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);
alDistanceModel(AL_LINEAR_DISTANCE);
/* Clear error code (I am not sure why this is done, but the example in
* the OpenAL docs do this.
*/
alGetError();
loadWavFile(&motorBuffer, "Running.wav");
loadWavFile(&enemyBuffer, "R2D2a.wav");
// create levels
ConfigReader levelsCfg(getLevelPath() + "levels.cfg");
const Ogre::StringVector& levelFiles = levelsCfg.parse(
"Levels", &Ogre::StringConverter::parseStringVector, Ogre::StringVector());
for (Ogre::StringVector::const_iterator iter = levelFiles.begin(); iter != levelFiles.end(); iter++)
levels.push_back(std::tr1::shared_ptr<Level>(new Level(this, getLevelPath() + *iter)));
startLevel(0);
return true;
}
void Game::startLevel(int index) {
currentLevelIndex = index;
world.reset(new World(this));
currentLevel = levels[index].get();
currentLevel->populate(world.get());
airplane = world->getPlayer();
cameraNode = airplane->getSceneNode()->createChildSceneNode("CameraNode");
cameraNode->attachObject(camera);
// add skybox
sceneManager->setSkyBox(true, currentLevel->getSkyBox(), 100);
display.reset(new Display(this));
display->setup();
}
void Game::update(float dt) {
try {
world->update(dt);
} catch (const Object::StopUpdates& e) {
// Ignore
}
display->update(dt);
const Ogre::Vector3 &cameraPos = cameraNode->getParentSceneNode()->getPosition();
alListener3f(AL_POSITION, cameraPos.x, cameraPos.y, cameraPos.z);
/*TODO: Set listener orientation */
}
void Game::setBreak() {
breaking = true;
}
bool Game::checkBreak() {
if (breaking) {
breaking = false;
return true;
} else {
return false;
}
}
void Game::lose() {
cameraNode->translate(Ogre::Vector3(0.0f, 10.0f, 50.0f));
}
void Game::win() {
startLevel((currentLevelIndex + 1) % levels.size());
}
bool Game::loadWavFile(ALuint * buffer, std::string file) {
#ifdef LINUX
*buffer = alureCreateBufferFromFile((audioPath + file).c_str());
#else
ALenum error;
alGenBuffers(1, buffer);
if ((error = alGetError()) != AL_NO_ERROR){
return false;
}
// Load sound file
ALenum alFormatBuffer;
char * alBuffer;
ALsizei alBufferLen;
ALsizei alFreqBuffer;
alutLoadWAVFile((audioPath + file).c_str(), &alFormatBuffer, &alBuffer, &alBufferLen, &alFreqBuffer);
if ((error = alGetError()) != AL_NO_ERROR){
alDeleteBuffers(1, buffer);
return false;
}
// Copy sound to buffer
alBufferData(*buffer, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);
if ((error = alGetError()) != AL_NO_ERROR){
alDeleteBuffers(1, buffer);
return false;
}
//Unload sound file
alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer);
if ((error = alGetError()) != AL_NO_ERROR){
alDeleteBuffers(1, buffer);
return false;
}
#endif
return true;
}