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. --- src/core.c | 12 ++++++++++++ src/lua_core.c | 9 +++++---- 2 files changed, 17 insertions(+), 4 deletions(-) (limited to 'src') 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