From cb2b0e4dff5e1d98054f9e5f809fd3a14cd220a1 Mon Sep 17 00:00:00 2001 From: jussi Date: Fri, 22 Nov 2024 14:57:31 +0200 Subject: WindowShouldClose and custom main loop example. --- API.md | 7 +++++ ReiLua_API.lua | 5 ++++ apiScanner.lua | 1 - changelog | 1 + examples/custom_main_loop/main.lua | 52 ++++++++++++++++++++++++++++++++++++++ include/core.h | 1 + src/core.c | 12 +++++++++ src/lua_core.c | 9 ++++--- 8 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 examples/custom_main_loop/main.lua diff --git a/API.md b/API.md index 06c70e0..d4196ff 100644 --- a/API.md +++ b/API.md @@ -3783,6 +3783,13 @@ Close window and unload OpenGL context and free all resources --- +> RL.WindowShouldClose() + +Check if application should close (KEY_ESCAPE pressed or windows close icon clicked). +Note! Not needed unless you want to make custom main loop + +--- + > state = RL.IsWindowReady() Check if window has been initialized successfully diff --git a/ReiLua_API.lua b/ReiLua_API.lua index f68df06..f757083 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -1337,6 +1337,11 @@ function RL.InitWindow( size, title ) end ---@return any RL.CloseWindow function RL.CloseWindow() end +---Check if application should close (KEY_ESCAPE pressed or windows close icon clicked). +---Note! Not needed unless you want to make custom main loop +---@return any RL.WindowShouldClose +function RL.WindowShouldClose() end + ---Check if window has been initialized successfully ---- Success return bool ---@return any state diff --git a/apiScanner.lua b/apiScanner.lua index 434d588..88a27cf 100644 --- a/apiScanner.lua +++ b/apiScanner.lua @@ -2,7 +2,6 @@ local raylib = { prefix = "RLAPI", file = "raylib.h", blacklist = { - WindowShouldClose = "Handled internally", GetScreenWidth = "Replaced by GetScreenSize", GetScreenHeight = "Replaced by GetScreenSize", GetRenderWidth = "Replaced by GetRenderSize", diff --git a/changelog b/changelog index 0f01211..34d7fed 100644 --- a/changelog +++ b/changelog @@ -26,6 +26,7 @@ DETAILED CHANGES: - ADDED: RL.config function. - ADDED: InitWindow. Can be called from RL.config. If not, will be called automatically before RL.init. (Curiously InitWindow is function #1057. Before that it was only called automatically.) + - ADDED: WindowShouldClose and custom main loop example. ------------------------------------------------------------------------ Release: ReiLua version 0.8.0 Using Raylib 5.0 and Forked Raygui 4.0 diff --git a/examples/custom_main_loop/main.lua b/examples/custom_main_loop/main.lua new file mode 100644 index 0000000..952496c --- /dev/null +++ b/examples/custom_main_loop/main.lua @@ -0,0 +1,52 @@ +package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" + +Vector2 = require( "vector2" ) + +local function dysfunctionalPrint( message ) + printt( message ) +end + +local function main() + RL.SetConfigFlags( RL.FLAG_VSYNC_HINT ) + RL.InitWindow( { 800, 600 }, "Custom Main Loop" ) + + local catTex = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" ) + local catPos = Vector2:new( 20, 20 ) + local catSpeed = 200 + + while not RL.WindowShouldClose() do + local delta = RL.GetFrameTime() + local vel = Vector2:new() + + if RL.IsKeyDown( RL.KEY_RIGHT ) then + vel.x = catSpeed * delta + elseif RL.IsKeyDown( RL.KEY_LEFT ) then + vel.x = -catSpeed * delta + end + if RL.IsKeyDown( RL.KEY_DOWN ) then + vel.y = catSpeed * delta + elseif RL.IsKeyDown( RL.KEY_UP ) then + vel.y = -catSpeed * delta + end + + if RL.IsKeyPressed( RL.KEY_ENTER ) then + dysfunctionalPrint( "This will fail and give usefull traceback info" ) + end + + catPos:addEq( vel ) + + RL.BeginDrawing() + RL.ClearBackground( RL.LIGHTGRAY ) + RL.DrawTexture( catTex, catPos, RL.WHITE ) + RL.DrawFPS( { 10, 10 } ) + RL.EndDrawing() + end +end + +-- Call main to get traceback info. +local success, result = xpcall( main, debug.traceback ) + +if not success then + RL.TraceLog( RL.LOG_WARNING, "Error: "..result ) + RL.CloseWindow() +end diff --git a/include/core.h b/include/core.h index e1647c2..e196446 100644 --- a/include/core.h +++ b/include/core.h @@ -6,6 +6,7 @@ void unloadBuffer( Buffer* buffer ); /* Window-related functions. */ int lcoreInitWindow( lua_State* L ); int lcoreCloseWindow( lua_State* L ); +int lcoreWindowShouldClose( lua_State* L ); int lcoreIsWindowReady( lua_State* L ); int lcoreIsWindowFullscreen( lua_State* L ); int lcoreIsWindowHidden( lua_State* L ); diff --git a/src/core.c b/src/core.c index 3ca5a3e..c5736da 100644 --- a/src/core.c +++ b/src/core.c @@ -54,6 +54,18 @@ int lcoreCloseWindow( lua_State* L ) { return 0; } +/* +> RL.WindowShouldClose() + +Check if application should close (KEY_ESCAPE pressed or windows close icon clicked). +Note! Not needed unless you want to make custom main loop +*/ +int lcoreWindowShouldClose( lua_State* L ) { + lua_pushboolean( L, WindowShouldClose() ); + + return 1; +} + /* > state = RL.IsWindowReady() diff --git a/src/lua_core.c b/src/lua_core.c index 3dbd199..aea751a 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -1176,12 +1176,13 @@ void luaCallMain() { luaL_dofile( L, path ); /* Check errors in main.lua */ - if ( lua_tostring( state->luaState, -1 ) ) { - TraceLog( LOG_ERROR, "Lua error: %s\n", lua_tostring( state->luaState, -1 ) ); + if ( lua_tostring( L, -1 ) ) { + TraceLog( LOG_ERROR, "Lua error: %s\n", lua_tostring( L, -1 ) ); + state->run = false; + return; } lua_pushcfunction( L, luaTraceback ); int tracebackidx = lua_gettop( L ); - /* Apply custom callback here. */ SetTraceLogCallback( logCustom ); @@ -1200,7 +1201,6 @@ void luaCallMain() { if ( !IsWindowReady() ) { InitWindow( 800, 600, "ReiLua" ); } - /* Set shader locs after we have window. */ if ( IsWindowReady() ) { stateContextInit(); } @@ -1295,6 +1295,7 @@ void luaRegister() { /* Window-related functions. */ assingGlobalFunction( "InitWindow", lcoreInitWindow ); assingGlobalFunction( "CloseWindow", lcoreCloseWindow ); + assingGlobalFunction( "WindowShouldClose", lcoreWindowShouldClose ); assingGlobalFunction( "IsWindowReady", lcoreIsWindowReady ); assingGlobalFunction( "IsWindowFullscreen", lcoreIsWindowFullscreen ); assingGlobalFunction( "IsWindowHidden", lcoreIsWindowHidden ); -- cgit v1.2.3