diff options
author | zlago | 2025-05-10 09:42:33 +0200 |
---|---|---|
committer | zlago | 2025-05-10 09:42:33 +0200 |
commit | 652b109aad1b39057075e8411d83ac61cab7237c (patch) | |
tree | 12d455cae62d2c74f240e6a765632ec831b68442 | |
parent | 8c44ff6a0f085850952b104b6f3daeab448cb4f8 (diff) |
framerate fix
not a permanent fix yet, casting to double feels wrong, and the game will not try to catch up if it oversleeps
-rw-r--r-- | src/main.c | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/src/main.c b/src/main.c index 09c5425..922ca20 100644 --- a/src/main.c +++ b/src/main.c @@ -4,8 +4,8 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <inttypes.h> -#include <errno.h> +#include <errno.h> // ENOENT +#include <math.h> // used by vk2dSleep #include "input.h" #include "loader.h" @@ -14,8 +14,8 @@ #include "tilemap.h" -#include "incbin.h" -#include "libplum.h" +#include "incbin.h" // game icon +#include "libplum.h" // decoding game icon #include "main.h" @@ -273,6 +273,27 @@ void game_render_flush(SDL_Texture *framebuffer) { SDL_RenderPresent(renderer); } +// from libertea / <https://github.com/PaoloMazzon> +double getTime(void) { + static double time = -1; + if (time == -1) + time = SDL_GetPerformanceCounter(); + return (double)(SDL_GetPerformanceCounter() - time) / SDL_GetPerformanceFrequency(); +} + +// also from libertea / <https://github.com/PaoloMazzon> +void vk2dSleep(double seconds) { + if (seconds <= 0) + return; + double start = SDL_GetPerformanceCounter(); + double milliseconds = floor(seconds * 1000); + SDL_Delay(milliseconds); + while ((SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() < seconds) { + volatile int i = 0; + (void) i; + } +} + int main(int const argc, char *const *const argv) { save_file_name = SDL_GetPrefPath("sylvie", "game"); save_file_name = SDL_realloc(save_file_name, strlen(save_file_name) + strlen("save.sav") + 1); @@ -358,12 +379,13 @@ int main(int const argc, char *const *const argv) { #if defined(__EMSCRIPTEN__) scale = 1; #else - if (scale <= 0) { // this looks very wrong + if (scale <= 0) { // because we used atoi, the user can set scale to -1 or something SDL_DisplayMode dm; if (SDL_GetDesktopDisplayMode(0, &dm) != 0) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "couldnt get desktop size", SDL_GetError(), NULL); - fprintf(stderr, "info: couldnt get desktop size %s\n", SDL_GetError()); + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "couldnt get desktop size", SDL_GetError(), NULL); + fprintf(stderr, "warn: couldnt get desktop size %s\n", SDL_GetError()); flags |= SDL_WINDOW_RESIZABLE; + scale = 3; } else { int x = dm.w / 2 / WINDOW_WIDTH; int y = dm.h / 2 / WINDOW_HEIGHT; @@ -375,8 +397,10 @@ int main(int const argc, char *const *const argv) { if (scale == 0) { scale = 1; } - if (dm.refresh_rate != 60) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "refresh rate", "this game currently only runs well on 60Hz displays", NULL); + if (dm.refresh_rate < 59) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, "refresh rate", "your monitor appears to have a refresh rate below 60Hz, the game will run slower\n" + "you can compile the game from source to disable vsync, unlocking the framerate\n" + "TODO: disabling vsync without a recompile", NULL); } } } @@ -494,6 +518,8 @@ void main_loop(void) { int x = 0, y = 0; while (1) { #endif + double const begin_time = getTime(); + input_pressed = input_held; SDL_Event evt; while (SDL_PollEvent(&evt)) { @@ -563,7 +589,7 @@ void main_loop(void) { i = evt.tfinger.fingerId; touch.positions[i].x = evt.tfinger.x; touch.positions[i].y = evt.tfinger.y; - printf("%" PRIu64 " %" PRIu64 " %f %f\n", evt.tfinger.touchId, evt.tfinger.fingerId, evt.tfinger.x, evt.tfinger.y); + //printf("%" PRIu64 " %" PRIu64 " %f %f\n", evt.tfinger.touchId, evt.tfinger.fingerId, evt.tfinger.x, evt.tfinger.y); reset_touch: touch.input_touch = 0; for (size_t i = 0; i < touch.allocated; i++) { @@ -733,6 +759,8 @@ void main_loop(void) { #if defined(__EMSCRIPTEN__) return; #else + double const end_time = getTime(); + vk2dSleep(1.0/60.0 - (end_time - begin_time)); } #endif |