1 #include "Application.h"
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
9 Application::Application(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 , controlled(univ.AddShip(Ship()))
18 Color(0xFF, 0xFF, 0xFF),
19 Color(0x00, 0x00, 0x00),
20 Color(0x00, 0xFF, 0x00),
21 Color(0xFF, 0x00, 0x00))
25 Color(0xFF, 0xFF, 0xFF),
26 Color(0x00, 0x00, 0x00),
27 Color(0x33, 0x33, 0xFF),
28 Color(0x00, 0x00, 0xFF))
29 , autopilot(*controlled, focus.Pos())
31 , last(SDL_GetTicks())
38 void Application::Run() {
41 Uint32 now = SDL_GetTicks();
42 int delta = now - last;
49 void Application::Loop(int delta) {
64 void Application::HandleEvents() {
66 while (SDL_PollEvent(&event)) {
72 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
73 cam.Resize(event.window.data1, event.window.data2);
77 if (!event.key.repeat) {
82 if (!event.key.repeat) {
93 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
94 switch (e.keysym.sym) {
126 apEnabled = !apEnabled;
136 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
137 switch (e.keysym.sym) {
174 void Application::Update(int dt) {
175 const float delta = dt / 1e3;
177 autopilot.Update(delta);
179 controlled->rotThrottle = control.x;
180 controlled->linThrottle = -control.y;
184 focus.SetSpeed(500 / cam.Zoom());
189 void Application::Render() {
190 constexpr Color background(0x00, 0x00, 0x00);
191 constexpr Color univGrid(0xEE, 0xEE, 0xEE);
192 constexpr Color secGrid(0x77, 0x77, 0x77);
193 constexpr Color entityColor(0x00, 0xAA, 0xAA);
194 constexpr Color focusColor(0xFA, 0xFA, 0x00);
196 canvas.SetColor(background);
200 cam.ToScreen(Vector<float>(0, 0)),
201 cam.ToScale(univ.size * univ.secSize * univ.areaSize),
202 cam.ToScale(univ.areaSize),
207 canvas.SetColor(focusColor);
208 canvas.Cross(cam.ToScreen(focus.Pos()), 15);
210 canvas.SetColor(entityColor);
211 for (const Ship &s : univ.Ships()) {
212 const Vector<float> direction = s.Dir();
213 const Vector<int> position = cam.ToScreen(s.pos);
214 const Vector<int> nose = position + Vector<int>(direction * 15.0f);
215 const Vector<int> left = position + Vector<int>((Rotate90(direction) * 8.0f) - (direction * 4.0f));
216 const Vector<int> right = position + Vector<int>((Rotate270(direction) * 8.0f) - (direction * 4.0f));
217 canvas.Line(position, nose);
218 canvas.Quad(nose, left, position, right);
221 autopilot.Render(canvas, cam);
223 linGauge.Render(canvas, controlled->linThrottle);
224 rotGauge.Render(canvas, controlled->rotThrottle);