GC_UNLOAD build time define and replaced with flag to change it at runtime.

This commit is contained in:
jussi
2023-11-15 18:34:34 +02:00
parent 7b10306ed5
commit 841aa897f1
10 changed files with 80 additions and 81 deletions

6
API.md
View File

@@ -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 ## Core - Files management functions
--- ---

View File

@@ -8,17 +8,12 @@ set( CMAKE_C_STANDARD 99 ) # Requires C99 standard
option( SHARED "Build using dynamic libraries." off ) option( SHARED "Build using dynamic libraries." off )
option( LUAJIT "Use LuaJIT." 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 ) if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE ) 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" ) set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" )
endif() endif()
if( GC_UNLOAD )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGC_UNLOAD" )
endif()
file( GLOB SOURCES src/*.c ) file( GLOB SOURCES src/*.c )
include_directories( include ) include_directories( include )

View File

@@ -1680,6 +1680,11 @@ function RL.OpenURL( url ) end
---@return any enabled ---@return any enabled
function RL.IsGCUnloadEnabled() end 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 -- Core - Files management functions
---Return game directory (where main.lua is located) ---Return game directory (where main.lua is located)

View File

@@ -18,6 +18,7 @@ KEY CHANGES:
- ADDED: Global variable descriptions for API. - ADDED: Global variable descriptions for API.
- CHANGED: Organized functions by putting them in the same order as in Raylib. - CHANGED: Organized functions by putting them in the same order as in Raylib.
- ADDED: Matrix library. - ADDED: Matrix library.
- Removed: GC_UNLOAD build time define and replaced with flag to change it at runtime.
DETAILED CHANGES: DETAILED CHANGES:
- CHANGED: GenImageColor now takes Vector2 as size. - CHANGED: GenImageColor now takes Vector2 as size.

View File

@@ -294,5 +294,4 @@ function RL.draw()
RL.EndTextureMode() 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.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 end

View File

@@ -94,6 +94,7 @@ int lcoreSetLogLevelInvalid( lua_State *L );
int lcoreGetLogLevelInvalid( lua_State *L ); int lcoreGetLogLevelInvalid( lua_State *L );
int lcoreOpenURL( lua_State *L ); int lcoreOpenURL( lua_State *L );
int lcoreIsGCUnloadEnabled( lua_State *L ); int lcoreIsGCUnloadEnabled( lua_State *L );
int lcoreSetGCUnload( lua_State *L );
/* Files management functions. */ /* Files management functions. */
int lcoreGetBasePath( lua_State *L ); int lcoreGetBasePath( lua_State *L );
int lcoreFileExists( lua_State *L ); int lcoreFileExists( lua_State *L );

View File

@@ -1253,6 +1253,17 @@ int lcoreIsGCUnloadEnabled( lua_State *L ) {
return 1; return 1;
} }
/*
> 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 ## Core - Files management functions
*/ */

View File

