From e66e6c79886b77d4b7f1a72bbb5947aec0a46a07 Mon Sep 17 00:00:00 2001 From: kkard2 Date: Wed, 24 Sep 2025 16:03:54 +0200 Subject: ngl i don't think this is correct --- examples/platform/platform_x11.c | 64 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'examples') 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 #include +#include #include #include #include @@ -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) { -- cgit v1.3.1