commit f0b3d10ed4c48f7051f94d75737b337f786d6fd5
parent 6c9c48853e5012285bfd0d04094d9c2c948db23a
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Sat, 2 Apr 2022 14:43:03 +0100
gl-asteroids: fix long-keypresses not being detected and added bullet graphic
Diffstat:
1 file changed, 22 insertions(+), 10 deletions(-)
diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp
@@ -48,8 +48,12 @@ coord const rockv4[] = {{0, 0}, {1, 0}, {.6,.4}, {0, .5}, {-.3,.6}, {-1, 0}, {-.
color const rockc[] = {{.5,.5,.5}, {.3,.3,.3}};
+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}};
+
visual ship{GL_TRIANGLES, shipv, shipc};
visual flame{GL_TRIANGLES, flamev, flamec};
+visual bullet(GL_TRIANGLE_STRIP, bulletv, bulletc);
visual rock1{GL_TRIANGLE_FAN, rockv1, rockc};
visual rock2{GL_TRIANGLE_FAN, rockv2, rockc};
visual rock3{GL_TRIANGLE_FAN, rockv3, rockc};
@@ -164,6 +168,13 @@ struct entity
}
};
+GLFWwindow* w;
+
+bool is_pressed(int key)
+{
+ return glfwGetKey(w, key) != GLFW_RELEASE;
+}
+
int main(int argc, char* argv[])
{
unsigned score = 0;
@@ -178,10 +189,10 @@ int main(int argc, char* argv[])
glfwInit();
- GLFWwindow* w = glfwCreateWindow(926, 926, argv[0], NULL, NULL);
+ w = glfwCreateWindow(926, 926, argv[0], NULL, NULL);
glfwMakeContextCurrent(w);
-
+
float last_t = glfwGetTime();
std::vector<entity> rocks;
@@ -190,7 +201,7 @@ int main(int argc, char* argv[])
float rock_time = 1, bullet_time = 0;
physics s{{}, {}, 0.f, 0.f, .06f};
- while (!glfwWindowShouldClose(w) && glfwGetKey(w, GLFW_KEY_ESCAPE) != GLFW_PRESS)
+ while (!glfwWindowShouldClose(w) && not is_pressed(GLFW_KEY_ESCAPE))
{
float t = glfwGetTime();
float dt = t - last_t;
@@ -219,21 +230,22 @@ int main(int argc, char* argv[])
rocks.emplace_back(*rock_types[rtype], physics{p, v, 0, dis_a(gen), dis_s(gen)});
}
- if (glfwGetKey(w, GLFW_KEY_SPACE) == GLFW_PRESS && bullet_time < t)
+ if (is_pressed(GLFW_KEY_SPACE) && bullet_time < t)
{
bullet_time = t + .5f;
- bullets.emplace_back(ship, s);
+ bullets.emplace_back(bullet, s);
auto& b = bullets.back().p;
- b.scale /= 3;
+ b.ang_mom = 0;
+ b.scale /= 5;
b.vel += {(float)sin(b.rot), (float)cos(b.rot)};
}
last_t = t;
- int kl = glfwGetKey(w, GLFW_KEY_LEFT) == GLFW_PRESS;
- int kr = glfwGetKey(w, GLFW_KEY_RIGHT) == GLFW_PRESS;
+ int kl = is_pressed(GLFW_KEY_LEFT);
+ int kr = is_pressed(GLFW_KEY_RIGHT);
if (kl ^ kr)
s.ang_mom = kr ? 2 : -2;
@@ -241,11 +253,11 @@ int main(int argc, char* argv[])
s.ang_mom = 0;
- bool up = glfwGetKey(w, GLFW_KEY_UP) == GLFW_PRESS;
+ bool up = is_pressed(GLFW_KEY_UP);
if (up)
{
float sr = sin(s.rot), cr = cos(s.rot);
- s.vel += coord{sr, cr} * dt;
+ s.vel += coord{sr, cr} * dt;
}
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);