]> git.localhorst.tv Git - sdl-test8.git/blob - src/app/Application.cpp
added collision engine, more or less stole gameplay from sdl-test7
[sdl-test8.git] / src / app / Application.cpp
1 /*
2  * Application.cpp
3  *
4  *  Created on: Apr 8, 2012
5  *      Author: holy
6  */
7
8 #include "Application.h"
9
10 #include "State.h"
11
12 #include <cassert>
13
14 namespace app {
15
16 Application::Application(sdl::InitScreen *screen, State *initialState)
17 : screen(screen)
18 , states()
19 , timer()
20 , last(SDL_GetTicks()) {
21         assert(screen && "cannot create application without screen");
22         assert(initialState && "cannot create application without initial state");
23         RealPushState(initialState);
24 }
25
26 Application::~Application(void) {
27         PopAllStates();
28 }
29
30
31 State *Application::CurrentState(void) {
32         return states.top();
33 }
34
35 void Application::ChangeState(State *s) {
36         RealPopState();
37         RealPushState(s);
38 }
39
40 void Application::PushState(State *s) {
41         RealPushState(s);
42 }
43
44 void Application::RealPushState(State *s) {
45         states.push(s);
46         s->EnterState(this, screen->Screen());
47 }
48
49 void Application::PopState(void) {
50         RealPopState();
51 }
52
53 void Application::RealPopState(void) {
54         if (states.empty()) return;
55         states.top()->ExitState();
56         delete states.top();
57         states.pop();
58 }
59
60 void Application::Quit(void) {
61         PopAllStates();
62 }
63
64 void Application::PopAllStates(void) {
65         while (!states.empty()) {
66                 RealPopState();
67         }
68 }
69
70
71 void Application::Run(void) {
72         while (CurrentState()) {
73                 Loop();
74         }
75 }
76
77 void Application::Loop(void) {
78         Uint32 now(SDL_GetTicks());
79         Uint32 deltaT(now - last);
80         if (deltaT > 34) deltaT = 34;
81
82         HandleEvents();
83         UpdateWorld(deltaT);
84         Render();
85
86         last = now;
87 }
88
89
90 void Application::HandleEvents(void) {
91         if (!CurrentState()) return;
92         SDL_Event event;
93         while (SDL_PollEvent(&event)) {
94                 switch (event.type) {
95                         case SDL_QUIT:
96                                 PopAllStates();
97                                 break;
98                         default:
99                                 CurrentState()->HandleEvent(event);
100                                 break;
101                 }
102         }
103 }
104
105 void Application::UpdateWorld(Uint32 deltaT) {
106         if (!CurrentState()) return;
107         for (Uint32 i(0); i < deltaT; ++i) {
108                 timer.Update(0.001);
109                 CurrentState()->UpdateWorld(timer);
110         }
111 }
112
113 void Application::Render(void) {
114         if (!CurrentState()) return;
115         CurrentState()->Render(screen->Screen());
116         screen->Flip();
117 }
118
119 }