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 , last(SDL_GetTicks())
16 controlled = univ.AddShip(Ship());
20 void Application::Run() {
23 Uint32 now = SDL_GetTicks();
24 int delta = now - last;
31 void Application::Loop(int delta) {
39 void Application::HandleEvents() {
41 while (SDL_PollEvent(&event)) {
47 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
48 cam.Resize(event.window.data1, event.window.data2);
52 if (!event.key.repeat) {
57 if (!event.key.repeat) {
68 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
69 switch (e.keysym.sym) {
105 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
106 switch (e.keysym.sym) {
143 void Application::Update(int dt) {
144 const float delta = dt / 1e3;
145 controlled->rotThrottle = control.x;
146 controlled->linThrottle = -control.y;
149 focus.SetSpeed(500 / cam.Zoom());
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);
161 canvas.SetColor(background);
165 cam.ToScreen(Vector<float>(0, 0)),
166 cam.ToScale(univ.size * univ.secSize * univ.areaSize),
167 cam.ToScale(univ.areaSize),
172 canvas.SetColor(focusColor);
173 canvas.Cross(cam.ToScreen(focus.Pos()), 15);
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);