examples

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

commit 317ff4eee92b6038d53305429b2cce648ad633e1
parent 6c3177246d046388bec96ca34516ec117c221bce
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Mon,  4 Apr 2022 23:34:57 +0100

gl-asteroids: Improve rock randomness

Diffstat:
Msrc/gl-asteroids.cpp | 22+++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -258,6 +258,16 @@ void draw_scene(float t) glfwPollEvents(); } +visual const& random_rock_visual(void) +{ + static unsigned rtype; + + ++rtype; + rtype %= rock_types.size(); + + return *rock_types[rtype]; +} + int main(int argc, char* argv[]) { unsigned score = 0; @@ -292,11 +302,6 @@ int main(int argc, char* argv[]) { rock_time += (rocks.size() < 3 ? 2 : 6) / log(t+1); - static unsigned rtype; - - ++rtype; - rtype %= rock_types.size(); - int dir = dis_d(gen); auto scale = dis_s(gen); @@ -318,7 +323,7 @@ int main(int argc, char* argv[]) std::swap(v.x, v.y); } - rocks.emplace_back(*rock_types[rtype], physics{p, v, 0, dis_a(gen), scale}); + rocks.emplace_back(random_rock_visual(), physics{p, v, 0, dis_a(gen), scale}); } if (is_pressed(GLFW_KEY_SPACE) && bullet_time < t) @@ -372,19 +377,22 @@ int main(int argc, char* argv[]) for (unsigned j = 0; j < 7; ++j) { entity debris = *r; + debris.v = &random_rock_visual(); debris.p.vel.x += .3 * dis_p(gen); debris.p.vel.y += .3 * dis_p(gen); - debris.p.scale /= 5; + debris.p.scale *= 1.5 * dis_s(gen); particle::make_particle(debris, .7); } if (r->p.scale > .1f) { + r->v = &random_rock_visual(); r->p.vel *= 1.5f; r->p.scale /= 2; rocks.push_back(*r); rocks.back().p.vel *= -1; rocks.back().p.ang_mom *= -1.5f; + rocks.back().v = &random_rock_visual(); ++i; } else