summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Melcher <github@matthiasm.com>2024-12-06 13:21:22 +0100
committerMatthias Melcher <github@matthiasm.com>2024-12-06 13:21:28 +0100
commitc61e3f63f67d4f98b8983a5661c343590ba5bf86 (patch)
treea2b7a7ee5e21f5277d3f60257cd15bf8a7d89a29
parent56756b41c298d2c342e7052b1486b6279087edc6 (diff)
glpuzzle: fixes timer and trackball max speed
Using 50fps instead of maximum render speed. Trackball limited to non-head-spinning speeds.
-rw-r--r--test/glpuzzle.cxx41
-rw-r--r--test/trackball.c5
2 files changed, 31 insertions, 15 deletions
diff --git a/test/glpuzzle.cxx b/test/glpuzzle.cxx
index f6d1c529e..f5a4a0fbe 100644
--- a/test/glpuzzle.cxx
+++ b/test/glpuzzle.cxx
@@ -84,7 +84,7 @@ static unsigned char colors[PIECES + 1][3] =
};
void changeState(void);
-void animate(void);
+void animate(int);
static struct puzzle *hashtable[HASHSIZE];
static struct puzzle *startPuzzle;
@@ -1123,6 +1123,9 @@ static int spinning;
static int enable_spinning = 1;
static float lastquat[4];
static int sel_piece;
+static int timer_active = 0; // restart another rimer at the end of `animate`
+static int timer_pending = 0; // a timer is waiting to be triggered
+static int timer_delay = 20; // timeout in msec (20ms = 50 frames per second)
static void
Reshape(int width, int height)
@@ -1140,11 +1143,11 @@ toggleSolve(void)
if (solving) {
freeSolutions();
solving = 0;
- glutChangeToMenuEntry(1, (char *)"Solving", 1);
+ glutChangeToMenuEntry(2, (char *)"Solve", 2);
glutSetWindowTitle((char *)"glpuzzle");
movingPiece = 0;
} else {
- glutChangeToMenuEntry(1, (char *)"Stop solving", 1);
+ glutChangeToMenuEntry(2, (char *)"Stop solving", 2);
glutSetWindowTitle((char *)"Solving...");
if (solvePuzzle()) {
solving = 1;
@@ -1158,7 +1161,10 @@ void reset_position(void)
{
spinning = 0;
trackball(curquat, 0.0, 0.0, 0.0, 0.0); // reset position
- glutIdleFunc(animate);
+ if (!timer_pending) {
+ timer_pending = 1;
+ glutTimerFunc(timer_delay, animate, 0);
+ }
}
void reset(void)
@@ -1167,7 +1173,7 @@ void reset(void)
if (solving) {
freeSolutions();
solving = 0;
- glutChangeToMenuEntry(1, (char *)"Solving", 1);
+ glutChangeToMenuEntry(2, (char *)"Solve", 2);
glutSetWindowTitle((char *)"glpuzzle");
movingPiece = 0;
changeState();
@@ -1195,7 +1201,7 @@ keyboard(unsigned char c, int x, int y)
if (solving) {
freeSolutions();
solving = 0;
- glutChangeToMenuEntry(1, (char *)"Solving", 1);
+ glutChangeToMenuEntry(2, (char *)"Solve", 2);
glutSetWindowTitle((char *)"glpuzzle");
movingPiece = 0;
changeState();
@@ -1270,7 +1276,7 @@ mouse(int b, int s, int x, int y)
if (solving) {
freeSolutions();
solving = 0;
- glutChangeToMenuEntry(1, (char *)"Solving", 1);
+ glutChangeToMenuEntry(2, (char *)"Solve", 2);
glutSetWindowTitle((char *)"glpuzzle");
movingPiece = 0;
}
@@ -1303,8 +1309,9 @@ mouse(int b, int s, int x, int y)
}
void
-animate(void)
+animate(int)
{
+ timer_pending = 0;
if (spinning) {
add_quats(lastquat, curquat, curquat);
}
@@ -1312,12 +1319,16 @@ animate(void)
if (solving) {
if (!continueSolving()) {
solving = 0;
- glutChangeToMenuEntry(1, (char *)"Solving", 1);
+ glutChangeToMenuEntry(2, (char *)"Solve", 2);
glutSetWindowTitle((char *)"glpuzzle");
}
}
if ((!solving && !spinning) || !visible) {
- glutIdleFunc(NULL);
+ timer_active = 0;
+ }
+ if (timer_active && !timer_pending) {
+ timer_pending = 1;
+ glutTimerFunc(timer_delay, animate, 0);
}
}
@@ -1326,12 +1337,16 @@ changeState(void)
{
if (visible) {
if (!solving && !spinning) {
- glutIdleFunc(NULL);
+ timer_active = 0;
} else {
- glutIdleFunc(animate);
+ timer_active = 1;
+ if (!timer_pending) {
+ timer_pending = 1;
+ glutTimerFunc(timer_delay, animate, 0);
+ }
}
} else {
- glutIdleFunc(NULL);
+ timer_active = 0;
}
}
diff --git a/test/trackball.c b/test/trackball.c
index 4e6086893..7876bd541 100644
--- a/test/trackball.c
+++ b/test/trackball.c
@@ -66,6 +66,7 @@
*/
static float tb_project_to_sphere(float, float, float);
static void normalize_quat(float [4]);
+static float max_velocity = 0.1;
void
vzero(float *v)
@@ -192,8 +193,8 @@ trackball(float q[4], float p1x, float p1y, float p2x, float p2y)
/*
* Avoid problems with out-of-control values...
*/
- if (t > 1.0) t = 1.0;
- if (t < -1.0) t = -1.0;
+ if (t > max_velocity) t = max_velocity;
+ if (t < -max_velocity) t = -max_velocity;
phi = float(2.0 * asin(t));
axis_to_quat(a,phi,q);