From a1c0b5e97f829fd0d0dc7f8cade36e304a593908 Mon Sep 17 00:00:00 2001 From: Diogo Cruz Diniz Date: Thu, 16 Apr 2026 23:08:01 +0100 Subject: [PATCH] I segfault, I don't know why, it doesn't even matter how hard you try --- src/main.cpp | 1 - src/renderer.hpp | 2 +- src/sim/af_spawner.cpp | 6 ++---- src/sim/af_spawner.hpp | 3 +-- src/sim/airfield.cpp | 13 ++++++++++++- src/sim/airfield.hpp | 6 +++++- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 48a2588..6132001 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,7 +46,6 @@ int main(int argc, char *argv[]) // World init dxd::sim::World world = dxd::sim::World({ .x = 10, .y = 10 }); - world.add_obj(new dxd::sim::Aircraft({ 0, 0 }, 1, 5)); world.add_obj(new dxd::sim::AFSpawner()); SDL_Event event; diff --git a/src/renderer.hpp b/src/renderer.hpp index ee1938d..092a4f6 100644 --- a/src/renderer.hpp +++ b/src/renderer.hpp @@ -94,7 +94,7 @@ public: void diamond(vec2 center, float size) { - size *= sqrtf(2); + size *= M_SQRT2f; SDL_FPoint points[5]; vec2 p; diff --git a/src/sim/af_spawner.cpp b/src/sim/af_spawner.cpp index c0501c6..033fe65 100644 --- a/src/sim/af_spawner.cpp +++ b/src/sim/af_spawner.cpp @@ -2,12 +2,10 @@ void dxd::sim::AFSpawner::tick(float timestep, World *world) { - ++_cur_tick; - - if (_cur_tick < _next_spawn) + if (_next_spawn-- == 0) return; - _next_spawn = _cur_tick + 60 + SDL_rand(120); + _next_spawn = 60 + SDL_rand(120); //Pos should spawn in a grid, check if free, and offset random from grid coord to look natural vec2 pos = {(SDL_randf()-.5f)*2*60.0f, (SDL_randf()-.5f)*2*60.0f}; diff --git a/src/sim/af_spawner.hpp b/src/sim/af_spawner.hpp index bb1fb4a..6acb109 100644 --- a/src/sim/af_spawner.hpp +++ b/src/sim/af_spawner.hpp @@ -13,14 +13,13 @@ class AFSpawner : public WorldObject { private: std::vector _airfields; - int _cur_tick; int _next_spawn; public: AFSpawner() { _airfields = std::vector(); - _cur_tick = _next_spawn = 0; + _next_spawn = 0; } void tick(float timestep, World *world) override; diff --git a/src/sim/airfield.cpp b/src/sim/airfield.cpp index e372053..5ef2c4b 100644 --- a/src/sim/airfield.cpp +++ b/src/sim/airfield.cpp @@ -1,9 +1,20 @@ #include "airfield.hpp" +#include "aircraft.hpp" void dxd::sim::Airfield::tick(float timestep, World *world) { if (--_ttl == 0) - world->remove_obj(this); + { + //world->remove_obj(this); + return; + } + + if (--_next_spawn == 0) + { + Aircraft *af = new Aircraft(_position, SDL_randf() * M_PIf, 5.0f); + world->add_obj(af); + _next_spawn = 60 + SDL_rand(240); + } } void dxd::sim::Airfield::draw(Renderer *rend) diff --git a/src/sim/airfield.hpp b/src/sim/airfield.hpp index e42304b..de2309b 100644 --- a/src/sim/airfield.hpp +++ b/src/sim/airfield.hpp @@ -13,9 +13,13 @@ class Airfield : public WorldObject private: vec2 _position; int _ttl; + int _next_spawn; public: - Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl) {} + Airfield(vec2 position, int ttl) : _position(position), _ttl(ttl) + { + _next_spawn = 30; + } void tick(float timestep, World *world) override; void draw(Renderer *rend) override;