]> git.localhorst.tv Git - orbi.git/blobdiff - src/collision.cpp
some cleanup
[orbi.git] / src / collision.cpp
index af4234aa10e7613335fe9a1bbb1d5fe50178dd3b..4212507f8fca29749862b077ebeeee8cd8348ee3 100644 (file)
@@ -5,6 +5,7 @@
 #include "graphics/Vector.h"
 #include "graphics/Window.h"
 #include "world/AABB.h"
+#include "world/Collision.h"
 
 #include <SDL.h>
 #include <vector>
@@ -27,11 +28,6 @@ struct World {
        AABB controlled;
        vector<AABB> stationary;
 
-       struct Collision {
-               Vector<float> pos;
-               Vector<float> norm;
-               Vector<float> depth;
-       };
        vector<Collision> coll;
 
        World()
@@ -42,171 +38,167 @@ struct World {
        , stationary()
        { }
 
-};
-
-void key_down(const SDL_KeyboardEvent &e, World &world) {
-       switch (e.keysym.sym) {
-               case SDLK_UP:
-                       world.move.y -= 1;
-                       break;
-               case SDLK_DOWN:
-                       world.move.y += 1;
-                       break;
-               case SDLK_LEFT:
-                       world.move.x -= 1;
-                       break;
-               case SDLK_RIGHT:
-                       world.move.x += 1;
-                       break;
-               default:
-                       break;
-       }
-}
 
-void key_up(const SDL_KeyboardEvent &e, World &world) {
-       switch (e.keysym.sym) {
-               case SDLK_UP:
-                       world.move.y += 1;
-                       break;
-               case SDLK_DOWN:
-                       world.move.y -= 1;
-                       break;
-               case SDLK_LEFT:
-                       world.move.x += 1;
-                       break;
-               case SDLK_RIGHT:
-                       world.move.x -= 1;
-                       break;
-               default:
-                       break;
+       void key_down(const SDL_KeyboardEvent &e) {
+               switch (e.keysym.sym) {
+                       case SDLK_UP:
+                               move.y -= 1;
+                               break;
+                       case SDLK_DOWN:
+                               move.y += 1;
+                               break;
+                       case SDLK_LEFT:
+                               move.x -= 1;
+                               break;
+                       case SDLK_RIGHT:
+                               move.x += 1;
+                               break;
+                       default:
+                               break;
+               }
        }
-}
 
-void handle(World &world) {
-       SDL_Event event;
-       while (SDL_PollEvent(&event)) {
-               switch (event.type) {
-                       case SDL_QUIT:
-                               world.alive = false;
+       void key_up(const SDL_KeyboardEvent &e) {
+               switch (e.keysym.sym) {
+                       case SDLK_UP:
+                               move.y += 1;
+                               break;
+                       case SDLK_DOWN:
+                               move.y -= 1;
                                break;
-                       case SDL_WINDOWEVENT:
-                               if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
-                                       world.cam.Resize(event.window.data1, event.window.data2);
-                               }
+                       case SDLK_LEFT:
+                               move.x += 1;
                                break;
-                       case SDL_KEYDOWN:
-                               if (!event.key.repeat) {
-                                       key_down(event.key, world);
-                               }
+                       case SDLK_RIGHT:
+                               move.x -= 1;
                                break;
-                       case SDL_KEYUP:
-                               if (!event.key.repeat) {
-                                       key_up(event.key, world);
-                               }
+                       default:
                                break;
                }
        }
-}
 
-void update(int dt, World &world) {
-       const Vector<float> speed { 5, 5 };
+       void handle() {
+               SDL_Event event;
+               while (SDL_PollEvent(&event)) {
+                       switch (event.type) {
+                               case SDL_QUIT:
+                                       alive = false;
+                                       break;
+                               case SDL_WINDOWEVENT:
+                                       if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
+                                               cam.Resize(event.window.data1, event.window.data2);
+                                       }
+                                       break;
+                               case SDL_KEYDOWN:
+                                       if (!event.key.repeat) {
+                                               key_down(event.key);
+                                       }
+                                       break;
+                               case SDL_KEYUP:
+                                       if (!event.key.repeat) {
+                                               key_up(event.key);
+                                       }
+                                       break;
+                       }
+               }
+       }
+
+       void update(int dt) {
+               const Vector<float> speed { 5, 5 };
 
-       const float delta = dt / 1e3;
+               const float delta = dt / 1e3;
 
-       world.controlled.Move(Vector<float>(world.move) * speed * delta);
-       world.focus = world.controlled.Center();
+               controlled.Move(Vector<float>(move) * speed * delta);
+               focus = controlled.Center();
 
-       world.coll.clear();
-       for (const AABB &e : world.stationary) {
-               World::Collision coll;
-               if (world.controlled.Intersects(
-                               e,
-                               coll.pos,
-                               coll.norm,
-                               coll.depth)) {
-                       world.coll.push_back(coll);
+               coll.clear();
+               for (const AABB &e : stationary) {
+                       Collision c;
+                       if (controlled.Intersects(e, c)) {
+                               coll.push_back(c);
+                       }
                }
        }
-}
 
-void render(Canvas &canvas, const World &world) {
-       constexpr Color background(0x00, 0x00, 0x00);
-       constexpr Color outlineColor(0x00, 0x00, 0xFA);
-       constexpr Color controlledColor(0xFA, 0xFA, 0x00);
-       constexpr Color entityColor(0x00, 0xFA, 0x00);
-       constexpr Color collisionColor(0xFA, 0x00, 0x00);
-       constexpr Color normalColor(0xFA, 0x00, 0x00);
-
-       canvas.SetColor(background);
-       canvas.Fill();
-
-       canvas.SetColor(outlineColor);
-       canvas.Grid(
-               world.cam.ToScreen(Vector<int>(0, 0)),
-               world.cam.ToScale(Vector<float>(10, 10)),
-               world.cam.ToScale(Vector<float>(1, 1)));
-
-       canvas.SetColor(entityColor);
-       for (const AABB &e : world.stationary) {
-               canvas.OutlineRect(
-                       world.cam.ToScreen(Vector<float>(e.Left(), e.Top())),
-                       world.cam.ToScale(e.Size()));
-       }
+       void render(Canvas &canvas) const {
+               constexpr Color background(0x00, 0x00, 0x00);
+               constexpr Color outlineColor(0x00, 0x00, 0xFA);
+               constexpr Color controlledColor(0xFA, 0xFA, 0x00);
+               constexpr Color entityColor(0x00, 0xFA, 0x00);
+               constexpr Color collisionColor(0xFA, 0x00, 0x00);
+               constexpr Color normalColor(0xFA, 0x00, 0x00);
+
+               canvas.SetColor(background);
+               canvas.Fill();
+
+               canvas.SetColor(outlineColor);
+               canvas.Grid(
+                       cam.ToScreen(Vector<int>(0, 0)),
+                       cam.ToScale(Vector<float>(10, 10)),
+                       cam.ToScale(Vector<float>(1, 1)));
+
+               canvas.SetColor(entityColor);
+               for (const AABB &e : stationary) {
+                       canvas.OutlineRect(
+                               cam.ToScreen(Vector<float>(e.Left(), e.Top())),
+                               cam.ToScale(e.Size()));
+               }
 
-       canvas.SetColor(controlledColor);
-       canvas.OutlineRect(
-               world.cam.ToScreen(Vector<float>(world.controlled.Left(), world.controlled.Top())),
-               world.cam.ToScale(world.controlled.Size()));
-
-       if (world.coll.empty()) return;
-
-       for (const World::Collision &c : world.coll) {
-               canvas.SetColor(collisionColor);
-               canvas.Arrow(
-                       world.cam.ToScreen(c.pos),
-                       world.cam.ToScreen(c.pos + c.depth));
-               canvas.SetColor(normalColor);
-               canvas.Arrow(
-                       world.cam.ToScreen(c.pos),
-                       world.cam.ToScreen(c.pos) + Vector<int>(c.norm * 25.0f));
+               canvas.SetColor(controlledColor);
+               canvas.OutlineRect(
+                       cam.ToScreen(Vector<float>(controlled.Left(), controlled.Top())),
+                       cam.ToScale(controlled.Size()));
+
+               if (coll.empty()) return;
+
+               for (const Collision &c : coll) {
+                       canvas.SetColor(collisionColor);
+                       canvas.Arrow(
+                               cam.ToScreen(c.pos),
+                               cam.ToScreen(c.pos + c.depth));
+                       canvas.SetColor(normalColor);
+                       canvas.Arrow(
+                               cam.ToScreen(c.pos),
+                               cam.ToScreen(c.pos) + Vector<int>(c.norm * 25.0f));
+               }
        }
-}
 
-void run(Canvas &canvas) {
-       World world;
-       world.cam.SetScale(Vector<float>(32, 32));
-       world.controlled.Resize(Vector<float>(2, 3));
-       world.controlled.Move(Vector<float>(1, 1.5));
-
-       AABB e;
-       e.Resize(Vector<float>(2, 2));
-       e.Move(Vector<float>(5, 5));
-       world.stationary.push_back(e);
-       e.Move(Vector<float>(0, 2));
-       world.stationary.push_back(e);
-       e.Move(Vector<float>(-2, 0));
-       world.stationary.push_back(e);
-
-       Uint32 last = SDL_GetTicks();
-       while (world.alive) {
-               handle(world);
-               Uint32 now = SDL_GetTicks();
-
-               int delta = now - last;
-               if (delta == 0) {
-                       SDL_Delay(1);
-                       continue;
-               } else if (delta > 30) {
-                       delta = 30;
+       void run(Canvas &canvas) {
+               cam.SetScale(Vector<float>(32, 32));
+               controlled.Resize(Vector<float>(2, 3));
+               controlled.Move(Vector<float>(1, 1.5));
+
+               AABB e;
+               e.Resize(Vector<float>(2, 2));
+               e.Move(Vector<float>(5, 5));
+               stationary.push_back(e);
+               e.Move(Vector<float>(0, 2));
+               stationary.push_back(e);
+               e.Move(Vector<float>(-2, 0));
+               stationary.push_back(e);
+
+               Uint32 last = SDL_GetTicks();
+               while (alive) {
+                       handle();
+                       Uint32 now = SDL_GetTicks();
+
+                       int delta = now - last;
+                       if (delta == 0) {
+                               SDL_Delay(1);
+                               continue;
+                       } else if (delta > 30) {
+                               delta = 30;
+                       }
+
+                       update(delta);
+                       render(canvas);
+
+                       canvas.Present();
+                       last = now;
                }
-
-               update(delta, world);
-               render(canvas, world);
-
-               canvas.Present();
-               last = now;
        }
-}
+
+};
 
 }
 
@@ -224,7 +216,8 @@ int main(int argc, const char *argv[]) {
                0
        ));
 
-       run(canv);
+       World world;
+       world.run(canv);
 
        return 0;
 }