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 , worldTex(canvas.CreateStaticTexture(world.Size()))
20 , focus(Vector<float>(250, 250), 25)
21 , cam(c.Size(), focus.Pos())
22 , last(SDL_GetTicks())
29 void Application::Run() {
32 Uint32 now = SDL_GetTicks();
33 int delta = now - last;
40 void Application::Loop(int delta) {
41 cout << "delta: " << delta << endl << endl;
42 auto enter = chrono::system_clock::now();
45 auto event = chrono::system_clock::now();
46 cout << " event: " << duration_cast<milliseconds>(event - enter).count() << endl;
56 update = chrono::system_clock::now();
57 cout << " update: " << duration_cast<milliseconds>(update - event).count() << endl;
61 auto render = chrono::system_clock::now();
62 cout << " render: " << duration_cast<milliseconds>(render - update).count() << endl;
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) {
119 void Application::OnKeyUp(const SDL_KeyboardEvent &e) {
120 switch (e.keysym.sym) {
139 void Application::Update(int dt) {
140 const float delta = dt / 1e3;
147 void Application::Render() {
154 void Application::RenderBackground() {
155 constexpr Color background(0x00, 0x00, 0x00);
157 canvas.SetColor(background);
161 void Application::RenderWorld() {
162 if (world.ColorDirty()) {
163 worldTex.SetColors(world.GetColors());
167 const Vector<int> pos = cam.ToScreen(Vector<float>(0, 0));
168 canvas.Copy(worldTex, pos);
171 void Application::RenderEntities() {
172 constexpr Color entityColor(0x00, 0xFA, 0x00);
173 canvas.SetColor(entityColor);
175 for (const Entity &e : world.Entities()) {
176 canvas.Cross(cam.ToScreen(e.pos), 10);
180 void Application::RenderUI() {
181 constexpr Color focusColor(0xFA, 0xFA, 0x00);
182 constexpr Color forceColor(0xFA, 0x00, 0x00);
183 constexpr Color speedColor(0x00, 0xFA, 0x00);
185 canvas.SetColor(focusColor);
186 canvas.Cross(cam.ToScreen(focus.Pos()), 15);
188 for (const Entity &e : world.Entities()) {
189 const Vector<int> screenPos = cam.ToScreen(e.pos);
191 canvas.SetColor(forceColor);
192 canvas.Arrow(screenPos, screenPos + Vector<int>(e.acc * e.mass * 10.0f));
194 canvas.SetColor(speedColor);
195 canvas.Arrow(screenPos, screenPos + Vector<int>(e.vel * 10.0f));
197 cout << "entity: pos " << e.pos << ", vel " << e.vel << ", acc " << e.acc << endl;
198 cout << " vel mag: " << Length(e.vel) << ", acc mag: " << Length(e.acc) << endl;