X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fcollision.cpp;h=4212507f8fca29749862b077ebeeee8cd8348ee3;hb=962405ec344818a7f6850d243feca7989ae5d41b;hp=af4234aa10e7613335fe9a1bbb1d5fe50178dd3b;hpb=dbc08d84d9de1a77cba0dd97e4701f4ac99d056e;p=orbi.git diff --git a/src/collision.cpp b/src/collision.cpp index af4234a..4212507 100644 --- a/src/collision.cpp +++ b/src/collision.cpp @@ -5,6 +5,7 @@ #include "graphics/Vector.h" #include "graphics/Window.h" #include "world/AABB.h" +#include "world/Collision.h" #include #include @@ -27,11 +28,6 @@ struct World { AABB controlled; vector stationary; - struct Collision { - Vector pos; - Vector norm; - Vector depth; - }; vector 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 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 speed { 5, 5 }; - const float delta = dt / 1e3; + const float delta = dt / 1e3; - world.controlled.Move(Vector(world.move) * speed * delta); - world.focus = world.controlled.Center(); + controlled.Move(Vector(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(0, 0)), - world.cam.ToScale(Vector(10, 10)), - world.cam.ToScale(Vector(1, 1))); - - canvas.SetColor(entityColor); - for (const AABB &e : world.stationary) { - canvas.OutlineRect( - world.cam.ToScreen(Vector(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(0, 0)), + cam.ToScale(Vector(10, 10)), + cam.ToScale(Vector(1, 1))); + + canvas.SetColor(entityColor); + for (const AABB &e : stationary) { + canvas.OutlineRect( + cam.ToScreen(Vector(e.Left(), e.Top())), + cam.ToScale(e.Size())); + } - canvas.SetColor(controlledColor); - canvas.OutlineRect( - world.cam.ToScreen(Vector(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(c.norm * 25.0f)); + canvas.SetColor(controlledColor); + canvas.OutlineRect( + cam.ToScreen(Vector(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(c.norm * 25.0f)); + } } -} -void run(Canvas &canvas) { - World world; - world.cam.SetScale(Vector(32, 32)); - world.controlled.Resize(Vector(2, 3)); - world.controlled.Move(Vector(1, 1.5)); - - AABB e; - e.Resize(Vector(2, 2)); - e.Move(Vector(5, 5)); - world.stationary.push_back(e); - e.Move(Vector(0, 2)); - world.stationary.push_back(e); - e.Move(Vector(-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(32, 32)); + controlled.Resize(Vector(2, 3)); + controlled.Move(Vector(1, 1.5)); + + AABB e; + e.Resize(Vector(2, 2)); + e.Move(Vector(5, 5)); + stationary.push_back(e); + e.Move(Vector(0, 2)); + stationary.push_back(e); + e.Move(Vector(-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; }