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
---

View File

@@ -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 )

View File

@@ -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)

View File

@@ -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.

View File

@@ -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

View File

@@ -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 );

View File

@@ -1253,6 +1253,17 @@ int lcoreIsGCUnloadEnabled( lua_State *L ) {
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
*/

View File

@@ -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 );

View File

@@ -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 ) );

View File

@@ -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" );