From f9aa6acb7a9456b9ce6dbe5a9e8b819929e96672 Mon Sep 17 00:00:00 2001 From: Carlos Aragones Date: Wed, 28 Apr 2021 11:07:25 +0200 Subject: [PATCH] try to accurate software vsync --- src/macosx/MacMiniFB.m | 24 +++++++++++++----------- src/wayland/WaylandMiniFB.c | 22 ++++++++++++---------- src/windows/WinMiniFB.c | 24 ++++++++++++------------ src/x11/X11MiniFB.c | 22 +++++++++++----------- 4 files changed, 48 insertions(+), 44 deletions(-) diff --git a/src/macosx/MacMiniFB.m b/src/macosx/MacMiniFB.m index c6dc272..7e40040 100644 --- a/src/macosx/MacMiniFB.m +++ b/src/macosx/MacMiniFB.m @@ -321,18 +321,8 @@ mfb_wait_sync(struct mfb_window *window) { double current; uint32_t millis = 1; while(1) { - event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES]; - if (event) { - [NSApp sendEvent:event]; - } - - if(window_data->close) { - destroy_window_data(window_data); - return false; - } - current = mfb_timer_now(window_data_osx->timer); - if (current >= g_time_for_frame) { + if (current >= g_time_for_frame * 0.96) { mfb_timer_reset(window_data_osx->timer); return true; } @@ -342,6 +332,18 @@ mfb_wait_sync(struct mfb_window *window) { usleep(millis * 1000); //sched_yield(); + + if(millis == 1) { + event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES]; + if (event) { + [NSApp sendEvent:event]; + + if(window_data->close) { + destroy_window_data(window_data); + return false; + } + } + } } } diff --git a/src/wayland/WaylandMiniFB.c b/src/wayland/WaylandMiniFB.c index eb0a3a6..120eee8 100644 --- a/src/wayland/WaylandMiniFB.c +++ b/src/wayland/WaylandMiniFB.c @@ -817,17 +817,8 @@ mfb_wait_sync(struct mfb_window *window) { double current; uint32_t millis = 1; while(1) { - if (wl_display_dispatch_pending(window_data_way->display) == -1) { - return false; - } - - if(window_data->close) { - destroy_window_data(window_data); - return false; - } - current = mfb_timer_now(window_data_way->timer); - if (current >= g_time_for_frame) { + if (current >= g_time_for_frame * 0.96) { mfb_timer_reset(window_data_way->timer); return true; } @@ -837,6 +828,17 @@ mfb_wait_sync(struct mfb_window *window) { usleep(millis * 1000); //sched_yield(); + + if(millis == 1) { + if (wl_display_dispatch_pending(window_data_way->display) == -1) { + return false; + } + + if(window_data->close) { + destroy_window_data(window_data); + return false; + } + } } return true; diff --git a/src/windows/WinMiniFB.c b/src/windows/WinMiniFB.c index 8f2d002..6be9dc2 100644 --- a/src/windows/WinMiniFB.c +++ b/src/windows/WinMiniFB.c @@ -666,18 +666,8 @@ mfb_wait_sync(struct mfb_window *window) { double current; uint32_t millis = 1; while (1) { - if(PeekMessage(&msg, window_data_win->window, 0, 0, PM_REMOVE)) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - - if (window_data->close) { - destroy_window_data(window_data); - return false; - } - - current = mfb_timer_now(window_data_win->timer);; - if (current >= g_time_for_frame) { + current = mfb_timer_now(window_data_win->timer); + if (current >= g_time_for_frame * 0.96) { mfb_timer_reset(window_data_win->timer); return true; } @@ -686,6 +676,16 @@ mfb_wait_sync(struct mfb_window *window) { } Sleep(millis); + + if(millis == 1 && PeekMessage(&msg, window_data_win->window, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + + if (window_data->close) { + destroy_window_data(window_data); + return false; + } + } } return true; diff --git a/src/x11/X11MiniFB.c b/src/x11/X11MiniFB.c index 4593c9b..5bae00a 100644 --- a/src/x11/X11MiniFB.c +++ b/src/x11/X11MiniFB.c @@ -452,18 +452,8 @@ mfb_wait_sync(struct mfb_window *window) { double current; uint32_t millis = 1; while(1) { - if(XEventsQueued(window_data_x11->display, QueuedAlready) > 0) { - XNextEvent(window_data_x11->display, &event); - processEvent(window_data, &event); - } - - if(window_data->close) { - destroy_window_data(window_data); - return false; - } - current = mfb_timer_now(window_data_x11->timer); - if (current >= g_time_for_frame) { + if (current >= g_time_for_frame * 0.96) { mfb_timer_reset(window_data_x11->timer); return true; } @@ -473,6 +463,16 @@ mfb_wait_sync(struct mfb_window *window) { usleep(millis * 1000); //sched_yield(); + + if(millis == 1 && XEventsQueued(window_data_x11->display, QueuedAlready) > 0) { + XNextEvent(window_data_x11->display, &event); + processEvent(window_data, &event); + + if(window_data->close) { + destroy_window_data(window_data); + return false; + } + } } return true;