examples

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

commit 6c3177246d046388bec96ca34516ec117c221bce
parent ea51d2f712d3f95513f8c928fb52b6e5f01f9969
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Mon,  4 Apr 2022 21:30:44 +0100

gl-asteroids: Add stars and improve flame effect

Diffstat:
Msrc/gl-asteroids.cpp | 33+++++++++++++++++++++------------
1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -38,7 +38,7 @@ struct visual coord const shipv[] = {{0, 2}, {1, -1}, {-1, -1}, {1,-1}, {.5,0}, {.5,0}}; color const shipc[] = {{1,1,1}, {.5,.5,.5}}; -coord const flamev[3] = {{0, -2}, {.5, -1}, {-.5, -1}}; +coord const flamev[3] = {{0, -2.5}, {.6, -.25}, {-.6, -.25}}; color const flamec[] = {{1,0,0}, {1,1,0}}; coord const rockv1[] = {{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 0}}; @@ -51,6 +51,8 @@ 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,.4,.0}, {.0,.4,.0}, {.0,.0,.0}}; +coord starsv[64]; + visual ship{GL_TRIANGLES, shipv, shipc}; visual flame{GL_TRIANGLES, flamev, flamec}; visual bullet(GL_TRIANGLE_STRIP, bulletv, bulletc); @@ -227,20 +229,30 @@ std::vector<entity> bullets; physics s{{}, {}, 0.f, 0.f, .06f}; bool up = false; -void draw_scene(void) +void draw_scene(float t) { glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); + glColor({1,1,1}); + glBegin(GL_POINTS); + for (auto v : starsv) + glCoord(v); + glEnd(); + particle::draw_all(); for (auto& r : rocks) r.draw(); for (auto& b : bullets) b.draw(); - draw_wrapped(ship, s); if (up) - draw(flame, s); + { + auto f = s; + f.scale *= 1 + 0.1 * sin(t * 49) * sin(t * 9); + draw_wrapped(flame, f); + } + draw_wrapped(ship, s); glfwSwapBuffers(w); glfwPollEvents(); @@ -268,6 +280,9 @@ int main(int argc, char* argv[]) float rock_time = 1, bullet_time = 0; + for (auto& c : starsv) + c.x = dis_p(gen), c.y = dis_p(gen); + while (!glfwWindowShouldClose(w) && not is_pressed(GLFW_KEY_ESCAPE)) { float t = glfwGetTime(); @@ -334,12 +349,6 @@ int main(int argc, char* argv[]) { float sr = sin(s.rot), cr = cos(s.rot); s.vel += coord{sr, cr} * dt; - - auto f = s; - f.ang_mom = 0; - f.vel = coord{sr, cr} * -0.5; - f.vel += coord{cr, -sr} * 0.2 * dis_p(gen); - particle::make_particle(entity(flame, f), 1); } // resolve motion @@ -397,11 +406,11 @@ int main(int argc, char* argv[]) auto timeout = std::chrono::steady_clock::now() + 1s; std::cout << "Score: " << score << '\n'; while (std::chrono::steady_clock::now() < timeout) - draw_scene(); + draw_scene(t); break; } - draw_scene(); + draw_scene(t); } glfwDestroyWindow(w);