]> git.localhorst.tv Git - orbi.git/blob - src/app/Application.cpp
initial collision tests
[orbi.git] / src / app / Application.cpp
1 #include "Application.h"
2
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
5 #include "../world/Entity.h"
6 #include "../world/Tileset.h"
7 #include "../world/World.h"
8
9
10 namespace orbi {
11
12 Application::Application(Canvas &c, World &w, Tileset &t)
13 : canvas(c)
14 , world(w)
15 , tiles(t)
16 , focus(Vector<float>(5, 5), 2)
17 , cam(c.Size(), focus.Pos())
18 , last(SDL_GetTicks())
19 , running(false)
20 , paused(false) {
21         cam.SetScale(tiles.TileSize());
22 }
23
24
25 void Application::Run() {
26         running = true;
27         while (running) {
28                 Uint32 now = SDL_GetTicks();
29                 int delta = now - last;
30                 Loop(delta);
31                 last = now;
32         }
33 }
34
35
36 void Application::Loop(int delta) {
37         HandleEvents();
38
39         if (delta == 0) {
40                 SDL_Delay(1);
41                 return;
42         } else if (delta > 30) {
43                 delta = 30;
44         }
45
46         if (!paused) {
47                 Update(delta);
48         }
49
50         Render();
51
52         canvas.Present();
53
54 }
55
56
57 void Application::HandleEvents() {
58         SDL_Event event;
59         while (SDL_PollEvent(&event)) {
60                 switch (event.type) {
61                         case SDL_QUIT:
62                                 running = false;
63                                 break;
64                         case SDL_WINDOWEVENT:
65                                 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
66                                         cam.Resize(event.window.data1, event.window.data2);
67                                 }
68                                 break;
69                         case SDL_KEYDOWN:
70                                 if (!event.key.repeat) {
71                                         OnKeyDown(event.key);
72                                 }
73                                 break;
74                         case SDL_KEYUP:
75                                 if (!event.key.repeat) {
76                                         OnKeyUp(event.key);
77                                 }
78                                 break;
79                         default:
80                                 // skip event
81                                 break;
82                 }
83         }
84 }
85
86 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
87         switch (e.keysym.sym) {
88                 case SDLK_UP:
89                         focus.MoveUp();
90                         break;
91                 case SDLK_DOWN:
92                         focus.MoveDown();
93                         break;
94                 case SDLK_LEFT:
95                         focus.MoveLeft();
96                         break;
97                 case SDLK_RIGHT:
98                         focus.MoveRight();
99                         break;
100                 case SDLK_p:
101                         paused = !paused;
102                         break;
103                 default:
104                         break;
105         }
106 }
107
108 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
109         switch (e.keysym.sym) {
110                 case SDLK_UP:
111                         focus.StopUp();
112                         break;
113                 case SDLK_DOWN:
114                         focus.StopDown();
115                         break;
116                 case SDLK_LEFT:
117                         focus.StopLeft();
118                         break;
119                 case SDLK_RIGHT:
120                         focus.StopRight();
121                         break;
122                 default:
123                         break;
124         }
125 }
126
127
128 void Application::Update(int dt) {
129         const float delta = dt / 1e3;
130         cam.Update(delta);
131         world.Update(dt);
132         focus.Update(delta);
133 }
134
135
136 void Application::Render() {
137         RenderBackground();
138         RenderWorld();
139         RenderEntities();
140         RenderUI();
141 }
142
143 void Application::RenderBackground() {
144         constexpr Color background(0x00, 0x00, 0x00);
145
146         canvas.SetColor(background);
147         canvas.Fill();
148 }
149
150 void Application::RenderWorld() {
151         const Vector<int> begin(0, 0);
152         const Vector<int> end(world.Size());
153
154         for (Vector<int> pos(begin); pos.y < end.y; ++pos.y) {
155                 for (pos.x = 0; pos.x < end.x; ++pos.x) {
156                         tiles.DrawFG(canvas, cam.ToScreen(pos), world, pos);
157                 }
158         }
159 }
160
161 void Application::RenderEntities() {
162         constexpr Color entityColor(0x00, 0xFA, 0x00);
163         canvas.SetColor(entityColor);
164
165         for (const Entity &e : world.Entities()) {
166                 const Vector<float> pos(e.Bounds().Left(), e.Bounds().Top());
167                 const Vector<float> size(e.Bounds().Size());
168                 canvas.OutlineRect(cam.ToScreen(pos), cam.ToScale(size));
169         }
170 }
171
172 void Application::RenderUI() {
173         constexpr Color outlineColor(0x00, 0x00, 0xFA);
174         constexpr Color focusColor(0xFA, 0xFA, 0x00);
175
176         canvas.SetColor(outlineColor);
177         canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
178
179         canvas.SetColor(focusColor);
180         canvas.Cross(cam.ToScreen(focus.Pos()), 15);
181 }
182
183 }