commit 326d4061b95da2fa868e7ee26a1ee9e60f039d34
parent 270dde44832f0ae876d924dcaf8a32febbfa4e66
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 22 Dec 2022 12:00:34 +0900
fix fps
Diffstat:
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/ex2/ex2.c b/ex2/ex2.c
@@ -9,6 +9,7 @@
/* macros */
#define INIT_WIDTH 800
#define INIT_HEIGHT 600
+#define FPS 60
/* variables */
@@ -62,11 +63,16 @@ main(void)
{
int px, py;
int quit;
+ long t0, t1, dt;
+ int fps_count;
struct timespec ts;
XEvent event;
setup();
quit = 0;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t0 = ts.tv_nsec;
+ fps_count = 0;
while (!quit){
while(XPending(display) > 0){
@@ -90,7 +96,25 @@ main(void)
break;
}
}
+
+ // fix fps
+ dt = 0;
+ while (dt < 1.0 * 1000 * 1000 * 1000 / FPS){
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ t1 = ts.tv_nsec;
+ dt = t1 > t0 ? t1 - t0 : t1 - t0 + 1000 * 1000 * 1000;
+ }
+ // count fps.
+ // simple but not precise.
+ fps_count++;
+ if (t1 < t0){
+ printf("fps: %u\n", fps_count);
+ fps_count = 0;
+ }
+
clock_gettime(CLOCK_MONOTONIC, &ts);
+ t0 = ts.tv_nsec;
+
px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000));
py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000));
XClearArea(display, window,
@@ -100,10 +124,6 @@ main(void)
XFillRectangle(display, window, gc,
px, py, // position
100, 100); // width and height
-
- ts.tv_sec = 0;
- ts.tv_nsec = 10 * 1000 * 1000;
- nanosleep(&ts, NULL);
}
cleanup();