examples

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

commit 81027f9fa9973e5b56e10200396492372a294d0f
parent 01a25f14bc50f21e384029650521babdce387a09
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Thu, 28 Apr 2022 19:23:31 +0100

gl-asteroids: Stop using single letter member/global names

Diffstat:
Msrc/gl-asteroids.cpp | 104++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 52 insertions(+), 52 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -80,8 +80,8 @@ struct color struct visual { GLint mode; - std::span<coord const> v; - std::span<color const> c; + std::span<coord const> verts; + std::span<color const> cols; }; coord const shipv[] = {{0, 2}, {1, -1}, {-1, -1}, {1,-1}, {.5,0}, {.5,0}}; @@ -222,11 +222,11 @@ void draw(visual const& v, physics const& p) { glBegin(v.mode); - for (unsigned i = 0; i < v.v.size(); ++i) + for (unsigned i = 0; i < v.verts.size(); ++i) { - if (i < v.c.size()) - glColor(v.c[i]); - glCoord(rotate(v.v[i] * p.scale, p.rot) + p.pos); + if (i < v.cols.size()) + glColor(v.cols[i]); + glCoord(rotate(v.verts[i] * p.scale, p.rot) + p.pos); } glEnd(); @@ -245,24 +245,24 @@ void draw_wrapped(visual const& v, physics p) struct entity { - visual const* v; - physics p; + visual const* vis; + physics phys; entity() = delete; entity(visual const& _v, physics _p) - : v(&_v) - , p(_p) + : vis(&_v) + , phys(_p) {} void draw() { - ::draw(*v, p); + ::draw(*vis, phys); } }; struct particle { - entity e; + entity ent; float ttl; static particle& make_particle(entity e, float ttl) @@ -274,15 +274,15 @@ struct particle for (auto& p : particles) { p.ttl -= dt; - p.e.p.move(dt); + p.ent.phys.move(dt); } std::erase_if(particles, [](auto& p) { return p.ttl <= 0; }); - std::erase_if(particles, [](auto const& p) { return p.e.p.left_arena(); }); + std::erase_if(particles, [](auto const& p) { return p.ent.phys.left_arena(); }); } static void draw_all() { for (auto& p : particles) - p.e.draw(); + p.ent.draw(); } private: static std::vector<particle> particles; @@ -302,7 +302,7 @@ std::vector<entity> bullets; std::optional<entity> powerups; std::function<void(void)> powerup_cb; -physics s{{}, {}, 0.f, 0.f, .06f}; +physics ship_phys{{}, {}, 0.f, 0.f, .06f}; bool up = false; void draw_scene(float t) @@ -326,11 +326,11 @@ void draw_scene(float t) if (up) { - auto f = s; + auto f = ship_phys; f.scale *= 1 + 0.1 * sin(t * 49) * sin(t * 9); draw_wrapped(flame, f); } - draw_wrapped(ship, s); + draw_wrapped(ship, ship_phys); glfwSwapBuffers(w); glfwPollEvents(); @@ -474,14 +474,14 @@ int main(int argc, char* argv[]) --ammo; bullet_time = t + 1/firerate; - bullets.emplace_back(bullet, s); - auto& b = bullets.back().p; + bullets.emplace_back(bullet, ship_phys); + auto& b = bullets.back().phys; b.ang_mom = 0; b.scale /= 1.25; auto vel = coord{(float)sin(b.rot), (float)cos(b.rot)} * bullet_speed; b.vel += vel; - s.vel -= vel * .05f; + ship_phys.vel -= vel * .05f; } last_t = t; @@ -490,29 +490,29 @@ int main(int argc, char* argv[]) int kr = is_pressed(GLFW_KEY_RIGHT); if (kl ^ kr) - s.ang_mom = (kr ? 1 : -1) * (up ? 3 : 2); + ship_phys.ang_mom = (kr ? 1 : -1) * (up ? 3 : 2); else - s.ang_mom = 0; + ship_phys.ang_mom = 0; up = is_pressed(GLFW_KEY_UP); if (up) { - float sr = sin(s.rot), cr = cos(s.rot); - s.vel += coord{sr, cr} * dt; + float sr = sin(ship_phys.rot), cr = cos(ship_phys.rot); + ship_phys.vel += coord{sr, cr} * dt; } // resolve motion for (auto& r : rocks) - r.p.move(dt); + r.phys.move(dt); for (auto& b : bullets) - b.p.move(dt); + b.phys.move(dt); if (powerups) - powerups->p.move(dt); + powerups->phys.move(dt); - s.move(dt); - s.wrap_pos(); + ship_phys.move(dt); + ship_phys.wrap_pos(); particle::move_all(dt); // handle collisions @@ -522,20 +522,20 @@ int main(int argc, char* argv[]) auto r = rocks.begin() + i; for (auto b = bullets.begin(); b < bullets.end();) { - if (is_colliding(r->p, b->p)) + if (is_colliding(r->phys, b->phys)) { play_sample(square); 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 = dis_ds(gen); + 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); } - if (r->p.scale > .13f) + if (r->phys.scale > .13f) { coord split{dis_v(gen), dis_v(gen)}; @@ -543,16 +543,16 @@ int main(int argc, char* argv[]) float sf = dis_z(gen); - r->p.scale *= sf; - nr.p.scale *= .9 - sf; + r->phys.scale *= sf; + nr.phys.scale *= .9 - sf; - r->v = &random_rock_visual(); - nr.v = &random_rock_visual(); + r->vis = &random_rock_visual(); + nr.vis = &random_rock_visual(); - r->p.vel += 2*split; - nr.p.vel -= split; + r->phys.vel += 2*split; + nr.phys.vel -= split; - nr.p.ang_mom *= -1.5f; + nr.phys.ang_mom *= -1.5f; rocks.push_back(nr); ++i; @@ -574,7 +574,7 @@ int main(int argc, char* argv[]) { for (auto b = bullets.begin(); b < bullets.end(); ++b) { - if (not is_colliding(b->p, powerups->p)) + if (not is_colliding(b->phys, powerups->phys)) continue; play_sample(saw); @@ -582,9 +582,9 @@ int main(int argc, char* argv[]) for (unsigned j = 0; j < 4; ++j) { entity debris = *powerups; - debris.p.vel.x += .3 * dis_p(gen); - debris.p.vel.y += .3 * dis_p(gen); - debris.p.scale *= 1.5 * dis_s(gen); + 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); } powerups.reset(); @@ -592,7 +592,7 @@ int main(int argc, char* argv[]) bullets.erase(b); break; } - if (is_colliding(s, powerups->p)) + if (is_colliding(ship_phys, powerups->phys)) { play_sample(blip); powerup_cb(); @@ -601,7 +601,7 @@ int main(int argc, char* argv[]) } } - if(any_of(rocks.begin(), rocks.end(), [](auto& r) { return is_colliding(s, r.p); })) + if(any_of(rocks.begin(), rocks.end(), [](auto& r) { return is_colliding(ship_phys, r.phys); })) { play_sample(bang); using namespace std::chrono_literals; @@ -613,10 +613,10 @@ int main(int argc, char* argv[]) break; } - score -= std::erase_if(bullets, [](auto const& e) { return e.p.left_arena(); }); - score -= std::erase_if(rocks, [](auto const& e) { return e.p.left_arena(); }); + score -= std::erase_if(bullets, [](auto const& e) { return e.phys.left_arena(); }); + score -= std::erase_if(rocks, [](auto const& e) { return e.phys.left_arena(); }); - if (powerups && powerups->p.left_arena()) + if (powerups && powerups->phys.left_arena()) { powerups.reset(); --score;