Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / commits / 92ba51c

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz

Remove rand dep AND cmath dep

Christian Immanuel · 2026-06-05 11:36 · 92ba51cdcde01d48a3b09e8687f6435e1d87f7e2

 src/rendering/floor_mandelbrot.cpp |  4 +---
 src/states/menu_state.cpp          |  4 +---
 src/systems/celebration_system.cpp |  5 ++---
 src/systems/particle_system.cpp    | 17 ++++++-----------
 src/systems/pult_system.cpp        |  2 --
 src/systems/word_ai_system.cpp     | 30 ++++++++++++------------------
 src/text_3d/text_3d.cpp            | 12 +++++-------
 src/utils/light.hpp                |  1 -
 src/utils/math.hpp                 | 19 ++++++++++++++++++-
 9 files changed, 45 insertions(+), 49 deletions(-)

diff --git a/src/rendering/floor_mandelbrot.cpp b/src/rendering/floor_mandelbrot.cpp
index f5836a9..5c05a2c 100644
--- a/src/rendering/floor_mandelbrot.cpp
+++ b/src/rendering/floor_mandelbrot.cpp
@@ -1,9 +1,7 @@
-#include "floor_mandelbrot.hpp"
-
 #include <SDL3/SDL.h>
-#include <cmath>
 
 #include "entities/camera.hpp"
+#include "floor_mandelbrot.hpp"
 
 
 void
diff --git a/src/states/menu_state.cpp b/src/states/menu_state.cpp
index 24c61ad..e13f4e8 100644
--- a/src/states/menu_state.cpp
+++ b/src/states/menu_state.cpp
@@ -1,10 +1,8 @@
-#include "menu_state.hpp"
-
 #include <SDL3/SDL.h>
-#include <cmath>
 
 #include "core/game.hpp"
 #include "levels/level.hpp"
+#include "menu_state.hpp"
 #include "rendering/gl_renderer.hpp"
 #include "states/game_state.hpp"
 
