diff --git a/src/main.cpp b/src/main.cpp index a404a8f..1c4dd49 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include "sim/world.hpp" #include "sim/aircraft.hpp" +#include "sim/airfield.hpp" static SDL_Window *window; static SDL_Renderer *sdl_renderer; @@ -44,8 +45,8 @@ int main(int argc, char *argv[]) // World init dxd::sim::World world = dxd::sim::World({ .x = 10, .y = 10 }); - dxd::sim::Aircraft *ac = new dxd::sim::Aircraft({ 0, 0 }, 1, 0.1); - world.add_obj(ac); + world.add_obj(new dxd::sim::Aircraft({ 0, 0 }, 1, 5)); + world.add_obj(new dxd::sim::Airfield({ 15, 0 })); SDL_Event event; bool running = true; @@ -53,20 +54,29 @@ int main(int argc, char *argv[]) while (running) { while (SDL_PollEvent(&event)) { if (event.type == SDL_EVENT_QUIT) running = false; + if (event.type == SDL_EVENT_KEY_DOWN) { + if (event.key.key == SDLK_ESCAPE) running = false; + } } + const bool *key_states = SDL_GetKeyboardState(NULL); + if (key_states[SDL_SCANCODE_W]) renderer.move_camera(vec2_unity * 30 * (1.0f / 60.0f)); + if (key_states[SDL_SCANCODE_S]) renderer.move_camera(-vec2_unity * 30 * (1.0f / 60.0f)); + if (key_states[SDL_SCANCODE_A]) renderer.move_camera(-vec2_unitx * 30 * (1.0f / 60.0f)); + if (key_states[SDL_SCANCODE_D]) renderer.move_camera(vec2_unitx * 30 * (1.0f / 60.0f)); + // Clear renderer.color(0, 0, 0, 255); SDL_RenderClear(sdl_renderer); // World draw - renderer.color(255, 255, 255, 255); world.draw(&renderer); world.tick(1.0f / 60.0f); // Usual test - renderer.color(127, 0, 0, 255); - renderer.line(-vec2_one, vec2_one); + renderer.color(64, 64, 64, 255); + renderer.line(-vec2_unity * 2000, vec2_unity * 2000); + renderer.line(-vec2_unitx * 2000, vec2_unitx * 2000); SDL_RenderPresent(sdl_renderer); SDL_Delay(1000 / 60); diff --git a/src/math.hpp b/src/math.hpp index 387d692..ae0360e 100644 --- a/src/math.hpp +++ b/src/math.hpp @@ -12,6 +12,8 @@ typedef struct vec2i { int x, y; } vec2i; static const vec2 vec2_zero = { 0, 0 }; static const vec2 vec2_one = { 1, 1 }; +static const vec2 vec2_unitx = { 1, 0 }; +static const vec2 vec2_unity = { 0, 1 }; static const vec2 vec2_max = { FLT_MAX, FLT_MAX }; static const vec2 vec2_min = { -FLT_MAX, -FLT_MAX }; static const vec2i vec2i_zero = { 0, 0 }; diff --git a/src/renderer.hpp b/src/renderer.hpp index 9cfeb22..54edf5c 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -47,7 +47,12 @@ public: Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height) { _center = { 0, 0 }; - _scale = 1; + _scale = 0.02; + } + + void move_camera(vec2 delta) + { + _center = _center + delta; } void color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) @@ -61,10 +66,9 @@ public: SDL_RenderLine(_sdl, as.x, as.y, bs.x, bs.y); } - void dot(vec2 center, float size) + void box(vec2 center, float size) { - vec2 view = to_viewv2(center - vec2_one * (size/2)); - vec2 screen = to_screenv2(view); + vec2 screen = to_screenv2(to_viewv2(center - vec2_one * (size/2))); float scr_size = to_screenf(to_viewf(size)); SDL_FRect rect = { .x = screen.x, @@ -74,6 +78,38 @@ public: }; SDL_RenderFillRect(_sdl, &rect); } + + void rect(vec2 center, float size) + { + vec2 screen = to_screenv2(to_viewv2(center - vec2_one * (size/2))); + float scr_size = to_screenf(to_viewf(size)); + SDL_FRect rect = { + .x = screen.x, + .y = screen.y - scr_size, //HACK: Do this somewhere else? Rect function? + .w = scr_size, + .h = scr_size, + }; + SDL_RenderRect(_sdl, &rect); + } + + void diamond(vec2 center, float size) + { + size *= sqrtf(2); + SDL_FPoint points[5]; + vec2 p; + + p = to_screenv2(to_viewv2(center + vec2_unitx * (size/2))); + points[0].x = p.x; points[0].y = p.y; + p = to_screenv2(to_viewv2(center + vec2_unity * (size/2))); + points[1].x = p.x; points[1].y = p.y; + p = to_screenv2(to_viewv2(center - vec2_unitx * (size/2))); + points[2].x = p.x; points[2].y = p.y; + p = to_screenv2(to_viewv2(center - vec2_unity * (size/2))); + points[3].x = p.x; points[3].y = p.y; + + points[4] = points[0]; + SDL_RenderLines(_sdl, points, 5); + } }; } // namespace dxd diff --git a/src/sim/aircraft.cpp b/src/sim/aircraft.cpp index a0cf55c..259fab7 100644 --- a/src/sim/aircraft.cpp +++ b/src/sim/aircraft.cpp @@ -7,6 +7,7 @@ void dxd::sim::Aircraft::tick(float timestep, vec2 bounds) void dxd::sim::Aircraft::draw(Renderer *rend) { - rend->dot(_position, 0.02); - rend->line(_position, _position + polar_to_vec2(_direction, 0.05)); + rend->color(255, 255, 255, 255); + rend->box(_position, 1); + rend->line(_position, _position + polar_to_vec2(_direction, 3)); } diff --git a/src/sim/airfield.cpp b/src/sim/airfield.cpp index 5b58f2d..6194405 100644 --- a/src/sim/airfield.cpp +++ b/src/sim/airfield.cpp @@ -7,5 +7,7 @@ void dxd::sim::Airfield::tick(float timestep, vec2 bounds) void dxd::sim::Airfield::draw(Renderer *rend) { - + rend->color(127, 127, 127, 255); + rend->rect(_position, 5); + rend->diamond(_position, 5); }