@@ -18,9 +18,10 @@
/* Buffer. */ /* Buffer. */
static int gcBuffer( lua_State *L ) { static int gcBuffer( lua_State *L ) {
Buffer *buffer = luaL_checkudata( L, 1, "Buffer" ); if ( state->gcUnload ) {
Buffer *buffer = luaL_checkudata( L, 1, "Buffer" );
unloadBuffer( buffer ); unloadBuffer( buffer );
}
} }
static void defineBuffer() { static void defineBuffer() {
@@ -29,17 +30,16 @@ static void defineBuffer() {
luaL_newmetatable( L, "Buffer" ); luaL_newmetatable( L, "Buffer" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcBuffer ); lua_pushcfunction( L, gcBuffer );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Image */ /* Image */
static int gcImage( lua_State *L ) { static int gcImage( lua_State *L ) {
Image *image = luaL_checkudata( L, 1, "Image" ); if ( state->gcUnload ) {
Image *image = luaL_checkudata( L, 1, "Image" );
UnloadImage( *image ); UnloadImage( *image );
}
} }
static void defineImage() { static void defineImage() {
@@ -48,17 +48,16 @@ static void defineImage() {
luaL_newmetatable( L, "Image" ); luaL_newmetatable( L, "Image" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcImage ); lua_pushcfunction( L, gcImage );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Texture */ /* Texture */
static int gcTexture( lua_State *L ) { static int gcTexture( lua_State *L ) {
Texture *texture = luaL_checkudata( L, 1, "Texture" ); if ( state->gcUnload ) {
Texture *texture = luaL_checkudata( L, 1, "Texture" );
UnloadTexture( *texture ); UnloadTexture( *texture );
}
} }
static void defineTexture() { static void defineTexture() {
@@ -67,17 +66,17 @@ static void defineTexture() {
luaL_newmetatable( L, "Texture" ); luaL_newmetatable( L, "Texture" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcTexture ); lua_pushcfunction( L, gcTexture );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* RenderRexture. */ /* RenderRexture. */
static int gcRenderTexture( lua_State *L ) { static int gcRenderTexture( lua_State *L ) {
RenderTexture *renderTexture = luaL_checkudata( L, 1, "RenderTexture" ); if ( state->gcUnload ) {
RenderTexture *renderTexture = luaL_checkudata( L, 1, "RenderTexture" );
UnloadRenderTexture( *renderTexture ); UnloadRenderTexture( *renderTexture );
}
} }
static void defineRenderTexture() { static void defineRenderTexture() {
@@ -86,10 +85,8 @@ static void defineRenderTexture() {
luaL_newmetatable( L, "RenderTexture" ); luaL_newmetatable( L, "RenderTexture" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcRenderTexture ); lua_pushcfunction( L, gcRenderTexture );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Camera2D. */ /* Camera2D. */
@@ -112,9 +109,10 @@ static void defineCamera3D() {
/* Shader. */ /* Shader. */
static int gcShader( lua_State *L ) { static int gcShader( lua_State *L ) {
Shader *shader = luaL_checkudata( L, 1, "Shader" ); if ( state->gcUnload ) {
Shader *shader = luaL_checkudata( L, 1, "Shader" );
UnloadShader( *shader ); UnloadShader( *shader );
}
} }
static void defineShader() { static void defineShader() {
@@ -123,17 +121,16 @@ static void defineShader() {
luaL_newmetatable( L, "Shader" ); luaL_newmetatable( L, "Shader" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcShader ); lua_pushcfunction( L, gcShader );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Font. */ /* Font. */
static int gcFont( lua_State *L ) { static int gcFont( lua_State *L ) {
Font *font = luaL_checkudata( L, 1, "Font" ); if ( state->gcUnload ) {
Font *font = luaL_checkudata( L, 1, "Font" );
UnloadFont( *font ); UnloadFont( *font );
}
} }
static void defineFont() { static void defineFont() {
@@ -142,17 +139,16 @@ static void defineFont() {
luaL_newmetatable( L, "Font" ); luaL_newmetatable( L, "Font" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcFont ); lua_pushcfunction( L, gcFont );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Wave. */ /* Wave. */
static int gcWave( lua_State *L ) { static int gcWave( lua_State *L ) {
Wave *wave = luaL_checkudata( L, 1, "Wave" ); if ( state->gcUnload ) {
Wave *wave = luaL_checkudata( L, 1, "Wave" );
UnloadWave( *wave ); UnloadWave( *wave );
}
} }
static void defineWave() { static void defineWave() {
@@ -161,17 +157,16 @@ static void defineWave() {
luaL_newmetatable( L, "Wave" ); luaL_newmetatable( L, "Wave" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcWave ); lua_pushcfunction( L, gcWave );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Sound. */ /* Sound. */
static int gcSound( lua_State *L ) { static int gcSound( lua_State *L ) {
Sound *sound = luaL_checkudata( L, 1, "Sound" ); if ( state->gcUnload ) {
Sound *sound = luaL_checkudata( L, 1, "Sound" );
UnloadSound( *sound ); UnloadSound( *sound );
}
} }
static void defineSound() { static void defineSound() {
@@ -180,17 +175,16 @@ static void defineSound() {
luaL_newmetatable( L, "Sound" ); luaL_newmetatable( L, "Sound" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcSound ); lua_pushcfunction( L, gcSound );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Music. */ /* Music. */
static int gcMusic( lua_State *L ) { static int gcMusic( lua_State *L ) {
Music *music = luaL_checkudata( L, 1, "Music" ); if ( state->gcUnload ) {
Music *music = luaL_checkudata( L, 1, "Music" );
UnloadMusicStream( *music ); UnloadMusicStream( *music );
}
} }
static void defineMusic() { static void defineMusic() {
@@ -199,10 +193,8 @@ static void defineMusic() {
luaL_newmetatable( L, "Music" ); luaL_newmetatable( L, "Music" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcMusic ); lua_pushcfunction( L, gcMusic );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Light. */ /* Light. */
@@ -216,12 +208,11 @@ static void defineLight() {
/* Material. */ /* Material. */
static int gcMaterial( lua_State *L ) { static int gcMaterial( lua_State *L ) {
Material *material = luaL_checkudata( L, 1, "Material" ); if ( state->gcUnload ) {
Material *material = luaL_checkudata( L, 1, "Material" );
/* Custom UnloadMaterial since we don't want to free Shaders or Textures. */ /* Custom UnloadMaterial since we don't want to free Shaders or Textures. */
unloadMaterial( material ); unloadMaterial( material );
}
// UnloadMaterial( *material );
} }
static void defineMaterial() { static void defineMaterial() {
@@ -230,17 +221,16 @@ static void defineMaterial() {
luaL_newmetatable( L, "Material" ); luaL_newmetatable( L, "Material" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcMaterial ); lua_pushcfunction( L, gcMaterial );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Mesh. */ /* Mesh. */
static int gcMesh( lua_State *L ) { static int gcMesh( lua_State *L ) {
Mesh *mesh = luaL_checkudata( L, 1, "Mesh" ); if ( state->gcUnload ) {
Mesh *mesh = luaL_checkudata( L, 1, "Mesh" );
UnloadMesh( *mesh ); UnloadMesh( *mesh );
}
} }
static void defineMesh() { static void defineMesh() {
@@ -249,18 +239,17 @@ static void defineMesh() {
luaL_newmetatable( L, "Mesh" ); luaL_newmetatable( L, "Mesh" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcMesh ); lua_pushcfunction( L, gcMesh );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Model. */ /* Model. */
static int gcModel( lua_State *L ) { static int gcModel( lua_State *L ) {
Model *model = luaL_checkudata( L, 1, "Model" ); if ( state->gcUnload ) {
Model *model = luaL_checkudata( L, 1, "Model" );
UnloadModel( *model ); UnloadModel( *model );
// UnloadModelKeepMeshes( *model ); // UnloadModelKeepMeshes( *model );
}
} }
static void defineModel() { static void defineModel() {
@@ -269,17 +258,16 @@ static void defineModel() {
luaL_newmetatable( L, "Model" ); luaL_newmetatable( L, "Model" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcModel ); lua_pushcfunction( L, gcModel );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* ModelAnimation. */ /* ModelAnimation. */
static int gcModelAnimation( lua_State *L ) { static int gcModelAnimation( lua_State *L ) {
ModelAnimation *modelAnimation = luaL_checkudata( L, 1, "ModelAnimation" ); if ( state->gcUnload ) {
ModelAnimation *modelAnimation = luaL_checkudata( L, 1, "ModelAnimation" );
UnloadModelAnimation( *modelAnimation ); UnloadModelAnimation( *modelAnimation );
}
} }
static void defineModelAnimation() { static void defineModelAnimation() {
@@ -288,10 +276,8 @@ static void defineModelAnimation() {
luaL_newmetatable( L, "ModelAnimation" ); luaL_newmetatable( L, "ModelAnimation" );
lua_pushvalue( L, -1 ); lua_pushvalue( L, -1 );
lua_setfield( L, -2, "__index" ); lua_setfield( L, -2, "__index" );
#ifdef GC_UNLOAD
lua_pushcfunction( L, gcModelAnimation ); lua_pushcfunction( L, gcModelAnimation );
lua_setfield( L, -2, "__gc" ); lua_setfield( L, -2, "__gc" );
#endif
} }
/* Assing globals. */ /* Assing globals. */
@@ -1591,6 +1577,7 @@ void luaRegister() {
assingGlobalFunction( "GetLogLevelInvalid", lcoreGetLogLevelInvalid ); assingGlobalFunction( "GetLogLevelInvalid", lcoreGetLogLevelInvalid );
assingGlobalFunction( "OpenURL", lcoreOpenURL ); assingGlobalFunction( "OpenURL", lcoreOpenURL );
assingGlobalFunction( "IsGCUnloadEnabled", lcoreIsGCUnloadEnabled ); assingGlobalFunction( "IsGCUnloadEnabled", lcoreIsGCUnloadEnabled );
assingGlobalFunction( "SetGCUnload", lcoreSetGCUnload );
/* Files management functions. */ /* Files management functions. */
assingGlobalFunction( "GetBasePath", lcoreGetBasePath ); assingGlobalFunction( "GetBasePath", lcoreGetBasePath );
assingGlobalFunction( "FileExists", lcoreFileExists ); assingGlobalFunction( "FileExists", lcoreFileExists );

View File

@@ -1471,7 +1471,6 @@ int lmodelsCreateMaterial( lua_State *L ) {
if ( strcmp( "texture", (char*)lua_tostring( L, -2 ) ) == 0 ) { if ( strcmp( "texture", (char*)lua_tostring( L, -2 ) ) == 0 ) {
Texture *texture = uluaGetTexture( L, lua_gettop( L ) ); Texture *texture = uluaGetTexture( L, lua_gettop( L ) );
material.maps[map].texture = *texture; 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 ) { else if ( strcmp( "color", (char*)lua_tostring( L, -2 ) ) == 0 ) {
material.maps[map].color = uluaGetColor( L, lua_gettop( L ) ); material.maps[map].color = uluaGetColor( L, lua_gettop( L ) );

View File

@@ -17,12 +17,7 @@ bool stateInit( int argn, const char **argc, const char *exePath ) {
state->resolution = (Vector2){ 800, 600 }; state->resolution = (Vector2){ 800, 600 };
state->luaState = NULL; state->luaState = NULL;
state->logLevelInvalid = LOG_ERROR; state->logLevelInvalid = LOG_ERROR;
#ifdef GC_UNLOAD
state->gcUnload = true; state->gcUnload = true;
#else
state->gcUnload = false;
#endif
InitWindow( state->resolution.x, state->resolution.y, "ReiLua" ); InitWindow( state->resolution.x, state->resolution.y, "ReiLua" );