Quadros a 60 fps: completa 16,7 ms em vez de dormir 14 ms fixos

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 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 17:31:27 -03:00
1 parent f4313417c0
commit a30d30c1ab
2 files changed
+27 -3

No files matched your search

@@ -355,10 +355,21 @@ bool tb_graphics_window_is_open(TinyBlocks_Block* window) {
/* === /* ===
* Loop principal * Loop principal
* === */ * === */
/* Completa o quadro em ~16,7 ms contando desde o anterior (60 fps) */
static void default_wait(void) { static void default_wait(void) {
struct timespec ts = { 0, 16 * 1000000L }; 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); nanosleep(&ts, NULL);
} }
}
clock_gettime(CLOCK_MONOTONIC, &prev);
}
void tb_graphics_run(TinyBlocks* rt) { void tb_graphics_run(TinyBlocks* rt) {
const TinyBlocks_GraphicsBackend* b = backend_ready(); const TinyBlocks_GraphicsBackend* b = backend_ready();
+15 -2
View File
@@ -712,7 +712,20 @@ static float be_measure_text(const char* text, float font_size) {
return (float)w; 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 = { static const TinyBlocks_GraphicsBackend s_sdl2_backend = {
.name = "sdl2", .name = "sdl2",
@@ -965,7 +978,7 @@ static void eval_sdl2_app(TinyBlocks_Block* self, TinyBlocks* rt) {
/* Libera os renderables adquiridos neste frame */ /* Libera os renderables adquiridos neste frame */
tb_renderable_release_all(); tb_renderable_release_all();
SDL_Delay(14); frame_pace();
} }
fonts_close_all(); fonts_close_all();