summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-11 11:28:43 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-11 11:28:43 -0400
commitda74a60d20d46a54eda8158f01e174611d1d8962 (patch)
tree33d1aa2b90c94d0b832c20a9ba5f4c5092bb5312
parent82b14e49b96a80ffb1614c3c6ae1dd2af182cddf (diff)
downloadballs-da74a60d20d46a54eda8158f01e174611d1d8962.zip
stabilize frameratetbb
-rw-r--r--balls.cpp12
-rw-r--r--balls.h1
2 files changed, 12 insertions, 1 deletions
diff --git a/balls.cpp b/balls.cpp
index 3b5b902..939df7b 100644
--- a/balls.cpp
+++ b/balls.cpp
@@ -197,6 +197,11 @@ volume(double radius) {
void
animate(int v) {
+ clock_t tstart, elapsed;
+ unsigned int nextFrame;
+
+ tstart = clock();
+
/* Update position. */
parallel_for(size_t(0), balls.size(), [] (size_t i) {
balls[i]->v.y -=G;
@@ -216,5 +221,10 @@ animate(int v) {
});
display();
- glutTimerFunc(FRAME_TIME_MS, animate, 0);
+
+ elapsed = (clock() - tstart) / (CLOCKS_PER_SEC / MS_PER_S);
+ /* time taken to draw this frame [ms] */
+ nextFrame = (elapsed > FRAME_TIME_MS) ? 0 : FRAME_TIME_MS-elapsed;
+ /* when to start drawing the next frame [ms] */
+ glutTimerFunc(nextFrame, animate, 0);
}
diff --git a/balls.h b/balls.h
index 4d070c9..92ae8dd 100644
--- a/balls.h
+++ b/balls.h
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <iostream>
#include <math.h>
+#include <time.h>
#include <GL/glut.h>
#include <oneapi/tbb.h>