From 724592ec67e8a29a55b5b46aae12a68ee26754e9 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 23 Jun 2023 11:22:12 +0200 Subject: [PATCH] - Added turbo option, to allow waiting for events and use less CPU --- lua.cpp | 12 ++++++++++++ mini.cpp | 8 +++++++- mini.h | 2 ++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lua.cpp b/lua.cpp index cea7739..437a873 100644 --- a/lua.cpp +++ b/lua.cpp @@ -782,6 +782,16 @@ extern "C" { return 1; } + static int cpp_turbo(lua_State *L) { + if (lua_gettop(L) >= 1) { + setturbo(lua_toboolean(L, 1)); + return 0; + } else { + setturbo(true); + return 0; + } + } + static int cpp_exit(lua_State *L) { exit(); return 0; @@ -904,6 +914,8 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_setconf); lua_setglobal(L, "setconf"); lua_pushcfunction(L,cpp_configfolder); lua_setglobal(L, "configfolder"); + lua_pushcfunction(L,cpp_turbo); lua_setglobal(L, "turbo"); + lua_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); diff --git a/mini.cpp b/mini.cpp index 3e89a11..2debe1c 100644 --- a/mini.cpp +++ b/mini.cpp @@ -9,6 +9,8 @@ #include #endif +extern DECLSPEC int SDLCALL (*event_handler_ptr)(SDL_Event*) = &SDL_PollEvent; + #pragma pack(1) struct surface_t { @@ -280,7 +282,7 @@ int main(int argc,char*argv[]){ mouse_just_pressed = 0; while(!should_exit) { mouse_wheel = 0; - while(SDL_PollEvent(&mini_eve)) { + while(event_handler_ptr(&mini_eve)) { if (mini_eve.type == SDL_QUIT) { should_exit=true; should_quit=true; break; } if (mini_eve.type == SDL_KEYDOWN) { /* @@ -1211,6 +1213,10 @@ const char *configfolder() { return file_getconfigfolder(); } +void setturbo(const bool value) { + event_handler_ptr = value ? &SDL_PollEvent : &SDL_WaitEvent; +} + void exit() { should_exit = true; should_quit = true; diff --git a/mini.h b/mini.h index 95dc7cb..0d87939 100644 --- a/mini.h +++ b/mini.h @@ -285,4 +285,6 @@ const char *getconfig(const char* key); void setconfig(const char* key, const char* value); const char *configfolder(); +void setturbo(const bool value); + void exit();