1 #include "Application.h"
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
5 #include "../world/Entity.h"
6 #include "../world/World.h"
11 using namespace chrono;
16 Application::Application(Canvas &c, World &w)
19 , focus(Vector<float>(250, 250), 25)
20 , cam(c.Size(), focus.Pos())
21 , last(SDL_GetTicks())
28 void Application::Run() {
31 Uint32 now = SDL_GetTicks();
32 int delta = now - last;
39 void Application::Loop(int delta) {
40 cout << "delta: " << delta << endl << endl;
41 auto enter = chrono::system_clock::now();
44 auto event = chrono::system_clock::now();
45 cout << " event: " << duration_cast<milliseconds>(event - enter).count() << endl;
55 update = chrono::system_clock::now();
56 cout << " update: " << duration_cast<milliseconds>(update - event).count() << endl;
60 auto render = chrono::system_clock::now();
61 cout << " render: " << duration_cast<milliseconds>(render - update).count() << endl;
67 void Application::HandleEvents() {
69 while (SDL_PollEvent(&event)) {
75 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
76 cam.Resize(event.window.data1, event.window.data2);
80 if (!event.key.repeat) {
85 if (!event.key.repeat) {
96 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
97 switch (e.keysym.sym) {
118 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
119 switch (e.keysym.sym) {
138 void Application::Update(int dt) {
139 const float delta = dt / 1e3;
146 void Application::Render() {
153 void Application::RenderBackground() {
154 constexpr Color background(0x00, 0x00, 0x00);
156 canvas.SetColor(background);
160 void Application::RenderWorld() {
161 Vector<int> begin(0, 0);
162 Vector<int> end(world.Size());
163 Vector<int> topLeft = cam.ToScreen(Vector<float>(begin));
164 Vector<int> bottomRight = cam.ToScreen(Vector<float>(end));
165 Vector<int> clip(canvas.Size());
167 if (begin.x > clip.x || begin.y > clip.y || end.x < 0 || end.y < 0) {
172 begin.x -= topLeft.x;
176 begin.y -= topLeft.y;
179 if (bottomRight.x > clip.x) {
180 end.x -= bottomRight.x - clip.x;
181 bottomRight.x = clip.x;
183 if (bottomRight.y > clip.y) {
184 end.y -= bottomRight.y - clip.y;
185 bottomRight.y = clip.y;
188 for (Vector<int> pos(begin), cur(topLeft); pos.y < end.y; ++pos.y, ++cur.y) {
189 for (pos.x = begin.x, cur.x = topLeft.x; pos.x < end.x; ++pos.x, ++cur.x) {
190 canvas.SetColor(world.ColorAt(pos));
196 void Application::RenderEntities() {
197 constexpr Color entityColor(0x00, 0xFA, 0x00);
198 canvas.SetColor(entityColor);
200 for (const Entity &e : world.Entities()) {
201 canvas.Cross(cam.ToScreen(e.pos), 10);
205 void Application::RenderUI() {
206 constexpr Color focusColor(0xFA, 0xFA, 0x00);
207 constexpr Color forceColor(0xFA, 0x00, 0x00);
208 constexpr Color speedColor(0x00, 0xFA, 0x00);
210 canvas.SetColor(focusColor);
211 canvas.Cross(cam.ToScreen(focus.Pos()), 15);
213 for (const Entity &e : world.Entities()) {
214 const Vector<int> screenPos = cam.ToScreen(e.pos);
216 canvas.SetColor(forceColor);
217 canvas.Arrow(screenPos, screenPos + Vector<int>(e.acc * e.mass * 10.0f));
219 canvas.SetColor(speedColor);
220 canvas.Arrow(screenPos, screenPos + Vector<int>(e.vel * 10.0f));
222 cout << "entity: pos " << e.pos << ", vel " << e.vel << ", acc " << e.acc << endl;