#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(10, 10, 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())
, last(SDL_GetTicks())
, running(false) {
-
+ controlled = univ.AddEntity(Entity());
}
HandleEvents();
Update(delta);
Render();
- screen.Flip();
+ canvas.Present();
}
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
case SDLK_RIGHT:
focus.MoveRight();
break;
+ case SDLK_w:
+ control.y -= 1;
+ break;
+ case SDLK_s:
+ control.y += 1;
+ break;
+ case SDLK_a:
+ control.x -= 1;
+ break;
+ case SDLK_d:
+ control.x += 1;
+ break;
+ case SDLK_z:
+ cam.StartZoom();
+ break;
+ case SDLK_x:
+ cam.StartShrink();
+ break;
default:
break;
}
case SDLK_RIGHT:
focus.StopRight();
break;
+ case SDLK_w:
+ control.y += 1;
+ break;
+ case SDLK_s:
+ control.y -= 1;
+ break;
+ case SDLK_a:
+ control.x += 1;
+ break;
+ case SDLK_d:
+ control.x -= 1;
+ break;
+ case SDLK_z:
+ cam.StopZoom();
+ break;
+ case SDLK_x:
+ cam.StopShrink();
+ break;
default:
break;
}
void Application::Update(int dt) {
- focus.Update(dt / 1e3);
+ const float delta = dt / 1e3;
+ controlled->acc = Vector<float>(control * 10);
+ cam.Update(delta);
+ univ.Update(delta);
+ focus.SetSpeed(500 / cam.Zoom());
+ focus.Update(delta);
}
void Application::Render() {
constexpr Color background(0x00, 0x00, 0x00);
- constexpr Color univGrid(0xFF, 0xFF, 0xFF);
- constexpr Color sectGrid(0xAA, 0xAA, 0xAA);
-
- constexpr Vector<int> areaSize(10, 10);
- constexpr Vector<int> sectSize(areaSize * 10);
- constexpr Vector<int> univSize(sectSize * 10);
-
- SDL_Surface *dst = screen.Screen();
- Vector<int> offset = cam.Offset();
-
- Fill(dst, background);
- Grid(dst, offset, offset + univSize + Vector<int>(1, 1), areaSize, sectGrid);
- Grid(dst, offset, offset + univSize + Vector<int>(1, 1), sectSize, univGrid);
- Cross(dst, offset + Vector<int>(focus.Pos()), 15, Color(0xFF, 0xFF, 0x00));
+ constexpr Color univGrid(0xEE, 0xEE, 0xEE);
+ constexpr Color secGrid(0x77, 0x77, 0x77);
+ constexpr Color entityColor(0x00, 0xAA, 0xAA);
+ constexpr Color focusColor(0xFA, 0xFA, 0x00);
+
+ 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 Entity &e : univ.Entities()) {
+ canvas.Cross(
+ cam.ToScreen(Vector<float>(e.area * univ.areaSize) + e.pos),
+ 10);
+ }
}
}