examples

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

commit 00c134357d45d84224f4fc126c921f12b2ea2b6f
parent f0b3d10ed4c48f7051f94d75737b337f786d6fd5
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Sat,  2 Apr 2022 18:15:45 +0100

gl-asteroids: Add basic particle system

Diffstat:
Msrc/gl-asteroids.cpp | 43++++++++++++++++++++++++++++++++++++++++---
1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -125,7 +125,7 @@ struct physics pos += vel * dt; pos.x = wrap(pos.x, -1, 1); pos.y = wrap(pos.y, -1, 1); - rot += ang_mom * dt; + rot += ang_mom * dt; } }; @@ -133,7 +133,7 @@ bool is_colliding(physics const& a, physics const& b) { float len = a.scale + b.scale; auto d = a.pos - b.pos; - + return d.x * d.x + d.y * d.y < len * len; } @@ -168,7 +168,36 @@ struct entity } }; -GLFWwindow* w; +struct particle +{ + entity e; + float ttl; + + static particle& make_particle(entity e, float ttl) + { + return particles.emplace_back(e, ttl); + } + static void move_all(float dt) + { + for (auto& p : particles) + { + p.ttl -= dt; + p.e.p.move(dt); + } + std::erase_if(particles, [](auto& p) { return p.ttl <= 0; }); + } + static void draw_all() + { + for (auto& p : particles) + p.e.draw(); + } +private: + static std::vector<particle> particles; +}; + +std::vector<particle> particle::particles; + +static GLFWwindow* w; bool is_pressed(int key) { @@ -258,6 +287,12 @@ int main(int argc, char* argv[]) { float sr = sin(s.rot), cr = cos(s.rot); s.vel += coord{sr, cr} * dt; + + auto f = s; + f.ang_mom = 0; + f.vel = coord{sr, cr} * -0.5; + f.vel += coord{cr, -sr} * 0.2 * dis_p(gen); + particle::make_particle(entity(flame, f), 1); } glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); @@ -267,6 +302,7 @@ int main(int argc, char* argv[]) for (auto& b : bullets) b.p.move(dt); s.move(dt); + particle::move_all(dt); if(any_of(rocks.begin(), rocks.end(), [&s](auto& r) { return is_colliding(s, r.p); })) glfwSetWindowShouldClose(w, 1); @@ -300,6 +336,7 @@ int main(int argc, char* argv[]) bang: continue; } + particle::draw_all(); for (auto& r : rocks) r.draw(); for (auto& b : bullets)