examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit ff494da64cb87734b2a98880e423546b093ab569
parent 3917a91f68f8dceb8155bf59c92df22f8152d23e
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Mon,  9 May 2022 23:47:05 +0100

gl-asteroids: Add variation to particle ttl

Diffstat:
Msrc/gl-asteroids.cpp | 32++++++++++++++++++++------------
1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -12,6 +12,8 @@ #include <optional> #include <functional> +/// SOUNDS + using sample = std::span<short>; std::mutex queued_sounds_mutex; @@ -58,6 +60,10 @@ void sound_routine(std::stop_token token) snd_pcm_close(handle); } +std::jthread sound(sound_routine); + +/// GRAPHICS + struct coord { coord() = default; @@ -170,6 +176,8 @@ coord& operator *=(coord& lhs, coord rhs) return lhs; } +/// PHYSICS + coord rotate(coord o, float a) { float s = sin(a), c = cos(a); @@ -270,13 +278,17 @@ static bool left_arena(entity const& e) return e.phys.left_arena(); } +std::random_device rd; +std::default_random_engine gen(rd()); + struct particle : entity { float ttl; static particle& make_particle(entity e, float ttl) { - return particles.emplace_back(e, ttl); + static std::uniform_real_distribution<float> d(.5, 1); + return particles.emplace_back(e, ttl * d(gen)); } static void move_all(float dt) { @@ -370,15 +382,10 @@ int main(int argc, char* argv[]) unsigned ammo = 10; float firerate = 2, bullet_speed = 1; - std::random_device rd; - std::default_random_engine gen(rd()); std::uniform_real_distribution<float> dis_p(-1,1); std::uniform_real_distribution<float> dis_s(0.05,0.25); - std::uniform_real_distribution<float> dis_ds(0.005,0.025); std::uniform_real_distribution<float> dis_v(0.02,0.05); - std::uniform_real_distribution<float> dis_z(.3,.6); std::uniform_int_distribution<int> dis_d(0,7); - std::uniform_real_distribution<float> dis_a(-3,3); std::array<short, 2 * 512> tone; for (unsigned i = 0; i < tone.size(); ++i) @@ -413,8 +420,6 @@ int main(int argc, char* argv[]) for (auto& c : starsv) c.x = dis_p(gen), c.y = dis_p(gen); - std::jthread sound(sound_routine); - while (!glfwWindowShouldClose(w) && not is_pressed(GLFW_KEY_ESCAPE)) { float t = glfwGetTime(); @@ -442,7 +447,8 @@ int main(int argc, char* argv[]) std::swap(v.x, v.y); } - rocks.emplace_back(random_rock_visual(), physics{p, v, 0, dis_a(gen), scale}); + static std::uniform_real_distribution<float> ang(-3,3); + rocks.emplace_back(random_rock_visual(), physics{p, v, 0, ang(gen), scale}); } if (powerup_time < t) @@ -543,12 +549,13 @@ int main(int argc, char* argv[]) play_sample(square); for (unsigned j = 0; j < 7; ++j) { + static std::uniform_real_distribution<float> scale(0.005,0.025); entity debris = *r; debris.vis = &random_rock_visual(); debris.phys.vel.x += .3 * dis_p(gen); debris.phys.vel.y += .3 * dis_p(gen); - debris.phys.scale = dis_ds(gen); - particle::make_particle(debris, .7); + debris.phys.scale = scale(gen); + particle::make_particle(debris, 1); } if (r->phys.scale > .13f) @@ -557,6 +564,7 @@ int main(int argc, char* argv[]) auto nr = *r; + static std::uniform_real_distribution<float> dis_z(.3,.6); float sf = dis_z(gen); r->phys.scale *= sf; @@ -601,7 +609,7 @@ int main(int argc, char* argv[]) debris.phys.vel.x += .3 * dis_p(gen); debris.phys.vel.y += .3 * dis_p(gen); debris.phys.scale *= 1.5 * dis_s(gen); - particle::make_particle(debris, .7); + particle::make_particle(debris, 2); } powerups.reset(); score -= 10;