]> git.localhorst.tv Git - space.git/blobdiff - src/app/Application.cpp
move to SDL2
[space.git] / src / app / Application.cpp
index 20c371887c153e9a48edbc474926e4c532442751..488b952243b94880e12886185d0b612080466b81 100644 (file)
@@ -1,17 +1,16 @@
 #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)
+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())
 , last(SDL_GetTicks())
 , running(false) {
        controlled = univ.AddEntity(Entity());
@@ -33,7 +32,7 @@ void Application::Loop(int delta) {
        HandleEvents();
        Update(delta);
        Render();
-       screen.Flip();
+       canvas.Present();
 }
 
 
@@ -44,15 +43,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
@@ -153,18 +157,25 @@ 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);
+       canvas.SetColor(background);
+       canvas.Fill();
 
-       Fill(dst, background);
-       Grid2(dst, begin, end, cam.ToScale(univ.areaSize), univ.secSize, secGrid, univGrid);
-       Cross(dst, cam.ToScreen(focus.Pos()), 15, focusColor);
+       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 Entity &e : univ.Entities()) {
-               Cross(dst, cam.ToScreen((e.area * univ.areaSize) + Vector<int>(e.pos)), 10, entityColor);
+               canvas.Cross(
+                       cam.ToScreen(Vector<float>(e.area * univ.areaSize) + e.pos),
+                       10);
        }
 }