summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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>