diff --git a/src/systems/celebration_system.cpp b/src/systems/celebration_system.cpp
index 75ed8f7..c70a418 100644
--- a/src/systems/celebration_system.cpp
+++ b/src/systems/celebration_system.cpp
@@ -41,14 +41,13 @@ CelebrationSystem::tick(double dt, ParticleSystem& particle_sys)
     // Fireworks bursts on a schedule. We aim for ~7 bursts across the
     // duration, with slight jitter to avoid mechanical pacing.
     if (_t >= _next_burst &&
-        _t <  _duration - 0.3)
-    {
+        _t <  _duration - 0.3) {
         // Random position across the play area, biased to upper half so
         // bursts read as airborne fireworks not floor_sys explosions.
         const double ws = ConfigGame::world_box_scale;
         const double margin = 3.0;
         auto rnd = []() {
-            return static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
+            return Random::unit();
         };
 
         Vec3 center {
diff --git a/src/systems/particle_system.cpp b/src/systems/particle_system.cpp
index d9e0807..4c25ed4 100644
--- a/src/systems/particle_system.cpp
+++ b/src/systems/particle_system.cpp
@@ -11,16 +11,11 @@
 
 namespace {
 
-// Cheap uniform random in [0, 1).
-double frand() {
-    return static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
-}
-
 // Random point on a unit sphere — used to spread burst velocities evenly.
 // Uses the "two uniform angles" method; fine for fireworks.
 Vec3 random_UnitSphere() {
-    double u    = frand() * 2.0 - 1.0;
-    double phi  = frand() * SDL_PI_D * 2.0;
+    double u    = Random::unit() * 2.0 - 1.0;
+    double phi  = Random::unit() * SDL_PI_D * 2.0;
     double s    = SDL_sqrt(1.0 - u * u);
     return Vec3{ s * SDL_cos(phi), u, s * SDL_sin(phi) };
 }
@@ -43,7 +38,7 @@ ParticleSystem::emit_FireworkBurst(const Vec3& center,
 
         // Speed 5..10 m/s in a random direction.
         Vec3   dir   = random_UnitSphere();
-        double speed = 5.0 + frand() * 5.0;
+        double speed = 5.0 + Random::unit() * 5.0;
         p.vel = dir * speed;
 
         // Slight upward bias so the burst reads as an explosion, not implosion.
@@ -51,9 +46,9 @@ ParticleSystem::emit_FireworkBurst(const Vec3& center,
 
         p.color = color;
         p.age      = 0.0;
-        p.lifetime = 2.5 + frand() * 1.5;
-        p.yaw  = static_cast<float>(frand() * SDL_PI_D * 2.0);
-        p.spin = static_cast<float>((frand() - 0.5) * 12.0);   // ± 6 rad/s
+        p.lifetime = 2.5 + Random::unit() * 1.5;
+        p.yaw  = static_cast<float>(Random::unit() * SDL_PI_D * 2.0);
+        p.spin = static_cast<float>((Random::unit() - 0.5) * 12.0);   // ± 6 rad/s
         p.alive = true;
 
         _particles.push_back(p);
diff --git a/src/systems/pult_system.cpp b/src/systems/pult_system.cpp
index 2a6a6c7..f857828 100644
--- a/src/systems/pult_system.cpp
+++ b/src/systems/pult_system.cpp
@@ -1,5 +1,3 @@
-#include <cmath>
-
 #include "entities/camera.hpp"
 #include "entities/player.hpp"
 #include "pult_system.hpp"
diff --git a/src/systems/word_ai_system.cpp b/src/systems/word_ai_system.cpp
index a536539..ff2d1ea 100644
--- a/src/systems/word_ai_system.cpp
+++ b/src/systems/word_ai_system.cpp
@@ -10,12 +10,6 @@
 #include "utils/config.hpp"
 
 
-static inline double
-random_unit()
-{
-    return (rand() % 10000) / 10000.0;
-}
-
 
 static Vec3
 perpendicular_horizontal(const Vec3& v)
@@ -101,9 +95,9 @@ WordAiSystem::update_Idle(Text3d& w, const Player& p, int active_ai_count, doubl
     if (dist_sq > ConfigAi::trigger_range * ConfigAi::trigger_range) return;
 
     double roll_threshold = ConfigAi::idle_aggro_roll * w.ai.aggressiveness * dt;
-    if (random_unit() > roll_threshold) return;
+    if (Random::unit() > roll_threshold) return;
 
-    if (random_unit() < ConfigAi::flee_bias) enter_Flee(w);
+    if (Random::unit() < ConfigAi::flee_bias) enter_Flee(w);
     else                            enter_Attack(w);
 }
 
@@ -141,8 +135,8 @@ void WordAiSystem::update_Attack(Text3d& w, const Player& p, CarriedLine& carrie
     if (w.ai.phase_timer <= 0.0) {
         w.ai.in_pause = !w.ai.in_pause;
         w.ai.phase_timer = w.ai.in_pause
-                           ? (0.4 + random_unit() * 0.8)
-                           : (1.0 + random_unit() * 1.5);
+                           ? (0.4 + Random::unit() * 0.8)
+                           : (1.0 + Random::unit() * 1.5);
     }
 
     Vec3 target_vel{};
@@ -171,7 +165,7 @@ void WordAiSystem::update_Attack(Text3d& w, const Player& p, CarriedLine& carrie
                 if (!node || !node->word) continue;
                 if (!Collision::aabb_Overlap(w, *node->word)) continue;
 
-                if (random_unit() < ConfigAi::steal_probability) {
+                if (Random::unit() < ConfigAi::steal_probability) {
                     Text3dPtr stolen = std::move(node->word);
                     stolen->body.mode = PhysicsMode::AiHover;
                     stolen->set_Color(ConfigColor::attack);
@@ -234,8 +228,8 @@ WordAiSystem::update_Flee(Text3d& w, const Player& p, DynArray<Text3dPtr>& relea
     if (w.ai.phase_timer <= 0.0) {
         w.ai.in_pause = !w.ai.in_pause;
         w.ai.phase_timer = w.ai.in_pause
-                           ? (0.3 + random_unit() * 0.5)
-                           : (1.5 + random_unit() * 1.5);
+                           ? (0.3 + Random::unit() * 0.5)
+                           : (1.5 + Random::unit() * 1.5);
     }
 
     Vec3 target_vel{};
@@ -258,7 +252,7 @@ WordAiSystem::update_Flee(Text3d& w, const Player& p, DynArray<Text3dPtr>& relea
         if (w.ai.state_timer > ConfigAi::flee_escape_time) {
             // Got away. If carrying loot, roll for drop-vs-keep.
             if (!w.ai.stolen.empty()) {
-                if (random_unit() < ConfigAi::drop_loot_probability) {
+                if (Random::unit() < ConfigAi::drop_loot_probability) {
                     release_StolenWords(w, released);
                 }
                 // Whether we dropped or kept, come back for another attack.
@@ -302,7 +296,7 @@ WordAiSystem::enter_Attack(Text3d& w)
 {
     w.ai.state             = Text3dAI::State::Attack;
     w.ai.state_timer       = 0.0;
-    w.ai.phase_timer       = 0.5 + random_unit() * 1.5;
+    w.ai.phase_timer       = 0.5 + Random::unit() * 1.5;
     w.ai.in_pause          = false;
     w.ai.has_stolen        = false;
     w.ai.hover_height_saved = w.body.hover_height;
@@ -310,8 +304,8 @@ WordAiSystem::enter_Attack(Text3d& w)
     w.set_Color(ConfigColor::attack);
 
     // Pick an orbit direction and starting angle for the no-carry wander.
-    w.ai.orbit_angle = random_unit() * SDL_PI_D * 2.0;
-    w.ai.orbit_dir   = (random_unit() < 0.5) ? -1.0 : 1.0;
+    w.ai.orbit_angle = Random::unit() * SDL_PI_D * 2.0;
+    w.ai.orbit_dir   = (Random::unit() < 0.5) ? -1.0 : 1.0;
 }
 
 
@@ -320,7 +314,7 @@ WordAiSystem::enter_Flee(Text3d& w)
 {
     w.ai.state             = Text3dAI::State::Flee;
     w.ai.state_timer       = 0.0;
-    w.ai.phase_timer       = 0.3 + random_unit() * 0.7;
+    w.ai.phase_timer       = 0.3 + Random::unit() * 0.7;
     w.ai.in_pause          = false;
     w.ai.hover_height_saved = w.body.hover_height;
     w.body.mode            = PhysicsMode::AiHover;
diff --git a/src/text_3d/text_3d.cpp b/src/text_3d/text_3d.cpp
index 311d3e5..5d56dbd 100644
--- a/src/text_3d/text_3d.cpp
+++ b/src/text_3d/text_3d.cpp
@@ -1,6 +1,4 @@
 #include <SDL3/SDL.h>
-#include <algorithm>
-#include <cstdlib>
 
 #include "c_token/token_color.hpp"
 #include "c_token/token_db.hpp"
@@ -89,14 +87,14 @@ Text3d::Text3d(const std::string& txt,
         set_Color(TokenColor::get(token_info->type));
 
     rebuild_Mesh();
-    body.hover_phase = (rand() % 1000) / 1000.0 * SDL_PI_D * 2.0;
+    body.hover_phase = Random::unit() * SDL_PI_D * 2.0;
 
 
     // AI personality
-    ai.aggressiveness = (rand() % 1000) / 1000.0;
-    ai.wiggle_phase   = (rand() % 1000) / 1000.0 * SDL_PI_D * 2.0;
-    ai.wiggle_freq    = 3.0 + (rand() % 1000) / 1000.0 * 4.0;   // 3–7 rad/s
-    ai.speed          = 2.5 + (rand() % 1000) / 1000.0 * 2.5;
+    ai.aggressiveness = Random::unit();
+    ai.wiggle_phase   = Random::unit() * SDL_PI_D * 2.0;
+    ai.wiggle_freq    = 3.0 + Random::unit() * 4.0;   // 3–7 rad/s
+    ai.speed          = 2.5 + Random::unit() * 2.5;
 }
 
 
diff --git a/src/utils/light.hpp b/src/utils/light.hpp
index 2cc25bf..d436bd8 100644
--- a/src/utils/light.hpp
+++ b/src/utils/light.hpp
@@ -1,7 +1,6 @@
 #pragma once
 
 #include "math.hpp"
-#include <cmath>
 
 // ─────────────────────────────────────────────────────────────────────────
 // Light
diff --git a/src/utils/math.hpp b/src/utils/math.hpp
index a37a1a3..1ebe461 100644
--- a/src/utils/math.hpp
+++ b/src/utils/math.hpp
@@ -1,6 +1,23 @@
 #pragma once
 
-#include <cmath>
+#include <random>
+
+
+namespace Random {
+inline int int_MinMax(int min, int max) {
+    std::mt19937 rng(std::random_device{}());
+    std::uniform_int_distribution<int> dist(min, max);
+    return dist(rng);
+}
+
+inline double
+unit()
+{
+    static std::mt19937 rng(std::random_device{}());
+    static std::uniform_real_distribution<double> dist(0.0, 1.0);
+    return dist(rng);
+}
+} // Rendom
 
 
 struct Vec3 {