WindowShouldClose and custom main loop example.
This commit is contained in:
7
API.md
7
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
52
examples/custom_main_loop/main.lua
Normal file
52
examples/custom_main_loop/main.lua
Normal file
@@ -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
|
||||
@@ -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 );
|
||||
|
||||
12
src/core.c
12
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()
|
||||
|
||||
|
||||
@@ -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 );
|
||||
|
||||
Reference in New Issue
Block a user