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