From a30d30c1abbe97d748b41ec81857fe1b53aa9366 Mon Sep 17 00:00:00 2001 From: mguinhos Date: Fri, 23 Oct 2026 17:31:27 -0300 Subject: [PATCH] Quadros a 60 fps: completa 16,7 ms em vez de dormir 14 ms fixos MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit O backend SDL dormia 14 ms em todo quadro, somados ao trabalho e à espera do vsync: o editor nunca passava de ~40 fps, por mais rápido que fosse o programa. Agora o quadro dorme só o que falta para ~16,7 ms desde o anterior (com vsync, nada). O mesmo no laço do SDL2 e na espera padrão do tinyblocks-graphics. Minecraft em blocos no editor: ~4 fps -> 60 fps. Co-Authored-By: Claude Opus 5.5 --- libraries/tinyblocks-graphics/src/tb_graphics.c | 15 +++++++++++++-- libraries/tinyblocks-sdl2/src/tb_sdl2.c | 17 +++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/libraries/tinyblocks-graphics/src/tb_graphics.c b/libraries/tinyblocks-graphics/src/tb_graphics.c index d3a8ab8..3a5f3ae 100644 --- a/libraries/tinyblocks-graphics/src/tb_graphics.c +++ b/libraries/tinyblocks-graphics/src/tb_graphics.c @@ -355,9 +355,20 @@ bool tb_graphics_window_is_open(TinyBlocks_Block* window) { /* === * Loop principal * === */ +/* Completa o quadro em ~16,7 ms contando desde o anterior (60 fps) */ static void default_wait(void) { - struct timespec ts = { 0, 16 * 1000000L }; - nanosleep(&ts, NULL); + static struct timespec prev = { 0, 0 }; + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + if (prev.tv_sec || prev.tv_nsec) { + long ns = (now.tv_sec - prev.tv_sec) * 1000000000L + (now.tv_nsec - prev.tv_nsec); + long left = 16666667L - ns; + if (left > 500000L) { + struct timespec ts = { 0, left }; + nanosleep(&ts, NULL); + } + } + clock_gettime(CLOCK_MONOTONIC, &prev); } void tb_graphics_run(TinyBlocks* rt) { diff --git a/libraries/tinyblocks-sdl2/src/tb_sdl2.c b/libraries/tinyblocks-sdl2/src/tb_sdl2.c index 7713ca3..810f1aa 100644 --- a/libraries/tinyblocks-sdl2/src/tb_sdl2.c +++ b/libraries/tinyblocks-sdl2/src/tb_sdl2.c @@ -712,7 +712,20 @@ static float be_measure_text(const char* text, float font_size) { return (float)w; } -static void be_frame_wait(void) { SDL_Delay(14); } +/* Completa o quadro em ~16,7 ms (60 fps) contando desde o anterior. Com + * vsync o present ja esperou e nao sobra nada para dormir; antes era um + * SDL_Delay(14) fixo somado ao trabalho e ao vsync (nunca dava 60 fps). */ +static void frame_pace(void) { + static Uint64 prev = 0; + Uint64 freq = SDL_GetPerformanceFrequency(), now = SDL_GetPerformanceCounter(); + if (prev) { + double ms = (double)(now - prev) * 1000.0 / (double)freq; + if (ms < 15.5) SDL_Delay((Uint32)(16.6 - ms)); + } + prev = SDL_GetPerformanceCounter(); +} + +static void be_frame_wait(void) { frame_pace(); } static const TinyBlocks_GraphicsBackend s_sdl2_backend = { .name = "sdl2", @@ -965,7 +978,7 @@ static void eval_sdl2_app(TinyBlocks_Block* self, TinyBlocks* rt) { /* Libera os renderables adquiridos neste frame */ tb_renderable_release_all(); - SDL_Delay(14); + frame_pace(); } fonts_close_all();