4 * Created on: Apr 8, 2012
8 #include "Application.h"
16 Application::Application(sdl::InitScreen *screen, State *initialState)
19 , last(SDL_GetTicks())
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() {
35 void Application::UpdateState() {
40 while (!toPush.empty()) {
41 State *s(toPush.front());
47 void Application::ChangeState(State *s) {
52 void Application::PushState(State *s) {
56 void Application::RealPushState(State *s) {
58 s->EnterState(*this, screen->Screen());
61 void Application::PopState() {
65 void Application::RealPopState() {
66 if (states.empty()) return;
67 states.top()->ExitState();
72 void Application::Quit() {
76 void Application::PopAllStates() {
77 while (!states.empty()) {
83 void Application::Run() {
84 while (CurrentState()) {
89 void Application::Loop() {
90 Uint32 now(SDL_GetTicks());
91 Uint32 deltaT(now - last);
92 if (deltaT > 34) deltaT = 34;
103 void Application::HandleEvents() {
104 if (!CurrentState()) return;
105 input.ResetInteractiveState();
107 while (SDL_PollEvent(&event)) {
108 switch (event.type) {
112 case SDL_VIDEORESIZE:
113 screen->Resize(event.resize.w, event.resize.h);
114 CurrentState()->Resize(event.resize.w, event.resize.h);
118 input.HandleKeyboardEvent(event.key);
125 CurrentState()->HandleInput(input);
128 void Application::UpdateWorld(Uint32 deltaT) {
129 if (!CurrentState()) return;
130 for (Uint32 i(0); i < deltaT; ++i) {
131 CurrentState()->UpdateWorld(0.001f);
135 void Application::Render(void) {
136 if (!CurrentState()) return;
137 CurrentState()->Render(screen->Screen());