X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;h=5820c57ba3eb10aeea807893f3f384a7bc5438a9;hb=699437a474de8b87ccb6749d44adf740e680d620;hp=9ebc2613d7d24a93be4fca69ddc66c200d16a13d;hpb=2d0a41dc0a53915153ceccda914d59affd388864;p=space.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 9ebc261..5820c57 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -1,20 +1,19 @@ #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(10, 10), Vector(10, 10), Vector(10, 10), 10) +Application::Application(Canvas &c) +: canvas(c) +, univ(Vector(10, 10), Vector(10, 10), Vector(10, 10)) , focus(Vector(500, 500), 500) -, cam(800, 800, focus.Pos()) +, cam(c.Size(), focus.Pos()) , last(SDL_GetTicks()) , running(false) { - controlled = univ.AddEntity(Entity()); + controlled = univ.AddShip(Ship()); } @@ -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 @@ -88,10 +92,10 @@ void Application::OnKeyDown(const SDL_KeyboardEvent &e) { control.x += 1; break; case SDLK_z: - cam.DoubleZoom(); + cam.StartZoom(); break; case SDLK_x: - cam.HalfZoom(); + cam.StartShrink(); break; default: break; @@ -124,6 +128,12 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) { case SDLK_d: control.x -= 1; break; + case SDLK_z: + cam.StopZoom(); + break; + case SDLK_x: + cam.StopShrink(); + break; default: break; } @@ -132,9 +142,11 @@ void Application::OnKeyUp(const SDL_KeyboardEvent &e) { void Application::Update(int dt) { const float delta = dt / 1e3; - controlled->acc = Vector(control * 10); + controlled->rotThrottle = control.x; + controlled->linThrottle = -control.y; cam.Update(delta); univ.Update(delta); + focus.SetSpeed(500 / cam.Zoom()); focus.Update(delta); } @@ -146,18 +158,29 @@ void Application::Render() { constexpr Color entityColor(0x00, 0xAA, 0xAA); constexpr Color focusColor(0xFA, 0xFA, 0x00); - SDL_Surface *dst = screen.Screen(); - const Vector begin = cam.ToScreen(Vector(0, 0)); - const Vector end = - cam.ToScreen((univ.size * univ.secSize * univ.areaSize)) + Vector(1, 1);; - - Fill(dst, background); - Grid(dst, begin, end, cam.ToScale(univ.areaSize), secGrid); - Grid(dst, begin, end, cam.ToScale(univ.secSize * univ.areaSize), univGrid); - Cross(dst, cam.ToScreen(focus.Pos()), 15, focusColor); - - for (const Entity &e : univ.Entities()) { - Cross(dst, cam.ToScreen((e.area * univ.areaSize) + Vector(e.pos)), 10, entityColor); + canvas.SetColor(background); + canvas.Fill(); + + canvas.Grid2( + cam.ToScreen(Vector(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 direction = s.Dir(); + const Vector position = cam.ToScreen(Vector(s.area * univ.areaSize) + s.pos); + const Vector nose = position + Vector(direction * 15.0f); + const Vector left = position + Vector((Rotate90(direction) * 8.0f) - (direction * 4.0f)); + const Vector right = position + Vector((Rotate270(direction) * 8.0f) - (direction * 4.0f)); + canvas.Line(position, nose); + canvas.Quad(nose, left, position, right); } }