diff options
| author | jussi | 2023-11-15 18:34:34 +0200 |
|---|---|---|
| committer | jussi | 2023-11-15 18:34:34 +0200 |
| commit | 841aa897f1868075134162cc71593ac9c3473115 (patch) | |
| tree | cc03b824d1a16a086aecc7a3246d9b434b7d3118 | |
| parent | 7b10306ed5c87517800a6058ad84e27c866f81c6 (diff) | |
| download | reilua-enhanced-841aa897f1868075134162cc71593ac9c3473115.tar.gz reilua-enhanced-841aa897f1868075134162cc71593ac9c3473115.tar.bz2 reilua-enhanced-841aa897f1868075134162cc71593ac9c3473115.zip | |
GC_UNLOAD build time define and replaced with flag to change it at runtime.
| -rw-r--r-- | API.md | 6 | ||||
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | ReiLua_API.lua | 5 | ||||
| -rw-r--r-- | changelog | 1 | ||||
| -rw-r--r-- | examples/platformer/main.lua | 1 | ||||
| -rw-r--r-- | include/core.h | 1 | ||||
| -rw-r--r-- | src/core.c | 11 | ||||
| -rw-r--r-- | src/lua_core.c | 125 | ||||
| -rw-r--r-- | src/models.c | 1 | ||||
| -rw-r--r-- | src/state.c | 5 |
10 files changed, 80 insertions, 81 deletions
@@ -4128,6 +4128,12 @@ Check if Lua garbage collection is set to unload object data --- +> RL.SetGCUnload( bool enabled ) + +Set Lua garbage collection to unload object data + +--- + ## Core - Files management functions --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 7194d02..f2b4c7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,17 +8,12 @@ set( CMAKE_C_STANDARD 99 ) # Requires C99 standard option( SHARED "Build using dynamic libraries." off ) option( LUAJIT "Use LuaJIT." off ) -option( GC_UNLOAD "Lua garbage collector unloads objects. If off, object unloading should be handled manually." on ) if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES ) set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE ) set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" ) endif() -if( GC_UNLOAD ) - set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGC_UNLOAD" ) -endif() - file( GLOB SOURCES src/*.c ) include_directories( include ) diff --git a/ReiLua_API.lua b/ReiLua_API.lua index 67ace3f..f1ffa0b 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -1680,6 +1680,11 @@ function RL.OpenURL( url ) end ---@return any enabled function RL.IsGCUnloadEnabled() end +---Set Lua garbage collection to unload object data +---@param enabled boolean +---@return any RL.SetGCUnload +function RL.SetGCUnload( enabled ) end + -- Core - Files management functions ---Return game directory (where main.lua is located) @@ -18,6 +18,7 @@ KEY CHANGES: - ADDED: Global variable descriptions for API. - CHANGED: Organized functions by putting them in the same order as in Raylib. - ADDED: Matrix library. + - Removed: GC_UNLOAD build time define and replaced with flag to change it at runtime. DETAILED CHANGES: - CHANGED: GenImageColor now takes Vector2 as size. diff --git a/examples/platformer/main.lua b/examples/platformer/main.lua index 821839a..854b02d 100644 --- a/examples/platformer/main.lua +++ b/examples/platformer/main.lua @@ -294,5 +294,4 @@ function RL.draw() RL.EndTextureMode() RL.DrawTexturePro( RL.GetRenderTextureTexture( framebuffer ), { 0, 0, res.x, -res.y }, { 0, 0, winSize.x, winSize.y }, { 0, 0 }, 0.0, RL.WHITE ) - -- RL.glBlitFramebuffer( framebuffer, -1, res, winSize, RL.GL_COLOR_BUFFER_BIT, RL.GL_NEAREST ) end diff --git a/include/core.h b/include/core.h index 32aa8f9..4b61d5f 100644 --- a/include/core.h +++ b/include/core.h @@ -94,6 +94,7 @@ int lcoreSetLogLevelInvalid( lua_State *L ); int lcoreGetLogLevelInvalid( lua_State *L ); int lcoreOpenURL( lua_State *L ); int lcoreIsGCUnloadEnabled( lua_State *L ); +int lcoreSetGCUnload( lua_State *L ); /* Files management functions. */ int lcoreGetBasePath( lua_State *L ); int lcoreFileExists( lua_State *L ); @@ -1254,6 +1254,17 @@ int lcoreIsGCUnloadEnabled( lua_State *L ) { } /* +> RL.SetGCUnload( bool enabled ) + +Set Lua garbage collection to unload object data +*/ +int lcoreSetGCUnload( lua_State *L ) { + state->gcUnload = uluaGetBoolean( L, 1 ); + + return 0; +} + +/* ## Core - Files management functions */ diff --git a/src/lua_core.c b/src/lua_core.c index 0eca9be..ebe0a42 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -18,9 +18,10 @@ /* Buffer. */ static int gcBuffer( lua_State *L ) { - Buffer *buffer = luaL_checkudata( L, 1, "Buffer" ); - - unloadBuffer( buffer ); + if ( state->gcUnload ) { + Buffer *buffer = luaL_checkudata( L, 1, "Buffer" ); + unloadBuffer( buffer ); + } } static void defineBuffer() { @@ -29,17 +30,16 @@ static void defineBuffer() { luaL_newmetatable( L, "Buffer" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcBuffer ); lua_setfield( L, -2, "__gc" ); -#endif } /* Image */ static int gcImage( lua_State *L ) { - Image *image = luaL_checkudata( L, 1, "Image" ); - - UnloadImage( *image ); + if ( state->gcUnload ) { + Image *image = luaL_checkudata( L, 1, "Image" ); + UnloadImage( *image ); + } } static void defineImage() { @@ -48,17 +48,16 @@ static void defineImage() { luaL_newmetatable( L, "Image" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcImage ); lua_setfield( L, -2, "__gc" ); -#endif } /* Texture */ static int gcTexture( lua_State *L ) { - Texture *texture = luaL_checkudata( L, 1, "Texture" ); - - UnloadTexture( *texture ); + if ( state->gcUnload ) { + Texture *texture = luaL_checkudata( L, 1, "Texture" ); + UnloadTexture( *texture ); + } } static void defineTexture() { @@ -67,17 +66,17 @@ static void defineTexture() { luaL_newmetatable( L, "Texture" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD + lua_pushcfunction( L, gcTexture ); lua_setfield( L, -2, "__gc" ); -#endif } /* RenderRexture. */ static int gcRenderTexture( lua_State *L ) { - RenderTexture *renderTexture = luaL_checkudata( L, 1, "RenderTexture" ); - - UnloadRenderTexture( *renderTexture ); + if ( state->gcUnload ) { + RenderTexture *renderTexture = luaL_checkudata( L, 1, "RenderTexture" ); + UnloadRenderTexture( *renderTexture ); + } } static void defineRenderTexture() { @@ -86,10 +85,8 @@ static void defineRenderTexture() { luaL_newmetatable( L, "RenderTexture" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcRenderTexture ); lua_setfield( L, -2, "__gc" ); -#endif } /* Camera2D. */ @@ -112,9 +109,10 @@ static void defineCamera3D() { /* Shader. */ static int gcShader( lua_State *L ) { - Shader *shader = luaL_checkudata( L, 1, "Shader" ); - - UnloadShader( *shader ); + if ( state->gcUnload ) { + Shader *shader = luaL_checkudata( L, 1, "Shader" ); + UnloadShader( *shader ); + } } static void defineShader() { @@ -123,17 +121,16 @@ static void defineShader() { luaL_newmetatable( L, "Shader" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcShader ); lua_setfield( L, -2, "__gc" ); -#endif } /* Font. */ static int gcFont( lua_State *L ) { - Font *font = luaL_checkudata( L, 1, "Font" ); - - UnloadFont( *font ); + if ( state->gcUnload ) { + Font *font = luaL_checkudata( L, 1, "Font" ); + UnloadFont( *font ); + } } static void defineFont() { @@ -142,17 +139,16 @@ static void defineFont() { luaL_newmetatable( L, "Font" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcFont ); lua_setfield( L, -2, "__gc" ); -#endif } /* Wave. */ static int gcWave( lua_State *L ) { - Wave *wave = luaL_checkudata( L, 1, "Wave" ); - - UnloadWave( *wave ); + if ( state->gcUnload ) { + Wave *wave = luaL_checkudata( L, 1, "Wave" ); + UnloadWave( *wave ); + } } static void defineWave() { @@ -161,17 +157,16 @@ static void defineWave() { luaL_newmetatable( L, "Wave" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcWave ); lua_setfield( L, -2, "__gc" ); -#endif } /* Sound. */ static int gcSound( lua_State *L ) { - Sound *sound = luaL_checkudata( L, 1, "Sound" ); - - UnloadSound( *sound ); + if ( state->gcUnload ) { + Sound *sound = luaL_checkudata( L, 1, "Sound" ); + UnloadSound( *sound ); + } } static void defineSound() { @@ -180,17 +175,16 @@ static void defineSound() { luaL_newmetatable( L, "Sound" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcSound ); lua_setfield( L, -2, "__gc" ); -#endif } /* Music. */ static int gcMusic( lua_State *L ) { - Music *music = luaL_checkudata( L, 1, "Music" ); - - UnloadMusicStream( *music ); + if ( state->gcUnload ) { + Music *music = luaL_checkudata( L, 1, "Music" ); + UnloadMusicStream( *music ); + } } static void defineMusic() { @@ -199,10 +193,8 @@ static void defineMusic() { luaL_newmetatable( L, "Music" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcMusic ); lua_setfield( L, -2, "__gc" ); -#endif } /* Light. */ @@ -216,12 +208,11 @@ static void defineLight() { /* Material. */ static int gcMaterial( lua_State *L ) { - Material *material = luaL_checkudata( L, 1, "Material" ); - - /* Custom UnloadMaterial since we don't want to free Shaders or Textures. */ - unloadMaterial( material ); - - // UnloadMaterial( *material ); + if ( state->gcUnload ) { + Material *material = luaL_checkudata( L, 1, "Material" ); + /* Custom UnloadMaterial since we don't want to free Shaders or Textures. */ + unloadMaterial( material ); + } } static void defineMaterial() { @@ -230,17 +221,16 @@ static void defineMaterial() { luaL_newmetatable( L, "Material" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcMaterial ); lua_setfield( L, -2, "__gc" ); -#endif } /* Mesh. */ static int gcMesh( lua_State *L ) { - Mesh *mesh = luaL_checkudata( L, 1, "Mesh" ); - - UnloadMesh( *mesh ); + if ( state->gcUnload ) { + Mesh *mesh = luaL_checkudata( L, 1, "Mesh" ); + UnloadMesh( *mesh ); + } } static void defineMesh() { @@ -249,18 +239,17 @@ static void defineMesh() { luaL_newmetatable( L, "Mesh" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcMesh ); lua_setfield( L, -2, "__gc" ); -#endif } /* Model. */ static int gcModel( lua_State *L ) { - Model *model = luaL_checkudata( L, 1, "Model" ); - - UnloadModel( *model ); - // UnloadModelKeepMeshes( *model ); + if ( state->gcUnload ) { + Model *model = luaL_checkudata( L, 1, "Model" ); + UnloadModel( *model ); + // UnloadModelKeepMeshes( *model ); + } } static void defineModel() { @@ -269,17 +258,16 @@ static void defineModel() { luaL_newmetatable( L, "Model" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcModel ); lua_setfield( L, -2, "__gc" ); -#endif } /* ModelAnimation. */ static int gcModelAnimation( lua_State *L ) { - ModelAnimation *modelAnimation = luaL_checkudata( L, 1, "ModelAnimation" ); - - UnloadModelAnimation( *modelAnimation ); + if ( state->gcUnload ) { + ModelAnimation *modelAnimation = luaL_checkudata( L, 1, "ModelAnimation" ); + UnloadModelAnimation( *modelAnimation ); + } } static void defineModelAnimation() { @@ -288,10 +276,8 @@ static void defineModelAnimation() { luaL_newmetatable( L, "ModelAnimation" ); lua_pushvalue( L, -1 ); lua_setfield( L, -2, "__index" ); -#ifdef GC_UNLOAD lua_pushcfunction( L, gcModelAnimation ); lua_setfield( L, -2, "__gc" ); -#endif } /* Assing globals. */ @@ -1591,6 +1577,7 @@ void luaRegister() { assingGlobalFunction( "GetLogLevelInvalid", lcoreGetLogLevelInvalid ); assingGlobalFunction( "OpenURL", lcoreOpenURL ); assingGlobalFunction( "IsGCUnloadEnabled", lcoreIsGCUnloadEnabled ); + assingGlobalFunction( "SetGCUnload", lcoreSetGCUnload ); /* Files management functions. */ assingGlobalFunction( "GetBasePath", lcoreGetBasePath ); assingGlobalFunction( "FileExists", lcoreFileExists ); diff --git a/src/models.c b/src/models.c index 9ff64d8..ae0a5cb 100644 --- a/src/models.c +++ b/src/models.c @@ -1471,7 +1471,6 @@ int lmodelsCreateMaterial( lua_State *L ) { if ( strcmp( "texture", (char*)lua_tostring( L, -2 ) ) == 0 ) { Texture *texture = uluaGetTexture( L, lua_gettop( L ) ); material.maps[map].texture = *texture; - printf( "Material Create material.maps[map].texture.id = %d\n", material.maps[map].texture.id ); } else if ( strcmp( "color", (char*)lua_tostring( L, -2 ) ) == 0 ) { material.maps[map].color = uluaGetColor( L, lua_gettop( L ) ); diff --git a/src/state.c b/src/state.c index 228cbce..b2de3d9 100644 --- a/src/state.c +++ b/src/state.c @@ -17,12 +17,7 @@ bool stateInit( int argn, const char **argc, const char *exePath ) { state->resolution = (Vector2){ 800, 600 }; state->luaState = NULL; state->logLevelInvalid = LOG_ERROR; - -#ifdef GC_UNLOAD state->gcUnload = true; -#else - state->gcUnload = false; -#endif InitWindow( state->resolution.x, state->resolution.y, "ReiLua" ); |
