1 #include "Application.h"
3 #include "../graphics/Canvas.h"
4 #include "../graphics/Color.h"
5 #include "../world/World.h"
10 using namespace chrono;
15 Application::Application(Canvas &c, World &w)
18 , focus(Vector<float>(5, 5), 25)
19 , cam(c.Size(), focus.Pos())
20 , last(SDL_GetTicks())
27 void Application::Run() {
30 Uint32 now = SDL_GetTicks();
31 int delta = now - last;
38 void Application::Loop(int delta) {
39 cout << "delta: " << delta << endl << endl;
54 void Application::HandleEvents() {
56 while (SDL_PollEvent(&event)) {
62 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
63 cam.Resize(event.window.data1, event.window.data2);
67 if (!event.key.repeat) {
72 if (!event.key.repeat) {
83 void Application::OnKeyDown(const SDL_KeyboardEvent &e) {
84 switch (e.keysym.sym) {
105 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
106 switch (e.keysym.sym) {
125 void Application::Update(int dt) {
126 const float delta = dt / 1e3;
133 void Application::Render() {
134 auto enter = chrono::system_clock::now();
136 auto background = chrono::system_clock::now();
137 cout << " background: " << duration_cast<milliseconds>(background - enter).count() << endl;
139 auto world = chrono::system_clock::now();
140 cout << " world: " << duration_cast<milliseconds>(world - background).count() << endl;
142 auto entities = chrono::system_clock::now();
143 cout << " entities: " << duration_cast<milliseconds>(entities - world).count() << endl;
145 auto ui = chrono::system_clock::now();
146 cout << " UI: " << duration_cast<milliseconds>(ui - entities).count() << endl;
149 void Application::RenderBackground() {
150 constexpr Color background(0x00, 0x00, 0x00);
152 canvas.SetColor(background);
156 void Application::RenderWorld() {
157 Vector<int> begin(0, 0);
158 Vector<int> end(world.Size());
159 Vector<int> topLeft = cam.ToScreen(Vector<float>(begin));
160 Vector<int> bottomRight = cam.ToScreen(Vector<float>(end));
161 Vector<int> clip(canvas.Size());
163 if (begin.x > clip.x || begin.y > clip.y || end.x < 0 || end.y < 0) {
168 begin.x -= topLeft.x;
172 begin.y -= topLeft.y;
175 if (bottomRight.x > clip.x) {
176 end.x -= bottomRight.x - clip.x;
177 bottomRight.x = clip.x;
179 if (bottomRight.y > clip.y) {
180 end.y -= bottomRight.y - clip.y;
181 bottomRight.y = clip.y;
184 for (Vector<int> pos(begin), cur(topLeft); pos.y < end.y; ++pos.y, ++cur.y) {
185 for (pos.x = begin.x, cur.x = topLeft.x; pos.x < end.x; ++pos.x, ++cur.x) {
186 canvas.SetColor(world.ColorAt(pos));
192 void Application::RenderEntities() {
196 void Application::RenderUI() {
197 constexpr Color focusColor(0xFA, 0xFA, 0x00);
198 constexpr Color forceColor(0xFA, 0x00, 0x00);
200 canvas.SetColor(focusColor);
201 canvas.Cross(cam.ToScreen(focus.Pos()), 15);
203 const Vector<float> force = world.ForceAt(focus.Pos(), 1);
204 const Vector<int> screenFocus = cam.ToScreen(focus.Pos());
206 canvas.SetColor(forceColor);
207 canvas.Arrow(screenFocus, screenFocus + Vector<int>(force * 10.0f));
208 cout << "force on 1kg at " << focus.Pos() << ": " << force << endl;