From b6e5dca27781cfc1ee214b5d405996d2a3804b8a Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 30 May 2025 17:43:43 +0200 Subject: [PATCH] - [NEW] system.fps() --- lua.cpp | 6 ++++++ mini.cpp | 14 +++++++++++++- mini.h | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lua.cpp b/lua.cpp index 79f27a9..af0d453 100644 --- a/lua.cpp +++ b/lua.cpp @@ -670,6 +670,11 @@ extern "C" { return 0; } + static int cpp_sys_fps(lua_State *L) { + lua_pushnumber(L, getfps()); + return 1; + } + // win // =============================================== @@ -922,6 +927,7 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_sys_updateTimeout); lua_setfield(L, -2, "updateOnEventsAndTimeout"); lua_pushcfunction(L,cpp_sys_dir); lua_setfield(L, -2, "getFilesInDataDirectory"); lua_pushcfunction(L,cpp_sys_exit); lua_setfield(L, -2, "quit"); + lua_pushcfunction(L,cpp_sys_fps); lua_setfield(L, -2, "fps"); lua_setglobal(L, "system"); lua_newtable(L); diff --git a/mini.cpp b/mini.cpp index 7b7e8ad..d292bb6 100644 --- a/mini.cpp +++ b/mini.cpp @@ -23,7 +23,9 @@ struct surface_t { uint32_t size; }; - +int fps=0; +int fps_counter=0; +uint32_t fps_timer=0; char window_title[256]; char config_folder[256]; uint16_t screen_width = 160; @@ -475,6 +477,12 @@ int main(int argc,char*argv[]){ SDL_UnlockTexture(mini_bak); SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL); SDL_RenderPresent(mini_ren); + fps_counter++; + if (SDL_GetTicks()>=(fps_timer+1000)) { + fps = fps_counter; + fps_counter=0; + fps_timer = SDL_GetTicks(); + } } lua_quit(); quitaudio(); @@ -1146,6 +1154,10 @@ int rnd(int x) { return rand()%x; } +int getfps() { + return fps; +} + void playmusic(const char *filename, const int loop) { int size; char *buffer = file_getfilebuffer(filename, size); diff --git a/mini.h b/mini.h index 0b32131..ee49b3b 100644 --- a/mini.h +++ b/mini.h @@ -220,6 +220,7 @@ float time(); bool beat(int16_t i); int rnd(int x); +int getfps(); #define debug printf