4 * Created on: Apr 8, 2012
8 #include "Application.h"
16 Application::Application(sdl::InitScreen &screen, State *initialState)
19 , last(SDL_GetTicks())
20 , inStateChage(false) {
21 assert(initialState && "cannot create application without initial state");
22 RealPushState(initialState);
25 Application::~Application() {
30 State *Application::CurrentState() {
31 return states.empty() ? 0 : states.top();
34 void Application::UpdateState() {
36 while (!stateChanges.empty()) {
37 switch (stateChanges.front().type) {
38 case StateCommand::PUSH:
39 RealPushState(stateChanges.front().state);
41 case StateCommand::POP:
44 case StateCommand::CHANGE:
45 RealChangeState(stateChanges.front().state);
53 void Application::ChangeState(State *s) {
58 cmd.type = StateCommand::CHANGE;
60 stateChanges.push(cmd);
64 void Application::PushState(State *s) {
69 cmd.type = StateCommand::PUSH;
71 stateChanges.push(cmd);
75 void Application::PopState() {
80 cmd.type = StateCommand::POP;
82 stateChanges.push(cmd);
86 void Application::RealChangeState(State *s) {
87 if (!states.empty()) {
88 states.top()->PauseState(screen.Screen());
89 states.top()->ExitState(*this, screen.Screen());
93 s->EnterState(*this, screen.Screen());
94 s->ResumeState(screen.Screen());
97 void Application::RealPushState(State *s) {
98 if (!states.empty()) {
99 states.top()->PauseState(screen.Screen());
102 s->EnterState(*this, screen.Screen());
103 s->ResumeState(screen.Screen());
106 void Application::RealPopState() {
107 if (states.empty()) return;
108 states.top()->PauseState(screen.Screen());
109 states.top()->ExitState(*this, screen.Screen());
112 if (!states.empty()) {
113 states.top()->ResumeState(screen.Screen());
117 void Application::Quit() {
121 void Application::PopAllStates() {
122 while (!states.empty()) {
123 states.top()->PauseState(screen.Screen());
124 states.top()->ExitState(*this, screen.Screen());
131 void Application::Run() {
132 while (CurrentState()) {
137 void Application::Loop() {
138 Uint32 now(SDL_GetTicks());
139 Uint32 deltaT(now - last);
140 GlobalTimers().Update(deltaT);
141 if (deltaT > 30) deltaT = 30;
143 if (CurrentState()) {
144 CurrentState()->GraphicsTimers().Update(deltaT);
147 if (!StateChangePending()) {
157 void Application::HandleEvents() {
158 if (!CurrentState()) return;
159 input.ResetInteractiveState();
161 while (SDL_PollEvent(&event)) {
162 switch (event.type) {
166 case SDL_VIDEORESIZE:
167 screen.Resize(event.resize.w, event.resize.h);
168 CurrentState()->Resize(event.resize.w, event.resize.h);
172 input.HandleKeyboardEvent(event.key);
179 if (CurrentState()) CurrentState()->HandleEvents(input);
182 void Application::UpdateWorld(Uint32 deltaT) {
183 if (!CurrentState()) return;
184 for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) {
185 CurrentState()->PhysicsTimers().Update(0.001f);
186 CurrentState()->UpdateWorld(0.001f);
190 void Application::Render(void) {
191 if (!CurrentState()) return;
192 CurrentState()->Render(screen.Screen());