examples

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

commit 3f1a9b58f4d9a047f668e47c1f161b5af4f5ae3a
parent ca9406c1b018edf610a767c77a0d22bec8ce2bac
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Sun,  3 Apr 2022 14:17:43 +0100

gl-asteroids: Start rocks offscreen and draw the ship 5 times to avoid popping

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

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -123,8 +123,15 @@ struct physics void move(float dt) { pos += vel * dt; - pos.x = wrap(pos.x, -1, 1); - pos.y = wrap(pos.y, -1, 1); + if (pos.x > 1 && vel.x > 0) + pos.x -= 2; + else if (pos.x < -1 && vel.x < 0) + pos.x += 2; + + if (pos.y > 1 && vel.y > 0) + pos.y -= 2; + else if (pos.y < -1 && vel.y < 0) + pos.y += 2; rot += ang_mom * dt; } }; @@ -139,19 +146,26 @@ bool is_colliding(physics const& a, physics const& b) void draw(visual const& v, physics const& p) { - static coord const offsets[] = {{0,0}, {2,0}, {0,2}, {-2,0}, {0,-2}}; - for (auto offset : offsets) + glBegin(v.mode); + + for (unsigned i = 0; i < v.v.size(); ++i) { - glBegin(v.mode); + if (i < v.c.size()) + glColor(v.c[i]); + glCoord(rotate(v.v[i] * p.scale, p.rot) + p.pos); + } - for (unsigned i = 0; i < v.v.size(); ++i) - { - if (i < v.c.size()) - glColor(v.c[i]); - glCoord(rotate(v.v[i] * p.scale, p.rot) + p.pos + offset); - } + glEnd(); +} - glEnd(); +void draw_wrapped(visual const& v, physics p) +{ + static coord const offsets[] = {{0,0}, {2,0}, {-2,2}, {-2,-2}, {2,-2}}; + + for (auto o : offsets) + { + p.pos += o; + draw(v, p); } } @@ -250,19 +264,26 @@ int main(int argc, char* argv[]) int dir = dis_d(gen); + auto scale = dis_s(gen); + coord p = {dis_p(gen), -1 - scale}; coord v(dis_v(gen), dis_v(gen)); + + v *= log(t+1); + if (dir & 1) v.x = -v.x; if (dir & 2) + { + p.y = -p.y; v.y = -v.y; - - v *= log(t+1); - - coord p = {dis_p(gen), 1}; - if (dir & 3) + } + if (dir & 4) + { std::swap(p.x, p.y); + std::swap(v.x, v.y); + } - rocks.emplace_back(*rock_types[rtype], physics{p, v, 0, dis_a(gen), dis_s(gen)}); + rocks.emplace_back(*rock_types[rtype], physics{p, v, 0, dis_a(gen), scale}); } if (is_pressed(GLFW_KEY_SPACE) && bullet_time < t) @@ -363,7 +384,7 @@ int main(int argc, char* argv[]) for (auto& b : bullets) b.draw(); - draw(ship, s); + draw_wrapped(ship, s); if (up) draw(flame, s);