]> git.localhorst.tv Git - blobs.git/blob - src/app/app.cpp
more stolen stuff
[blobs.git] / src / app / app.cpp
1 #include "Application.hpp"
2 #include "State.hpp"
3
4 #include <SDL.h>
5
6
7 namespace blobs {
8 namespace app {
9
10 Application::Application()
11 : states() {
12 }
13
14 Application::~Application() {
15 }
16
17
18 void Application::PushState(State *s) {
19         if (!states.empty()) {
20                 states.top()->OnPause();
21         }
22         states.emplace(s);
23         ++s->ref_count;
24         if (s->ref_count == 1) {
25                 s->OnEnter();
26         }
27         s->OnResume();
28 }
29
30 State *Application::PopState() {
31         State *s = states.top();
32         states.pop();
33         s->OnPause();
34         s->OnExit();
35         if (!states.empty()) {
36                 states.top()->OnResume();
37         }
38         return s;
39 }
40
41 State *Application::SwitchState(State *s_new) {
42         State *s_old = states.top();
43         states.top() = s_new;
44         --s_old->ref_count;
45         ++s_new->ref_count;
46         s_old->OnPause();
47         if (s_old->ref_count == 0) {
48                 s_old->OnExit();
49         }
50         if (s_new->ref_count == 1) {
51                 s_new->OnEnter();
52         }
53         s_new->OnResume();
54         return s_old;
55 }
56
57 State &Application::GetState() {
58         return *states.top();
59 }
60
61 bool Application::HasState() const noexcept {
62         return !states.empty();
63 }
64
65
66 void Application::Run() {
67         Uint32 last = SDL_GetTicks();
68         while (HasState()) {
69                 Uint32 now = SDL_GetTicks();
70                 int delta = now - last;
71                 Loop(delta);
72                 last = now;
73         }
74 }
75
76 void Application::Loop(int dt) {
77         HandleEvents();
78         if (!HasState()) return;
79         GetState().Update(dt);
80         if (!HasState()) return;
81         GetState().Render();
82 }
83
84 void Application::HandleEvents() {
85         SDL_Event event;
86         while (HasState() && SDL_PollEvent(&event)) {
87                 GetState().Handle(event);
88         }
89 }
90
91 void State::Handle(const SDL_Event &event) {
92         switch (event.type) {
93                 case SDL_WINDOWEVENT:
94                         Handle(event.window);
95                         break;
96                 default:
97                         OnEvent(event);
98                         break;
99         }
100 }
101
102 void State::Handle(const SDL_WindowEvent &event) {
103         switch (event.event) {
104                 case SDL_WINDOWEVENT_FOCUS_GAINED:
105                         OnFocus();
106                         break;
107                 case SDL_WINDOWEVENT_FOCUS_LOST:
108                         OnBlur();
109                         break;
110                 case SDL_WINDOWEVENT_RESIZED:
111                         //env.viewport.Resize(event.data1, event.data2);
112                         OnResize();
113                         break;
114                 default:
115                         break;
116         }
117 }
118
119 void State::Update(int dt) {
120         OnUpdate(dt);
121 }
122
123 void State::Render() {
124         OnRender();
125 }
126
127 }
128 }