-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayState.cpp
More file actions
201 lines (151 loc) · 4.86 KB
/
PlayState.cpp
File metadata and controls
201 lines (151 loc) · 4.86 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
/*
* PlayState.cpp
* Prototyp
*
* Created by Christian Pehle on 26.08.09.
* Copyright 2009 Universität Heidelberg. All rights reserved.
*
*/
#include "PlayState.h"
#include "MenuState.h"
#include "Game.h"
#include <Ogre/OgreTextureUnitState.h>
#include "RTSCamera.h"
namespace Orbifold {
class Game;
PlayState::PlayState() :
ogre(0),
window(0),
//camRaySceneQuery(0),
initialised(false)
{}
PlayState::~PlayState() {
if(this->instance) {
delete this->instance;
}
}
PlayState* PlayState::instance = 0;
PlayState* PlayState::getSingleton() {
if(!instance) {
instance = new PlayState();
}
return instance;
}
void PlayState::enter() {
PlayState* state = PlayState::getSingleton();
if (state->initialised) {
state->restore();
} else {
state->initialise();
}
}
void PlayState::exit() {
PlayState* state = PlayState::getSingleton();
state->save();
if (state->scene) state->scene->clearScene();
state->tray->hideAll();
}
void PlayState::save() {
if (camera) camera->save();
}
void PlayState::restore() {
PlayState* state = PlayState::getSingleton();
state->setupView();
state->setupContent();
state->camera->restore();
state->tray->showAll();
}
void PlayState::shutdown() {
PlayState* state = PlayState::getSingleton();
if (state->scene) state->scene->clearScene();
if (contentSetup) state->cleanupContent();
contentSetup = false;
if (resourcesLoaded) state->unloadResources();
resourcesLoaded = false;
if (state->scene) state->ogre->destroySceneManager(state->scene);
state->scene = 0;
}
void PlayState::initialise() {
PlayState* state = PlayState::getSingleton();
state->ogre = Ogre::Root::getSingletonPtr();
state->window = Game::getRenderWindow();
state->timer = new Ogre::Timer();
//
Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Essential");
state->tray = new SdkTrayManager("GameControls", state->window, Game::getMouse(), this);
state->tray->getTrayContainer(TL_NONE)->hide();
//state->velocity = Ogre::Vector3(0,0,0);
this->initialiseView();
this->initialiseContent();
this->setupView();
this->setupContent();
// has to be called after the content is setup,
// because its rayscenequery depends on an actual world being present
this->camera->initialise();
initialised = true;
}
void PlayState::initialiseView() {
this->scene = this->ogre->createSceneManager("TerrainSceneManager");
RTSCamera* rtscam = new RTSCamera("PlayerCamera", this->scene);
this->camera = rtscam;
//this->camera->initialise();
}
void PlayState::setupView() {
Ogre::Camera* cam = camera->getCamera();
Ogre::Viewport* v = this->window->addViewport(cam);
//v->setBackgroundColour(Ogre::ColourValue(0.5,0.0,0.5));
cam->setAspectRatio(Ogre::Real(v->getActualWidth())/v->getActualHeight());
}
void PlayState::initialiseContent() {
Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Scene");
}
void PlayState::setupContent(){
Ogre::Plane waterPlane;
// Set ambient light
this->scene->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
// create light
Ogre::Light* l = this->scene->createLight("MainLight");
l->setPosition(20,80,50);
Ogre::ColourValue fadeColour(0.93, 0.86, 0.76);
this->scene->setFog(Ogre::FOG_LINEAR, fadeColour, .001, 500, 1000);
this->window->getViewport(0)->setBackgroundColour(fadeColour);
std::string terrain_config("terrain.cfg");
this->scene->setWorldGeometry(terrain_config);
// Define the required skyplane
Ogre::Plane plane;
plane.d = 5000;
plane.normal = -Ogre::Vector3::UNIT_Y;
}
void PlayState::cleanupContent(){}
void PlayState::locateResources(){}
void PlayState::loadResources(){}
void PlayState::unloadResources(){}
// A lot of stubs.
void PlayState::update() {
// get time since last Update
unsigned long tslu = this->timer->getMilliseconds();
this->timer->reset();
this->camera->update(tslu);
this->tray->update();
}
bool PlayState::keyPressed(const OIS::KeyEvent &evt) {
return true;
}
bool PlayState::keyReleased(const OIS::KeyEvent &evt) {
return true;
}
bool PlayState::mouseMoved(const OIS::MouseEvent &evt) {
if (this->tray->injectMouseMove(evt)) return true;
return true;
}
bool PlayState::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id) {
Game::requestStateChange(MenuState::getSingleton());
return true;
}
bool PlayState::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id) {return true;}
void PlayState::windowResized(Ogre::RenderWindow* rw) {}
void PlayState::windowMoved(Ogre::RenderWindow* rw) {}
bool PlayState::windowClosing(Ogre::RenderWindow* rw) {return true;}
void PlayState::windowClosed(Ogre::RenderWindow* rw) {}
void PlayState::windowFocusChange(Ogre::RenderWindow* rw) {}
}