diff options
| author | kkard2 <[email protected]> | 2025-09-24 16:03:54 +0200 |
|---|---|---|
| committer | kkard2 <[email protected]> | 2025-09-24 16:03:54 +0200 |
| commit | e66e6c79886b77d4b7f1a72bbb5947aec0a46a07 (patch) | |
| tree | f21025a93322d53103332767bd0efacae70a4007 | |
| parent | 83413173c00e0387fbf05cc3d764fcd6a43bf991 (diff) | |
ngl i don't think this is correct
| -rw-r--r-- | examples/platform/platform_x11.c | 64 | ||||
| -rwxr-xr-x | run_example | 2 |
2 files changed, 62 insertions, 4 deletions
diff --git a/examples/platform/platform_x11.c b/examples/platform/platform_x11.c index 70a827e..4e26165 100644 --- a/examples/platform/platform_x11.c +++ b/examples/platform/platform_x11.c @@ -3,6 +3,7 @@ #include <X11/Xlib.h> #include <X11/Xutil.h> +#include <bits/time.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> @@ -17,8 +18,10 @@ #include "../example.h" -#define DEFAULT_WIDTH 256 -#define DEFAULT_HEIGHT 256 +#define DEFAULT_WIDTH 1280 +#define DEFAULT_HEIGHT 720 + +#define TARGET_FRAME_TIME_NS 16666667L static size_t pixels_buffer_count = 0; static sponge_Texture canvas = { 0 }; @@ -26,6 +29,28 @@ static float *depths = NULL; static Display *dpy; static XImage *ximg = NULL; +void timespec_add_ns(struct timespec *inout_t, long ns) { + inout_t->tv_nsec += ns; + while (inout_t->tv_nsec >= 1000000000L) { + inout_t->tv_nsec -= 1000000000L; + inout_t->tv_sec += 1; + } +} + +long long timespec_sub_ns(struct timespec *a, struct timespec *b) { + long long sec = a->tv_sec - b->tv_sec; + long long result = a->tv_nsec - b->tv_nsec; + result += sec * 1000000000L; + return result; +} + +int timespec_cmp(struct timespec *a, struct timespec *b) { + if (a->tv_sec < b->tv_sec) return -1; + if (a->tv_sec > b->tv_sec) return 1; + if (a->tv_nsec < b->tv_nsec) return -1; + if (a->tv_nsec > b->tv_nsec) return 1; + return 0; +} void init_canvas(uint32_t width, uint32_t height) { if (ximg) { @@ -104,12 +129,37 @@ int main() { init_canvas(DEFAULT_WIDTH, DEFAULT_HEIGHT); init(); + struct timespec ts_next; + if (clock_gettime(CLOCK_MONOTONIC, &ts_next)) { + fprintf(stderr, "clock_gettime fail\n"); + return 1; + } + timespec_add_ns(&ts_next, TARGET_FRAME_TIME_NS); + int running = 1; while (running) { + if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_next, NULL)) { + fprintf(stderr, "clock_nanosleep fail\n"); + return 1; + } + + struct timespec ts_now; + if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) { + fprintf(stderr, "clock_gettime fail\n"); + return 1; + } + if (sponge_texture_valid(canvas)) { draw_frame_3d(canvas, depths); + struct timespec ts_draw; + if (clock_gettime(CLOCK_MONOTONIC, &ts_draw)) { + fprintf(stderr, "clock_gettime fail\n"); + return 1; + } + long long draw_time = timespec_sub_ns(&ts_draw, &ts_now); + fprintf(stderr, "draw time: %3lld.%03lldms\n", draw_time / 1000000, (draw_time % 1000000) / 1000); + XPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, canvas.width, canvas.height); - usleep(16000); } while (XPending(dpy)) { @@ -132,6 +182,14 @@ int main() { break; } } + + while (timespec_cmp(&ts_next, &ts_now) <= 0) { + timespec_add_ns(&ts_next, TARGET_FRAME_TIME_NS); + if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) { + fprintf(stderr, "clock_gettime fail\n"); + return 1; + } + } } if (ximg) { diff --git a/run_example b/run_example index 80d6c10..046f066 100755 --- a/run_example +++ b/run_example @@ -1,3 +1,3 @@ #!/bin/bash cc -o example examples/platform/platform_x11.c examples/$1 \ - -lX11 -lm -Wall -Wextra -Wno-type-limits -Wno-pointer-sign -g && ./example + -lX11 -lm -Wall -Wextra -Wno-type-limits -Wno-pointer-sign -g -O3 -march=native && ./example |
