examples

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

commit 97bf4e4f5c2f09ee6f2e843aed1b27aed306bcb0
parent 665a10412fc08fdc81263695f3bb86fda96eb88e
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Sun,  3 Apr 2022 14:09:15 +0100

gl-asteroids: Tidy up a bit and add collision debris

Diffstat:
Msrc/gl-asteroids.cpp | 29+++++++++++++++++++++++------
1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -46,10 +46,10 @@ coord const rockv2[] = {{0, 0}, {1, 0}, {.7,.7}, {0, 1}, {-1, 0}, {-.9,-.9}, {0, coord const rockv3[] = {{0, 0}, {1, 0}, {.7,.4}, {0, 1}, {-.8,.6}, {-1, 0}, {-.6,-.5}, {0, -1}, {1, 0}}; coord const rockv4[] = {{0, 0}, {1, 0}, {.6,.4}, {0, .5}, {-.3,.6}, {-1, 0}, {-.6,-.5}, {0, -1}, {1, 0}}; -color const rockc[] = {{.5,.5,.5}, {.3,.3,.3}}; +color const rockc[] = {{.5,.5,.5}, {.2,.2,.2}}; coord const bulletv[] = {{0, 4}, {1, 1}, {-1, 1}, {1, -4}, {-1, -4}}; -color const bulletc[] = {{.0,.8,.0}, {.0,.6,.0}, {.0,.6,.0}, {.0,.4,.0}}; +color const bulletc[] = {{.0,.8,.0}, {.0,.4,.0}, {.0,.4,.0}, {.0,.0,.0}}; visual ship{GL_TRIANGLES, shipv, shipc}; visual flame{GL_TRIANGLES, flamev, flamec}; @@ -252,6 +252,8 @@ int main(int argc, char* argv[]) if (dir & 2) v.y = -v.y; + v *= log(t+1); + coord p = {dis_p(gen), 1}; if (dir & 3) std::swap(p.x, p.y); @@ -261,7 +263,7 @@ int main(int argc, char* argv[]) if (is_pressed(GLFW_KEY_SPACE) && bullet_time < t) { - bullet_time = t + .5f; + bullet_time = t + .3f; bullets.emplace_back(bullet, s); auto& b = bullets.back().p; @@ -281,7 +283,6 @@ int main(int argc, char* argv[]) else s.ang_mom = 0; - bool up = is_pressed(GLFW_KEY_UP); if (up) { @@ -294,8 +295,8 @@ int main(int argc, char* argv[]) 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); + + // resolve motion for (auto& r : rocks) r.p.move(dt); @@ -304,6 +305,8 @@ int main(int argc, char* argv[]) s.move(dt); particle::move_all(dt); + // handle collisions + if(any_of(rocks.begin(), rocks.end(), [&s](auto& r) { return is_colliding(s, r.p); })) glfwSetWindowShouldClose(w, 1); @@ -314,6 +317,15 @@ int main(int argc, char* argv[]) { if (is_colliding(r->p, b->p)) { + for (unsigned j = 0; j < 7; ++j) + { + entity debris = *r; + debris.p.vel.x += .3 * dis_p(gen); + debris.p.vel.y += .3 * dis_p(gen); + debris.p.scale /= 5; + particle::make_particle(debris, .7); + } + if (r->p.scale > .1f) { r->p.vel *= 1.5f; @@ -336,6 +348,11 @@ int main(int argc, char* argv[]) bang: continue; } + // Draw scene + + glClearColor(0,0,0,1); + glClear(GL_COLOR_BUFFER_BIT); + particle::draw_all(); for (auto& r : rocks) r.draw();