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())
34 univ.AddResource(Resource("Liquid Hydrogen", 70));
35 univ.AddResource(Resource("Liquid Qxygen", 1141));
36 univ.AddResource(Resource("Water", 999));
40 void Application::Run() {
43 Uint32 now = SDL_GetTicks();
44 int delta = now - last;
51 void Application::Loop(int delta) {
66 void Application::HandleEvents() {
68 while (SDL_PollEvent(&event)) {
74 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
75 cam.Resize(event.window.data1, event.window.data2);
79 if (!event.key.repeat) {
84 if (!event.key.repeat) {
95 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
96 switch (e.keysym.sym) {
128 apEnabled = !apEnabled;
138 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
139 switch (e.keysym.sym) {
176 void Application::Update(int dt) {
177 const float delta = dt / 1e3;
179 autopilot.Update(delta);
181 controlled->rotThrottle = control.x;
182 controlled->linThrottle = -control.y;
186 focus.SetSpeed(500 / cam.Zoom());
191 void Application::Render() {
198 void Application::RenderBackground() {
199 constexpr Color background(0x00, 0x00, 0x00);
201 canvas.SetColor(background);
205 void Application::RenderGrid() {
206 constexpr Color univGrid(0xEE, 0xEE, 0xEE);
207 constexpr Color secGrid(0x77, 0x77, 0x77);
210 cam.ToScreen(Vector<float>(0, 0)),
211 cam.ToScale(univ.size * univ.secSize * univ.areaSize),
212 cam.ToScale(univ.areaSize),
218 void Application::RenderShips() {
219 constexpr Color shipColor(0x00, 0xAA, 0xAA);
221 canvas.SetColor(shipColor);
222 for (const Ship &s : univ.Ships()) {
223 const Vector<float> direction = s.Dir();
224 const Vector<int> position = cam.ToScreen(s.pos);
225 const Vector<int> nose = position + Vector<int>(direction * 15.0f);
226 const Vector<int> left = position + Vector<int>((Rotate90(direction) * 8.0f) - (direction * 4.0f));
227 const Vector<int> right = position + Vector<int>((Rotate270(direction) * 8.0f) - (direction * 4.0f));
228 canvas.Line(position, nose);
229 canvas.Quad(nose, left, position, right);
233 void Application::RenderUI() {
234 constexpr Color focusColor(0xFA, 0xFA, 0x00);
236 autopilot.Render(canvas, cam);
238 linGauge.Render(canvas, controlled->linThrottle);
239 rotGauge.Render(canvas, controlled->rotThrottle);
241 canvas.SetColor(focusColor);
242 canvas.Cross(cam.ToScreen(focus.Pos()), 15);