-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppStateManager.cpp
More file actions
186 lines (142 loc) · 4.15 KB
/
AppStateManager.cpp
File metadata and controls
186 lines (142 loc) · 4.15 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
//================================================//
#include "AppStateManager.hpp"
//================================================//
AppStateManager::AppStateManager(void)
{
m_bShutdown = false;
}
//================================================//
AppStateManager::~AppStateManager(void)
{
state_info_t si;
for(;!m_ActiveStateStack.empty();){
m_ActiveStateStack.back()->exit();
m_ActiveStateStack.pop_back();
}
for(;m_States.empty();){
si = m_States.back();
si.state->destroy();
m_States.pop_back();
}
}
//================================================//
void AppStateManager::manageAppState(Ogre::String stateName, AppState* state)
{
try{
state_info_t new_state_info;
new_state_info.name = stateName;
new_state_info.state = state;
m_States.push_back(new_state_info);
} catch(std::exception& e){
delete state;
throw Ogre::Exception(Ogre::Exception::ERR_INTERNAL_ERROR, "Error while trying to manage a new app state\n" + Ogre::String(e.what()), "AppStateManager.cpp (41)");
}
}
//================================================//
AppState* AppStateManager::findByName(Ogre::String stateName)
{
std::vector<state_info_t>::iterator itr;
for(itr=m_States.begin(); itr!=m_States.end(); ++itr){
if(itr->name == stateName){
return itr->state;
}
}
return 0;
}
//================================================//
/* The main application loop */
void AppStateManager::start(AppState* state)
{
changeAppState(state);
int timeSinceLastFrame = 1;
int startTime = 0;
for(;!m_bShutdown;){
if(Base::getSingletonPtr()->m_pRenderWindow->isClosed())
m_bShutdown = true;
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
Ogre::WindowEventUtilities::messagePump();
#endif
if(Base::getSingletonPtr()->m_pRenderWindow->isActive()){
startTime = Base::getSingletonPtr()->m_pTimer->getMillisecondsCPU();
Base::getSingletonPtr()->m_pKeyboard->capture();
Base::getSingletonPtr()->m_pMouse->capture();
// Update the active state
m_ActiveStateStack.back()->update(timeSinceLastFrame);
// General Ogre updates
Base::getSingletonPtr()->updateOgre(timeSinceLastFrame);
// Render the frame
Base::getSingletonPtr()->m_pRoot->renderOneFrame();
timeSinceLastFrame = Base::getSingletonPtr()->m_pTimer->getMillisecondsCPU() - startTime;
}
else{
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
Sleep(1000);
#else
Sleep(1);
#endif
}
}
Base::getSingletonPtr()->m_pLog->logMessage("Shutting down...");
}
//================================================//
void AppStateManager::changeAppState(AppState* state)
{
if(!m_ActiveStateStack.empty()){
m_ActiveStateStack.back()->exit();
m_ActiveStateStack.pop_back();
}
m_ActiveStateStack.push_back(state);
init(state);
m_ActiveStateStack.back()->enter();
}
//================================================//
bool AppStateManager::pushAppState(AppState* state)
{
if(!m_ActiveStateStack.empty()){
if(!m_ActiveStateStack.back()->pause())
return false;
}
m_ActiveStateStack.push_back(state);
init(state);
m_ActiveStateStack.back()->enter();
return true;
}
//================================================//
void AppStateManager::popAppState(void)
{
if(!m_ActiveStateStack.empty()){
m_ActiveStateStack.back()->exit();
m_ActiveStateStack.pop_back();
}
if(!m_ActiveStateStack.empty()){
init(m_ActiveStateStack.back());
m_ActiveStateStack.back()->resume();
}
else{
shutdown();
}
}
//================================================//
void AppStateManager::pauseAppState(void)
{
if(!m_ActiveStateStack.empty()){
m_ActiveStateStack.back()->pause();
}
if(m_ActiveStateStack.size() > 2){
init(m_ActiveStateStack.at(m_ActiveStateStack.size() - 2));
m_ActiveStateStack.at(m_ActiveStateStack.size() - 2)->resume();
}
}
//================================================//
void AppStateManager::shutdown(void)
{
m_bShutdown = true;
}
//================================================//
void AppStateManager::init(AppState* state)
{
Base::getSingletonPtr()->m_pKeyboard->setEventCallback(state);
Base::getSingletonPtr()->m_pMouse->setEventCallback(state);
Base::getSingletonPtr()->m_pRenderWindow->resetStatistics();
}
//================================================//