1 #include "Application.h"
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
5 #include "../world/Character.h"
6 #include "../world/Entity.h"
7 #include "../world/Tileset.h"
8 #include "../world/World.h"
13 Application::Application(Canvas &c, World &w, Tileset &t)
20 , cam(c.Size(), focus)
21 , last(SDL_GetTicks())
24 cam.SetScale(tiles.TileSize());
28 void Application::Control(Entity &e) {
32 void Application::Relinquish() {
37 void Application::Run() {
40 Uint32 now = SDL_GetTicks();
41 int delta = now - last;
48 void Application::Loop(int delta) {
54 } else if (delta > 30) {
69 void Application::HandleEvents() {
71 while (SDL_PollEvent(&event)) {
77 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
78 cam.Resize(event.window.data1, event.window.data2);
82 if (!event.key.repeat) {
87 if (!event.key.repeat) {
98 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
99 switch (e.keysym.sym) {
125 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
126 switch (e.keysym.sym) {
150 void Application::Update(int dt) {
151 const float delta = dt / 1e3;
152 for (int i = 0; i < dt; ++i) {
156 target.Update(delta);
157 focus = ctrl.Controlling()
158 ? ctrl.Controlled().vbox.Center()
162 // testing arm rotation
163 //dynamic_cast<Character *>(*world.Entities().begin())->armAngle -= 3 * delta;
167 void Application::Render() {
174 void Application::RenderBackground() {
175 constexpr Color background(0x00, 0x00, 0x00);
176 constexpr Color outlineColor(0x00, 0x00, 0xFA);
178 canvas.SetColor(background);
181 canvas.SetColor(outlineColor);
182 canvas.Grid(cam.ToScreen(Vector<int>(0, 0)), cam.ToScale(world.Size()), cam.ToScale(world.TileSize()));
185 void Application::RenderWorld() {
186 const Vector<int> begin(0, 0);
187 const Vector<int> end(world.Size());
189 for (Vector<int> pos(begin); pos.y < end.y; ++pos.y) {
190 for (pos.x = 0; pos.x < end.x; ++pos.x) {
191 tiles.DrawFG(canvas, cam.ToScreen(pos), world, pos);
196 void Application::RenderEntities() {
197 constexpr Color boundsColor(0xFA, 0x00, 0x00);
198 constexpr Color vboxColor(0xFA, 0xFA, 0x00);
199 constexpr Color hboxColor(0x00, 0xFA, 0x00);
200 constexpr Color dotColor(0x00, 0xFA, 0xFA);
202 for (const Entity *e : world.Entities()) {
203 canvas.SetColor(boundsColor);
205 cam.ToScreen(e->bounds.LeftTop()),
206 cam.ToScale(e->bounds.Size())
208 if (const Character *c = dynamic_cast<const Character *>(e)) {
209 if (c->orient == Entity::LOOKS_LEFT) {
210 canvas.OutlineRectRot(
212 cam.ToScreen(c->bounds.LeftTop() + c->arm.LeftTop()),
213 cam.ToScale(c->arm.Size())
215 cam.ToScreen(c->bounds.LeftTop() + c->armOrigin),
218 //canvas.SetColor(dotColor);
219 //canvas.Cross(cam.ToScreen(c->bounds.LeftTop() + c->armOrigin), 5);
221 canvas.OutlineRectRot(
223 cam.ToScreen(c->bounds.RightTop() + MirrorY(c->arm.RightTop())),
224 cam.ToScale(c->arm.Size())
226 cam.ToScreen(c->bounds.RightTop() + MirrorY(c->armOrigin)),
229 //canvas.SetColor(dotColor);
230 //canvas.Cross(cam.ToScreen(c->bounds.RightTop() + MirrorY(c->armOrigin)), 5);
233 canvas.SetColor(vboxColor);
235 cam.ToScreen(e->vbox.LeftTop()),
236 cam.ToScale(e->vbox.Size())
238 canvas.SetColor(hboxColor);
240 cam.ToScreen(e->hbox.LeftTop()),
241 cam.ToScale(e->hbox.Size())
246 void Application::RenderUI() {
247 constexpr Color targetColor(0xFA, 0xFA, 0x00);
249 canvas.SetColor(targetColor);
250 canvas.Cross(cam.ToScreen(target.Pos()), 15);