4 * Created on: Apr 8, 2012
8 #include "Application.h"
16 Application::Application(sdl::InitScreen *screen, State *initialState)
19 , last(SDL_GetTicks()) {
20 assert(screen && "cannot create application without screen");
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() {
35 while (!stateChanges.empty()) {
36 switch (stateChanges.front().type) {
37 case StateCommand::PUSH:
38 RealPushState(stateChanges.front().state);
40 case StateCommand::POP:
43 case StateCommand::CHANGE:
44 RealChangeState(stateChanges.front().state);
51 void Application::ChangeState(State *s) {
53 cmd.type = StateCommand::CHANGE;
55 stateChanges.push(cmd);
58 void Application::PushState(State *s) {
60 cmd.type = StateCommand::PUSH;
62 stateChanges.push(cmd);
65 void Application::PopState() {
67 cmd.type = StateCommand::POP;
69 stateChanges.push(cmd);
72 void Application::RealChangeState(State *s) {
73 if (!states.empty()) {
74 states.top()->PauseState(*this, screen->Screen());
75 states.top()->ExitState(*this, screen->Screen());
79 s->EnterState(*this, screen->Screen());
80 s->ResumeState(*this, screen->Screen());
83 void Application::RealPushState(State *s) {
84 if (!states.empty()) {
85 states.top()->PauseState(*this, screen->Screen());
88 s->EnterState(*this, screen->Screen());
89 s->ResumeState(*this, screen->Screen());
92 void Application::RealPopState() {
93 if (states.empty()) return;
94 states.top()->PauseState(*this, screen->Screen());
95 states.top()->ExitState(*this, screen->Screen());
98 if (!states.empty()) {
99 states.top()->ResumeState(*this, screen->Screen());
103 void Application::Quit() {
107 void Application::PopAllStates() {
108 while (!states.empty()) {
114 void Application::Run() {
115 while (CurrentState()) {
120 void Application::Loop() {
121 Uint32 now(SDL_GetTicks());
122 Uint32 deltaT(now - last);
123 GlobalTimers().Update(deltaT);
124 if (deltaT > 30) deltaT = 30;
126 if (CurrentState()) {
127 CurrentState()->GraphicsTimers().Update(deltaT);
130 if (!StateChangePending()) {
140 void Application::HandleEvents() {
141 if (!CurrentState()) return;
142 input.ResetInteractiveState();
144 while (SDL_PollEvent(&event)) {
145 switch (event.type) {
149 case SDL_VIDEORESIZE:
150 screen->Resize(event.resize.w, event.resize.h);
151 CurrentState()->Resize(event.resize.w, event.resize.h);
155 input.HandleKeyboardEvent(event.key);
162 if (CurrentState()) CurrentState()->HandleEvents(input);
165 void Application::UpdateWorld(Uint32 deltaT) {
166 if (!CurrentState()) return;
167 for (Uint32 i(0); i < deltaT; ++i) {
168 CurrentState()->PhysicsTimers().Update(0.001f);
169 CurrentState()->UpdateWorld(0.001f);
173 void Application::Render(void) {
174 if (!CurrentState()) return;
175 CurrentState()->Render(screen->Screen());