]> git.localhorst.tv Git - space.git/blobdiff - src/app/Application.cpp
split render function
[space.git] / src / app / Application.cpp
index 247819c41bd0088d9f1a51e9b3dc7745954ff17c..6a217666015bb228f9588e1214dd6e92a4063a2f 100644 (file)
@@ -1,20 +1,39 @@
 #include "Application.h"
 
+#include "../graphics/Canvas.h"
 #include "../graphics/Color.h"
-#include "../graphics/primitive.h"
-#include "../sdl/InitScreen.h"
 
 
 namespace space {
 
-Application::Application(InitScreen &s)
-: screen(s)
-, univ(10, 10, 10, 10, 10)
+Application::Application(Canvas &c)
+: canvas(c)
+, univ(Vector<int>(10, 10), Vector<int>(10, 10), Vector<int>(10, 10))
 , focus(Vector<float>(500, 500), 500)
-, cam(800, 800, focus.Pos())
+, cam(c.Size(), focus.Pos())
+, controlled(univ.AddShip(Ship()))
+, linGauge(
+       Vector<int>(15, 100),
+       Vector<int>(10, 10),
+       Color(0xFF, 0xFF, 0xFF),
+       Color(0x00, 0x00, 0x00),
+       Color(0x00, 0xFF, 0x00),
+       Color(0xFF, 0x00, 0x00))
+, rotGauge(
+       Vector<int>(15, 100),
+       Vector<int>(27, 10),
+       Color(0xFF, 0xFF, 0xFF),
+       Color(0x00, 0x00, 0x00),
+       Color(0x33, 0x33, 0xFF),
+       Color(0x00, 0x00, 0xFF))
+, autopilot(*controlled, focus.Pos())
+, apEnabled(false)
 , last(SDL_GetTicks())
-, running(false) {
-
+, running(false)
+, paused(false) {
+       univ.AddResource(Resource("Liquid Hydrogen", 70));
+       univ.AddResource(Resource("Liquid Qxygen", 1141));
+       univ.AddResource(Resource("Water", 999));
 }
 
 
@@ -31,9 +50,16 @@ void Application::Run() {
 
 void Application::Loop(int delta) {
        HandleEvents();
-       Update(delta);
+       if (delta == 0) {
+               SDL_Delay(1);
+               return;
+       }
+
+       if (!paused) {
+               Update(delta);
+       }
        Render();
-       screen.Flip();
+       canvas.Present();
 }
 
 
@@ -44,15 +70,20 @@ void Application::HandleEvents() {
                        case SDL_QUIT:
                                running = false;
                                break;
-                       case SDL_VIDEORESIZE:
-                               screen.Resize(event.resize.w, event.resize.h);
-                               cam.Resize(event.resize.w, event.resize.h);
+                       case SDL_WINDOWEVENT:
+                               if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
+                                       cam.Resize(event.window.data1, event.window.data2);
+                               }
                                break;
                        case SDL_KEYDOWN:
-                               OnKeyDown(event.key);
+                               if (!event.key.repeat) {
+                                       OnKeyDown(event.key);
+                               }
                                break;
                        case SDL_KEYUP:
-                               OnKeyUp(event.key);
+                               if (!event.key.repeat) {
+                                       OnKeyUp(event.key);
+                               }
                                break;
                        default:
                                // skip event
@@ -75,6 +106,30 @@ void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
                case SDLK_RIGHT:
                        focus.MoveRight();
                        break;
+               case SDLK_w:
+                       control.y -= 1;
+                       break;
+               case SDLK_s:
+                       control.y += 1;
+                       break;
+               case SDLK_a:
+                       control.x -= 1;
+                       break;
+               case SDLK_d:
+                       control.x += 1;
+                       break;
+               case SDLK_z:
+                       cam.StartZoom();
+                       break;
+               case SDLK_x:
+                       cam.StartShrink();
+                       break;
+               case SDLK_c:
+                       apEnabled = !apEnabled;
+                       break;
+               case SDLK_p:
+                       paused = !paused;
+                       break;
                default:
                        break;
        }
@@ -94,6 +149,24 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
                case SDLK_RIGHT:
                        focus.StopRight();
                        break;
+               case SDLK_w:
+                       control.y += 1;
+                       break;
+               case SDLK_s:
+                       control.y -= 1;
+                       break;
+               case SDLK_a:
+                       control.x += 1;
+                       break;
+               case SDLK_d:
+                       control.x -= 1;
+                       break;
+               case SDLK_z:
+                       cam.StopZoom();
+                       break;
+               case SDLK_x:
+                       cam.StopShrink();
+                       break;
                default:
                        break;
        }
@@ -101,26 +174,72 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
 
 
 void Application::Update(int dt) {
-       focus.Update(dt / 1e3);
+       const float delta = dt / 1e3;
+       if (apEnabled) {
+               autopilot.Update(delta);
+       } else {
+               controlled->rotThrottle = control.x;
+               controlled->linThrottle = -control.y;
+       }
+       cam.Update(delta);
+       univ.Update(delta);
+       focus.SetSpeed(500 / cam.Zoom());
+       focus.Update(delta);
 }
 
 
 void Application::Render() {
+       RenderBackground();
+       RenderGrid();
+       RenderShips();
+       RenderUI();
+}
+
+void Application::RenderBackground() {
        constexpr Color background(0x00, 0x00, 0x00);
-       constexpr Color univGrid(0xFF, 0xFF, 0xFF);
-       constexpr Color sectGrid(0xAA, 0xAA, 0xAA);
 
-       constexpr Vector<int> areaSize(10, 10);
-       constexpr Vector<int> sectSize(areaSize * 10);
-       constexpr Vector<int> univSize(sectSize * 10);
+       canvas.SetColor(background);
+       canvas.Fill();
+}
+
+void Application::RenderGrid() {
+       constexpr Color univGrid(0xEE, 0xEE, 0xEE);
+       constexpr Color secGrid(0x77, 0x77, 0x77);
+
+       canvas.Grid2(
+               cam.ToScreen(Vector<float>(0, 0)),
+               cam.ToScale(univ.size * univ.secSize * univ.areaSize),
+               cam.ToScale(univ.areaSize),
+               univ.secSize,
+               secGrid,
+               univGrid);
+}
+
+void Application::RenderShips() {
+       constexpr Color shipColor(0x00, 0xAA, 0xAA);
+
+       canvas.SetColor(shipColor);
+       for (const Ship &s : univ.Ships()) {
+               const Vector<float> direction = s.Dir();
+               const Vector<int> position = cam.ToScreen(s.pos);
+               const Vector<int> nose = position + Vector<int>(direction * 15.0f);
+               const Vector<int> left = position + Vector<int>((Rotate90(direction) * 8.0f) - (direction * 4.0f));
+               const Vector<int> right = position + Vector<int>((Rotate270(direction) * 8.0f) - (direction * 4.0f));
+               canvas.Line(position, nose);
+               canvas.Quad(nose, left, position, right);
+       }
+}
+
+void Application::RenderUI() {
+       constexpr Color focusColor(0xFA, 0xFA, 0x00);
+
+       autopilot.Render(canvas, cam);
 
-       SDL_Surface *dst = screen.Screen();
-       Vector<int> offset = cam.Offset();
+       linGauge.Render(canvas, controlled->linThrottle);
+       rotGauge.Render(canvas, controlled->rotThrottle);
 
-       Fill(dst, background);
-       Grid(dst, offset, offset + univSize + Vector<int>(1, 1), areaSize, sectGrid);
-       Grid(dst, offset, offset + univSize + Vector<int>(1, 1), sectSize, univGrid);
-       Cross(dst, offset + Vector<int>(focus.Pos()), 15, Color(0xFF, 0xFF, 0x00));
+       canvas.SetColor(focusColor);
+       canvas.Cross(cam.ToScreen(focus.Pos()), 15);
 }
 
 }