]> git.localhorst.tv Git - l2e.git/blobdiff - src/app/Application.cpp
switched geometric scalars from floating to fixed
[l2e.git] / src / app / Application.cpp
index 19aa06a18543de3e1c7662a490098d794e8e0aca..76d98efc7fbc2609053391f7b7894693523a6c9f 100644 (file)
@@ -1,10 +1,3 @@
-/*
- * Application.cpp
- *
- *  Created on: Apr 8, 2012
- *      Author: holy
- */
-
 #include "Application.h"
 
 #include "State.h"
 
 namespace app {
 
-Application::Application(sdl::InitScreen *screen, State *initialState)
+Application::Application(sdl::InitScreen &screen, State *initialState)
 : screen(screen)
 , states()
 , last(SDL_GetTicks())
-, toPop(0) {
-       assert(screen && "cannot create application without screen");
+, inStateChage(false) {
        assert(initialState && "cannot create application without initial state");
        RealPushState(initialState);
 }
@@ -29,44 +21,90 @@ Application::~Application() {
 
 
 State *Application::CurrentState() {
-       return states.top();
+       return states.empty() ? 0 : states.top();
 }
 
 void Application::UpdateState() {
-       while (toPop > 0) {
-               RealPopState();
-               --toPop;
-       }
-       while (!toPush.empty()) {
-               State *s(toPush.front());
-               toPush.pop();
-               RealPushState(s);
+       inStateChage = true;
+       while (!stateChanges.empty()) {
+               switch (stateChanges.front().type) {
+                       case StateCommand::PUSH:
+                               RealPushState(stateChanges.front().state);
+                               break;
+                       case StateCommand::POP:
+                               RealPopState();
+                               break;
+                       case StateCommand::CHANGE:
+                               RealChangeState(stateChanges.front().state);
+                               break;
+               }
+               stateChanges.pop();
        }
+       inStateChage = false;
 }
 
 void Application::ChangeState(State *s) {
-       PopState();
-       PushState(s);
+       if (inStateChage) {
+               RealChangeState(s);
+       } else {
+               StateCommand cmd;
+               cmd.type = StateCommand::CHANGE;
+               cmd.state = s;
+               stateChanges.push(cmd);
+       }
 }
 
 void Application::PushState(State *s) {
-       toPush.push(s);
+       if (inStateChage) {
+               RealPushState(s);
+       } else {
+               StateCommand cmd;
+               cmd.type = StateCommand::PUSH;
+               cmd.state = s;
+               stateChanges.push(cmd);
+       }
 }
 
-void Application::RealPushState(State *s) {
+void Application::PopState() {
+       if (inStateChage) {
+               RealPopState();
+       } else {
+               StateCommand cmd;
+               cmd.type = StateCommand::POP;
+               cmd.state = 0;
+               stateChanges.push(cmd);
+       }
+}
+
+void Application::RealChangeState(State *s) {
+       if (!states.empty()) {
+               states.top()->PauseState(screen.Screen());
+               states.top()->ExitState(*this, screen.Screen());
+               states.pop();
+       }
        states.push(s);
-       s->EnterState(*this, screen->Screen());
+       s->EnterState(*this, screen.Screen());
+       s->ResumeState(screen.Screen());
 }
 
-void Application::PopState() {
-       ++toPop;
+void Application::RealPushState(State *s) {
+       if (!states.empty()) {
+               states.top()->PauseState(screen.Screen());
+       }
+       states.push(s);
+       s->EnterState(*this, screen.Screen());
+       s->ResumeState(screen.Screen());
 }
 
 void Application::RealPopState() {
        if (states.empty()) return;
-       states.top()->ExitState();
+       states.top()->PauseState(screen.Screen());
+       states.top()->ExitState(*this, screen.Screen());
        delete states.top();
        states.pop();
+       if (!states.empty()) {
+               states.top()->ResumeState(screen.Screen());
+       }
 }
 
 void Application::Quit() {
@@ -75,7 +113,10 @@ void Application::Quit() {
 
 void Application::PopAllStates() {
        while (!states.empty()) {
-               RealPopState();
+               states.top()->PauseState(screen.Screen());
+               states.top()->ExitState(*this, screen.Screen());
+               delete states.top();
+               states.pop();
        }
 }
 
@@ -89,11 +130,17 @@ void Application::Run() {
 void Application::Loop() {
        Uint32 now(SDL_GetTicks());
        Uint32 deltaT(now - last);
-       if (deltaT > 34) deltaT = 34;
+       GlobalTimers().Update(deltaT);
+       if (deltaT > 30) deltaT = 30;
 
+       if (CurrentState()) {
+               CurrentState()->GraphicsTimers().Update(deltaT);
+       }
        HandleEvents();
-       UpdateWorld(deltaT);
-       Render();
+       if (!StateChangePending()) {
+               UpdateWorld(deltaT);
+               Render();
+       }
 
        last = now;
        UpdateState();
@@ -110,7 +157,7 @@ void Application::HandleEvents() {
                                PopAllStates();
                                break;
                        case SDL_VIDEORESIZE:
-                               screen->Resize(event.resize.w, event.resize.h);
+                               screen.Resize(event.resize.w, event.resize.h);
                                CurrentState()->Resize(event.resize.w, event.resize.h);
                                break;
                        case SDL_KEYDOWN:
@@ -122,20 +169,21 @@ void Application::HandleEvents() {
                                break;
                }
        }
-       CurrentState()->HandleInput(input);
+       if (CurrentState()) CurrentState()->HandleEvents(input);
 }
 
 void Application::UpdateWorld(Uint32 deltaT) {
        if (!CurrentState()) return;
-       for (Uint32 i(0); i < deltaT; ++i) {
-               CurrentState()->UpdateWorld(0.001f);
+       for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) {
+               CurrentState()->PhysicsTimers().Update(1);
+               CurrentState()->UpdateWorld(1);
        }
 }
 
 void Application::Render(void) {
        if (!CurrentState()) return;
-       CurrentState()->Render(screen->Screen());
-       screen->Flip();
+       CurrentState()->Render(screen.Screen());
+       screen.Flip();
 }
 
 }