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