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(screen && "cannot create application without screen");
22 assert(initialState && "cannot create application without initial state");
23 RealPushState(initialState);
26 Application::~Application() {
31 State *Application::CurrentState() {
32 return states.empty() ? 0 : states.top();
35 void Application::UpdateState() {
37 while (!stateChanges.empty()) {
38 switch (stateChanges.front().type) {
39 case StateCommand::PUSH:
40 RealPushState(stateChanges.front().state);
42 case StateCommand::POP:
45 case StateCommand::CHANGE:
46 RealChangeState(stateChanges.front().state);
54 void Application::ChangeState(State *s) {
59 cmd.type = StateCommand::CHANGE;
61 stateChanges.push(cmd);
65 void Application::PushState(State *s) {
70 cmd.type = StateCommand::PUSH;
72 stateChanges.push(cmd);
76 void Application::PopState() {
81 cmd.type = StateCommand::POP;
83 stateChanges.push(cmd);
87 void Application::RealChangeState(State *s) {
88 if (!states.empty()) {
89 states.top()->PauseState(*this, screen->Screen());
90 states.top()->ExitState(*this, screen->Screen());
94 s->EnterState(*this, screen->Screen());
95 s->ResumeState(*this, screen->Screen());
98 void Application::RealPushState(State *s) {
99 if (!states.empty()) {
100 states.top()->PauseState(*this, screen->Screen());
103 s->EnterState(*this, screen->Screen());
104 s->ResumeState(*this, screen->Screen());
107 void Application::RealPopState() {
108 if (states.empty()) return;
109 states.top()->PauseState(*this, screen->Screen());
110 states.top()->ExitState(*this, screen->Screen());
113 if (!states.empty()) {
114 states.top()->ResumeState(*this, screen->Screen());
118 void Application::Quit() {
122 void Application::PopAllStates() {
123 while (!states.empty()) {
124 states.top()->PauseState(*this, screen->Screen());
125 states.top()->ExitState(*this, screen->Screen());
132 void Application::Run() {
133 while (CurrentState()) {
138 void Application::Loop() {
139 Uint32 now(SDL_GetTicks());
140 Uint32 deltaT(now - last);
141 GlobalTimers().Update(deltaT);
142 if (deltaT > 30) deltaT = 30;
144 if (CurrentState()) {
145 CurrentState()->GraphicsTimers().Update(deltaT);
148 if (!StateChangePending()) {
158 void Application::HandleEvents() {
159 if (!CurrentState()) return;
160 input.ResetInteractiveState();
162 while (SDL_PollEvent(&event)) {
163 switch (event.type) {
167 case SDL_VIDEORESIZE:
168 screen->Resize(event.resize.w, event.resize.h);
169 CurrentState()->Resize(event.resize.w, event.resize.h);
173 input.HandleKeyboardEvent(event.key);
180 if (CurrentState()) CurrentState()->HandleEvents(input);
183 void Application::UpdateWorld(Uint32 deltaT) {
184 if (!CurrentState()) return;
185 for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) {
186 CurrentState()->PhysicsTimers().Update(0.001f);
187 CurrentState()->UpdateWorld(0.001f);
191 void Application::Render(void) {
192 if (!CurrentState()) return;
193 CurrentState()->Render(screen->Screen());