feat: Airfields, scaling and movement

This commit is contained in:
2026-04-16 22:18:42 +01:00
parent 8b6b0a20f3
commit 965c0f393c
5 changed files with 63 additions and 12 deletions

View File

@@ -8,6 +8,7 @@
#include "sim/world.hpp" #include "sim/world.hpp"
#include "sim/aircraft.hpp" #include "sim/aircraft.hpp"
#include "sim/airfield.hpp"
static SDL_Window *window; static SDL_Window *window;
static SDL_Renderer *sdl_renderer; static SDL_Renderer *sdl_renderer;
@@ -44,8 +45,8 @@ int main(int argc, char *argv[])
// World init // World init
dxd::sim::World world = dxd::sim::World({ .x = 10, .y = 10 }); 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(new dxd::sim::Aircraft({ 0, 0 }, 1, 5));
world.add_obj(ac); world.add_obj(new dxd::sim::Airfield({ 15, 0 }));
SDL_Event event; SDL_Event event;
bool running = true; bool running = true;
@@ -53,20 +54,29 @@ int main(int argc, char *argv[])
while (running) { while (running) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) running = false; 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 // Clear
renderer.color(0, 0, 0, 255); renderer.color(0, 0, 0, 255);
SDL_RenderClear(sdl_renderer); SDL_RenderClear(sdl_renderer);
// World draw // World draw
renderer.color(255, 255, 255, 255);
world.draw(&renderer); world.draw(&renderer);
world.tick(1.0f / 60.0f); world.tick(1.0f / 60.0f);
// Usual test // Usual test
renderer.color(127, 0, 0, 255); renderer.color(64, 64, 64, 255);
renderer.line(-vec2_one, vec2_one); renderer.line(-vec2_unity * 2000, vec2_unity * 2000);
renderer.line(-vec2_unitx * 2000, vec2_unitx * 2000);
SDL_RenderPresent(sdl_renderer); SDL_RenderPresent(sdl_renderer);
SDL_Delay(1000 / 60); SDL_Delay(1000 / 60);

View File

@@ -12,6 +12,8 @@ typedef struct vec2i { int x, y; } vec2i;
static const vec2 vec2_zero = { 0, 0 }; static const vec2 vec2_zero = { 0, 0 };
static const vec2 vec2_one = { 1, 1 }; 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_max = { FLT_MAX, FLT_MAX };
static const vec2 vec2_min = { -FLT_MAX, -FLT_MAX }; static const vec2 vec2_min = { -FLT_MAX, -FLT_MAX };
static const vec2i vec2i_zero = { 0, 0 }; static const vec2i vec2i_zero = { 0, 0 };

View File

@@ -47,7 +47,12 @@ public:
Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height) Renderer(SDL_Renderer *sdl, int width, int height) : _sdl(sdl), _width(width), _height(height)
{ {
_center = { 0, 0 }; _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) 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); 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(to_viewv2(center - vec2_one * (size/2)));
vec2 screen = to_screenv2(view);
float scr_size = to_screenf(to_viewf(size)); float scr_size = to_screenf(to_viewf(size));
SDL_FRect rect = { SDL_FRect rect = {
.x = screen.x, .x = screen.x,
@@ -74,6 +78,38 @@ public:
}; };
SDL_RenderFillRect(_sdl, &rect); 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 } // namespace dxd

View File

@@ -7,6 +7,7 @@ void dxd::sim::Aircraft::tick(float timestep, vec2 bounds)
void dxd::sim::Aircraft::draw(Renderer *rend) void dxd::sim::Aircraft::draw(Renderer *rend)
{ {
rend->dot(_position, 0.02); rend->color(255, 255, 255, 255);
rend->line(_position, _position + polar_to_vec2(_direction, 0.05)); rend->box(_position, 1);
rend->line(_position, _position + polar_to_vec2(_direction, 3));
} }

View File

@@ -7,5 +7,7 @@ void dxd::sim::Airfield::tick(float timestep, vec2 bounds)
void dxd::sim::Airfield::draw(Renderer *rend) void dxd::sim::Airfield::draw(Renderer *rend)
{ {
rend->color(127, 127, 127, 255);
rend->rect(_position, 5);
rend->diamond(_position, 5);
} }