]> git.localhorst.tv Git - space.git/blob - src/app/Application.cpp
force based movement
[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.AddShip(Ship());
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->rotThrottle = control.x;
146         controlled->linThrottle = -control.y;
147         cam.Update(delta);
148         univ.Update(delta);
149         focus.SetSpeed(500 / cam.Zoom());
150         focus.Update(delta);
151 }
152
153
154 void Application::Render() {
155         constexpr Color background(0x00, 0x00, 0x00);
156         constexpr Color univGrid(0xEE, 0xEE, 0xEE);
157         constexpr Color secGrid(0x77, 0x77, 0x77);
158         constexpr Color entityColor(0x00, 0xAA, 0xAA);
159         constexpr Color focusColor(0xFA, 0xFA, 0x00);
160
161         canvas.SetColor(background);
162         canvas.Fill();
163
164         canvas.Grid2(
165                 cam.ToScreen(Vector<float>(0, 0)),
166                 cam.ToScale(univ.size * univ.secSize * univ.areaSize),
167                 cam.ToScale(univ.areaSize),
168                 univ.secSize,
169                 secGrid,
170                 univGrid);
171
172         canvas.SetColor(focusColor);
173         canvas.Cross(cam.ToScreen(focus.Pos()), 15);
174
175         canvas.SetColor(entityColor);
176         for (const Ship &s : univ.Ships()) {
177                 const Vector<float> direction = s.Dir();
178                 const Vector<int> position = cam.ToScreen(Vector<float>(s.area * univ.areaSize) + s.pos);
179                 const Vector<int> nose = position + Vector<int>(direction * 15.0f);
180                 const Vector<int> left = position + Vector<int>((Rotate90(direction) * 8.0f) - (direction * 4.0f));
181                 const Vector<int> right = position + Vector<int>((Rotate270(direction) * 8.0f) - (direction * 4.0f));
182                 canvas.Line(position, nose);
183                 canvas.Quad(nose, left, position, right);
184         }
185 }
186
187 }