1 #include "Application.h"
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
5 #include "../world/Entity.h"
6 #include "../world/Tileset.h"
7 #include "../world/World.h"
12 Application::Application(Canvas &c, World &w, Tileset &t)
16 , focus(Vector<float>(5, 5), 2)
17 , cam(c.Size(), focus.Pos())
18 , last(SDL_GetTicks())
21 cam.SetScale(tiles.TileSize());
25 void Application::Run() {
28 Uint32 now = SDL_GetTicks();
29 int delta = now - last;
36 void Application::Loop(int delta) {
42 } else if (delta > 30) {
57 void Application::HandleEvents() {
59 while (SDL_PollEvent(&event)) {
65 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
66 cam.Resize(event.window.data1, event.window.data2);
70 if (!event.key.repeat) {
75 if (!event.key.repeat) {
86 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
87 switch (e.keysym.sym) {
108 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
109 switch (e.keysym.sym) {
128 void Application::Update(int dt) {
129 const float delta = dt / 1e3;
136 void Application::Render() {
143 void Application::RenderBackground() {
144 constexpr Color background(0x00, 0x00, 0x00);
146 canvas.SetColor(background);
150 void Application::RenderWorld() {
151 const Vector<int> begin(0, 0);
152 const Vector<int> end(world.Size());
154 for (Vector<int> pos(begin); pos.y < end.y; ++pos.y) {
155 for (pos.x = 0; pos.x < end.x; ++pos.x) {
156 tiles.DrawFG(canvas, cam.ToScreen(pos), world, pos);
161 void Application::RenderEntities() {
162 constexpr Color entityColor(0x00, 0xFA, 0x00);
163 canvas.SetColor(entityColor);
165 for (const Entity &e : world.Entities()) {
166 const Vector<float> pos(e.Bounds().Left(), e.Bounds().Top());
167 const Vector<float> size(e.Bounds().Size());
168 canvas.OutlineRect(cam.ToScreen(pos), cam.ToScale(size));
172 void Application::RenderUI() {
173 constexpr Color outlineColor(0x00, 0x00, 0xFA);
174 constexpr Color focusColor(0xFA, 0xFA, 0x00);
176 canvas.SetColor(outlineColor);
177 canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
179 canvas.SetColor(focusColor);
180 canvas.Cross(cam.ToScreen(focus.Pos()), 15);