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)
19 , cam(c.Size(), focus)
20 , last(SDL_GetTicks())
23 cam.SetScale(tiles.TileSize());
27 void Application::Control(Entity &e) {
31 void Application::Relinquish() {
36 void Application::Run() {
39 Uint32 now = SDL_GetTicks();
40 int delta = now - last;
47 void Application::Loop(int delta) {
53 } else if (delta > 30) {
68 void Application::HandleEvents() {
70 while (SDL_PollEvent(&event)) {
76 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
77 cam.Resize(event.window.data1, event.window.data2);
81 if (!event.key.repeat) {
86 if (!event.key.repeat) {
97 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
98 switch (e.keysym.sym) {
124 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
125 switch (e.keysym.sym) {
149 void Application::Update(int dt) {
150 const float delta = dt / 1e3;
151 for (int i = 0; i < dt; ++i) {
155 target.Update(delta);
156 focus = ctrl.Controlling()
157 ? ctrl.Controlled().vbox.Center()
163 void Application::Render() {
170 void Application::RenderBackground() {
171 constexpr Color background(0x00, 0x00, 0x00);
172 constexpr Color outlineColor(0x00, 0x00, 0xFA);
174 canvas.SetColor(background);
177 canvas.SetColor(outlineColor);
178 canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(Vector<float>(1, 1)));
181 void Application::RenderWorld() {
182 const Vector<int> begin(0, 0);
183 const Vector<int> end(world.Size());
185 for (Vector<int> pos(begin); pos.y < end.y; ++pos.y) {
186 for (pos.x = 0; pos.x < end.x; ++pos.x) {
187 tiles.DrawFG(canvas, cam.ToScreen(pos), world, pos);
192 void Application::RenderEntities() {
193 constexpr Color boundsColor(0xFA, 0x00, 0x00);
194 constexpr Color vboxColor(0xFA, 0xFA, 0x00);
195 constexpr Color hboxColor(0x00, 0xFA, 0x00);
197 for (const Entity &e : world.Entities()) {
198 canvas.SetColor(boundsColor);
200 cam.ToScreen(Vector<float>(e.bounds.Left(), e.bounds.Top())),
201 cam.ToScale(Vector<float>(e.bounds.Size()))
203 canvas.SetColor(vboxColor);
205 cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Top())),
206 cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Top()))
209 cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Bottom())),
210 cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Bottom()))
212 canvas.SetColor(hboxColor);
214 cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Top())),
215 cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Bottom()))
218 cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Top())),
219 cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Bottom()))
224 void Application::RenderUI() {
225 constexpr Color targetColor(0xFA, 0xFA, 0x00);
227 canvas.SetColor(targetColor);
228 canvas.Cross(cam.ToScreen(target.Pos()), 15);