1 #include "Application.h"
9 Application::Application(sdl::InitScreen &screen, State *initialState)
12 , last(SDL_GetTicks())
13 , inStateChage(false) {
14 assert(initialState && "cannot create application without initial state");
15 RealPushState(initialState);
18 Application::~Application() {
23 State *Application::CurrentState() {
24 return states.empty() ? 0 : states.top();
27 void Application::UpdateState() {
29 while (!stateChanges.empty()) {
30 switch (stateChanges.front().type) {
31 case StateCommand::PUSH:
32 RealPushState(stateChanges.front().state);
34 case StateCommand::POP:
37 case StateCommand::CHANGE:
38 RealChangeState(stateChanges.front().state);
46 void Application::ChangeState(State *s) {
51 cmd.type = StateCommand::CHANGE;
53 stateChanges.push(cmd);
57 void Application::PushState(State *s) {
62 cmd.type = StateCommand::PUSH;
64 stateChanges.push(cmd);
68 void Application::PopState() {
73 cmd.type = StateCommand::POP;
75 stateChanges.push(cmd);
79 void Application::RealChangeState(State *s) {
80 if (!states.empty()) {
81 states.top()->PauseState(screen.Screen());
82 states.top()->ExitState(*this, screen.Screen());
86 s->EnterState(*this, screen.Screen());
87 s->ResumeState(screen.Screen());
90 void Application::RealPushState(State *s) {
91 if (!states.empty()) {
92 states.top()->PauseState(screen.Screen());
95 s->EnterState(*this, screen.Screen());
96 s->ResumeState(screen.Screen());
99 void Application::RealPopState() {
100 if (states.empty()) return;
101 states.top()->PauseState(screen.Screen());
102 states.top()->ExitState(*this, screen.Screen());
105 if (!states.empty()) {
106 states.top()->ResumeState(screen.Screen());
110 void Application::Quit() {
114 void Application::PopAllStates() {
115 while (!states.empty()) {
116 states.top()->PauseState(screen.Screen());
117 states.top()->ExitState(*this, screen.Screen());
124 void Application::Run() {
125 while (CurrentState()) {
130 void Application::Loop() {
131 Uint32 now(SDL_GetTicks());
132 Uint32 deltaT(now - last);
133 GlobalTimers().Update(deltaT);
134 if (deltaT > 30) deltaT = 30;
136 if (CurrentState()) {
137 CurrentState()->GraphicsTimers().Update(deltaT);
140 if (!StateChangePending()) {
150 void Application::HandleEvents() {
151 if (!CurrentState()) return;
152 input.ResetInteractiveState();
154 while (SDL_PollEvent(&event)) {
155 switch (event.type) {
159 case SDL_VIDEORESIZE:
160 screen.Resize(event.resize.w, event.resize.h);
161 CurrentState()->Resize(event.resize.w, event.resize.h);
165 input.HandleKeyboardEvent(event.key);
172 if (CurrentState()) CurrentState()->HandleEvents(input);
175 void Application::UpdateWorld(Uint32 deltaT) {
176 if (!CurrentState()) return;
177 for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) {
178 CurrentState()->PhysicsTimers().Update(0.001f);
179 CurrentState()->UpdateWorld(0.001f);
183 void Application::Render(void) {
184 if (!CurrentState()) return;
185 CurrentState()->Render(screen.Screen());