]> git.localhorst.tv Git - space.git/blobdiff - src/app/Application.cpp
added autopilot that sucks
[space.git] / src / app / Application.cpp
index d18c757eca9141e62b18ea112844bc49eb049cc4..e3c6a2b878bc1bb39c4294bd632192d8ebca40df 100644 (file)
@@ -1,20 +1,23 @@
 #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(Vector<int>(10, 10), Vector<int>(10, 10), Vector<int>(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()))
+, autopilot(*controlled, focus.Pos())
+, apEnabled(false)
 , last(SDL_GetTicks())
-, running(false) {
-       controlled = univ.AddEntity(Entity());
+, running(false)
+, paused(false) {
+
 }
 
 
@@ -31,9 +34,11 @@ void Application::Run() {
 
 void Application::Loop(int delta) {
        HandleEvents();
-       Update(delta);
+       if (!paused) {
+               Update(delta);
+       }
        Render();
-       screen.Flip();
+       canvas.Present();
 }
 
 
@@ -44,15 +49,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
@@ -93,6 +103,12 @@ void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
                case SDLK_x:
                        cam.StartShrink();
                        break;
+               case SDLK_c:
+                       apEnabled = !apEnabled;
+                       break;
+               case SDLK_p:
+                       paused = !paused;
+                       break;
                default:
                        break;
        }
@@ -138,9 +154,15 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
 
 void Application::Update(int dt) {
        const float delta = dt / 1e3;
-       controlled->acc = Vector<float>(control * 10);
+       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);
 }
 
@@ -152,19 +174,32 @@ void Application::Render() {
        constexpr Color entityColor(0x00, 0xAA, 0xAA);
        constexpr Color focusColor(0xFA, 0xFA, 0x00);
 
-       SDL_Surface *dst = screen.Screen();
-       const Vector<int> begin = cam.ToScreen(Vector<float>(0, 0));
-       const Vector<int> end =
-               cam.ToScreen((univ.size * univ.secSize * univ.areaSize))
-                       + Vector<int>(1, 1);
-
-       Fill(dst, background);
-       Grid2(dst, begin, end, cam.ToScale(univ.areaSize), univ.secSize, secGrid, univGrid);
-       Cross(dst, cam.ToScreen(focus.Pos()), 15, focusColor);
-
-       for (const Entity &e : univ.Entities()) {
-               Cross(dst, cam.ToScreen((e.area * univ.areaSize) + Vector<int>(e.pos)), 10, entityColor);
+       canvas.SetColor(background);
+       canvas.Fill();
+
+       canvas.Grid2(
+               cam.ToScreen(Vector<float>(0, 0)),
+               cam.ToScale(univ.size * univ.secSize * univ.areaSize),
+               cam.ToScale(univ.areaSize),
+               univ.secSize,
+               secGrid,
+               univGrid);
+
+       canvas.SetColor(focusColor);
+       canvas.Cross(cam.ToScreen(focus.Pos()), 15);
+
+       canvas.SetColor(entityColor);
+       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);
        }
+
+       autopilot.Render(canvas, cam);
 }
 
 }