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()))
15 , autopilot(*controlled, focus.Pos())
17 , last(SDL_GetTicks())
24 void Application::Run() {
27 Uint32 now = SDL_GetTicks();
28 int delta = now - last;
35 void Application::Loop(int delta) {
45 void Application::HandleEvents() {
47 while (SDL_PollEvent(&event)) {
53 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
54 cam.Resize(event.window.data1, event.window.data2);
58 if (!event.key.repeat) {
63 if (!event.key.repeat) {
74 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
75 switch (e.keysym.sym) {
107 apEnabled = !apEnabled;
117 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
118 switch (e.keysym.sym) {
155 void Application::Update(int dt) {
156 const float delta = dt / 1e3;
158 autopilot.Update(delta);
160 controlled->rotThrottle = control.x;
161 controlled->linThrottle = -control.y;
165 focus.SetSpeed(500 / cam.Zoom());
170 void Application::Render() {
171 constexpr Color background(0x00, 0x00, 0x00);
172 constexpr Color univGrid(0xEE, 0xEE, 0xEE);
173 constexpr Color secGrid(0x77, 0x77, 0x77);
174 constexpr Color entityColor(0x00, 0xAA, 0xAA);
175 constexpr Color focusColor(0xFA, 0xFA, 0x00);
177 canvas.SetColor(background);
181 cam.ToScreen(Vector<float>(0, 0)),
182 cam.ToScale(univ.size * univ.secSize * univ.areaSize),
183 cam.ToScale(univ.areaSize),
188 canvas.SetColor(focusColor);
189 canvas.Cross(cam.ToScreen(focus.Pos()), 15);
191 canvas.SetColor(entityColor);
192 for (const Ship &s : univ.Ships()) {
193 const Vector<float> direction = s.Dir();
194 const Vector<int> position = cam.ToScreen(s.pos);
195 const Vector<int> nose = position + Vector<int>(direction * 15.0f);
196 const Vector<int> left = position + Vector<int>((Rotate90(direction) * 8.0f) - (direction * 4.0f));
197 const Vector<int> right = position + Vector<int>((Rotate270(direction) * 8.0f) - (direction * 4.0f));
198 canvas.Line(position, nose);
199 canvas.Quad(nose, left, position, right);
202 autopilot.Render(canvas, cam);