examples

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

commit 70a7ce89334ef1a40ece89c42834308be751d7b5
parent 44dc0f57e75e875fc0907b5e0afd6d936ca682d1
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Wed, 10 Aug 2022 16:50:57 +0100

gl-asteroids: Add acceleration powerup and make window fit

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

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -64,6 +64,8 @@ std::jthread sound(sound_routine); /// GRAPHICS +constexpr int window_size = 700; + struct coord { coord() = default; @@ -104,6 +106,7 @@ coord const rockv4[] = {{0, 0}, {1, 0}, {.6,.4}, {0, .5}, {-.3,.6}, {-1, 0}, {-. color const rockc[] = {{.5,.5,.5}, {.2,.2,.2}}; color const powerupcb[] = {{.8,.4,.2}, {.4,.2,.1}}; color const powerupcf[] = {{.2,.4,.8}, {.1,.2,.4}}; +color const powerupca[] = {{.8,.8,.2}, {.4,.4,.1}}; coord const bulletv[] = {{0, 1}, {.25, .25}, {-.25, .25}, {.25, -1}, {-.25, -1}}; color const bulletc[] = {{.0,.8,.0}, {.0,.4,.0}, {.0,.4,.0}, {.0,.0,.0}}; @@ -119,6 +122,7 @@ visual rock3{GL_TRIANGLE_FAN, rockv3, rockc}; visual rock4{GL_TRIANGLE_FAN, rockv4, rockc}; visual powerup_firerate{GL_TRIANGLE_FAN, rockv1, powerupcf}; visual powerup_bulletspeed{GL_TRIANGLE_FAN, rockv1, powerupcb}; +visual powerup_acceleration{GL_TRIANGLE_FAN, rockv1, powerupca}; std::array<visual const*, 4> rock_types = {&rock1, &rock2, &rock3, &rock4}; float wrap(float n, float min, float max) @@ -395,7 +399,7 @@ visual const& random_rock_visual(void) int main(int, char* argv[]) { signed score = 0; - float firerate = 2, bullet_speed = 1; + float firerate = 2, bullet_speed = 1, acceleration = 0.8; std::uniform_real_distribution<float> dis_p(-1,1); std::uniform_real_distribution<float> dis_s(0.05,0.25); @@ -424,7 +428,10 @@ int main(int, char* argv[]) glfwInit(); - w = glfwCreateWindow(926, 926, argv[0], NULL, NULL); + w = glfwCreateWindow(window_size, window_size, argv[0], NULL, NULL); + int wx, wy; + glfwGetFramebufferSize(w, &wx, &wy); + std::cout << "x: " << wx << " y: " << wy << '\n'; glfwMakeContextCurrent(w); @@ -488,7 +495,7 @@ int main(int, char* argv[]) std::function<void(void)> cb; static int type; - switch (type++ % 2) + switch (type++ % 3) { case 0: vis = &powerup_bulletspeed; @@ -498,6 +505,10 @@ int main(int, char* argv[]) vis = &powerup_firerate; cb = [&bullet_speed](){ bullet_speed += .2f; }; break; + case 2: + vis = &powerup_acceleration; + cb = [&acceleration](){ acceleration += .2f; }; + break; } powerups.emplace(entity{*vis, physics{p, v, 0, 0, scale}}, cb); @@ -538,7 +549,7 @@ int main(int, char* argv[]) up = is_pressed(GLFW_KEY_UP); if (up) - ship_phys.vel += coord_from_rotation(ship_phys.rot) * dt; + ship_phys.vel += coord_from_rotation(ship_phys.rot) * dt * acceleration; // resolve motion