diff options
| -rw-r--r-- | API.md | 29 | ||||
| -rw-r--r-- | ReiLua_API.lua | 28 | ||||
| -rw-r--r-- | changelog | 5 | ||||
| -rw-r--r-- | examples/dungeon_crawler/main.lua | 2 | ||||
| -rw-r--r-- | examples/heightmap/main.lua | 2 | ||||
| -rw-r--r-- | examples/lightmap/main.lua | 38 | ||||
| -rw-r--r-- | examples/pixelated/main.lua | 2 | ||||
| -rw-r--r-- | examples/platformer/main.lua | 5 | ||||
| -rw-r--r-- | examples/snake/main.lua | 4 | ||||
| -rw-r--r-- | include/state.h | 13 | ||||
| -rw-r--r-- | include/textures.h | 12 | ||||
| -rw-r--r-- | src/core.c | 5 | ||||
| -rw-r--r-- | src/gl.c | 8 | ||||
| -rw-r--r-- | src/lua_core.c | 8 | ||||
| -rw-r--r-- | src/models.c | 11 | ||||
| -rw-r--r-- | src/shapes.c | 2 | ||||
| -rw-r--r-- | src/state.c | 17 | ||||
| -rw-r--r-- | src/textures.c | 206 |
18 files changed, 153 insertions, 244 deletions
@@ -675,9 +675,9 @@ NPATCH_THREE_PATCH_HORIZONTAL ## Globals - TextureModes -TEXTURE_SOURCE_TEXTURE +TEXTURE_TYPE_TEXTURE -TEXTURE_SOURCE_RENDER_TEXTURE +TEXTURE_TYPE_RENDER_TEXTURE ## Globals - Colors @@ -3560,19 +3560,10 @@ Unload texture from GPU memory ( VRAM ) --- -> success = RL.UnloadRenderTexture( RenderTexture2D target ) - -Unload render texture from GPU memory ( VRAM ) - -- Failure return false -- Success return true - ---- - > success = RL.UpdateTexture( Texture2D texture, int{} pixels ) Update GPU texture with new data -NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format +NOTE! Should be TEXTURE_TYPE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format - Failure return false - Success return true @@ -3582,7 +3573,7 @@ NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, > success = RL.UpdateTextureRec( Texture2D texture, Rectangle rec, int{} pixels ) Update GPU texture rectangle with new data -NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format +NOTE! Should be TEXTURE_TYPE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format - Failure return false - Success return true @@ -3644,19 +3635,11 @@ Ends drawing to render texture --- -> success = RL.SetTextureSource( int textureSource ) +> type = RL.GetTextureType( Texture2D texture ) -Set what texture source to use ( TEXTURE_SOURCE_TEXTURE or TEXTURE_SOURCE_RENDER_TEXTURE ) +Get texture type ( TEXTURE_TYPE_TEXTURE or TEXTURE_TYPE_RENDER_TEXTURE ) - Failure return false -- Success return true - ---- - -> textureSource = RL.GetTextureSource() - -Get current texture source type ( TEXTURE_SOURCE_TEXTURE or TEXTURE_SOURCE_RENDER_TEXTURE ) - - Success return int --- diff --git a/ReiLua_API.lua b/ReiLua_API.lua index f2c4289..c8e187e 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -381,8 +381,8 @@ RL.NPATCH_THREE_PATCH_HORIZONTAL=2 -- Globals - TextureModes -RL.TEXTURE_SOURCE_TEXTURE=0 -RL.TEXTURE_SOURCE_RENDER_TEXTURE=1 +RL.TEXTURE_TYPE_TEXTURE=0 +RL.TEXTURE_TYPE_RENDER_TEXTURE=1 -- Globals - Colors @@ -2639,15 +2639,8 @@ function RL.LoadRenderTexture( size ) end ---@return any success function RL.UnloadTexture( texture ) end ----Unload render texture from GPU memory ( VRAM ) ----- Failure return false ----- Success return true ----@param target any ----@return any success -function RL.UnloadRenderTexture( target ) end - ---Update GPU texture with new data ----NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format +---NOTE! Should be TEXTURE_TYPE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format ---- Failure return false ---- Success return true ---@param texture any @@ -2656,7 +2649,7 @@ function RL.UnloadRenderTexture( target ) end function RL.UpdateTexture( texture, pixels ) end ---Update GPU texture rectangle with new data ----NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format +---NOTE! Should be TEXTURE_TYPE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format ---- Failure return false ---- Success return true ---@param texture any @@ -2721,17 +2714,12 @@ function RL.BeginTextureMode( target ) end ---@return any RL.EndTextureMode function RL.EndTextureMode() end ----Set what texture source to use ( TEXTURE_SOURCE_TEXTURE or TEXTURE_SOURCE_RENDER_TEXTURE ) +---Get texture type ( TEXTURE_TYPE_TEXTURE or TEXTURE_TYPE_RENDER_TEXTURE ) ---- Failure return false ----- Success return true ----@param textureSource integer ----@return any success -function RL.SetTextureSource( textureSource ) end - ----Get current texture source type ( TEXTURE_SOURCE_TEXTURE or TEXTURE_SOURCE_RENDER_TEXTURE ) ---- Success return int ----@return any textureSource -function RL.GetTextureSource() end +---@param texture any +---@return any type +function RL.GetTextureType( texture ) end -- Textures - Texture Configuration @@ -11,6 +11,7 @@ KEY CHANGES: - ADDED: Camera3D Lua lib. - ADDED: Raygui wrapper lib. - CHANGED: Can now have multiple Music objects like other Raylib objects instead of just one. + - CHANGED: Texture now can be either Texture or RenderTexture. No need to change texture source anymore. Detailed changes: - FIXED: uluaGetRay was looking for integers instead of tables. @@ -59,6 +60,10 @@ Detailed changes: - ADDED: GetMusicLooping - ADDED: DrawCapsule and DrawCapsuleWires - ADDED: Free Camera3D example + - REMOVED: SetTextureSource + - REMOVED: GetTextureSource + - REMOVED: UnloadRenderTexture + - ADDED: GetTextureType ------------------------------------------------------------------------ Release: ReiLua version 0.4.0 Using Raylib 4.2 diff --git a/examples/dungeon_crawler/main.lua b/examples/dungeon_crawler/main.lua index 3818f67..f8810b1 100644 --- a/examples/dungeon_crawler/main.lua +++ b/examples/dungeon_crawler/main.lua @@ -121,7 +121,5 @@ function RL.draw() RL.EndMode3D() -- RL.EndTextureMode() - -- RL.SetTextureSource( TEXTURE_SOURCE_RENDER_TEXTURE ) -- RL.DrawTexturePro( framebuffer, { 0, 0, res[1], -res[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, WHITE ) - -- RL.SetTextureSource( TEXTURE_SOURCE_TEXTURE ) end diff --git a/examples/heightmap/main.lua b/examples/heightmap/main.lua index ae1f6b8..1b194bb 100644 --- a/examples/heightmap/main.lua +++ b/examples/heightmap/main.lua @@ -75,11 +75,9 @@ function RL.init() RL.EndTextureMode() material = RL.LoadMaterialDefault() - RL.SetTextureSource( RL.TEXTURE_SOURCE_RENDER_TEXTURE ) -- RL.GenTextureMipmaps( groundTexture ) -- RL.SetTextureFilter( groundTexture, RL.TEXTURE_FILTER_TRILINEAR ) RL.SetMaterialTexture( material, RL.MATERIAL_MAP_ALBEDO, groundTexture ) - RL.SetTextureSource( RL.TEXTURE_SOURCE_TEXTURE ) matrix = RL.MatrixMultiply( RL.MatrixIdentity(), RL.MatrixTranslate( { -4, 0, -4 } ) ) end diff --git a/examples/lightmap/main.lua b/examples/lightmap/main.lua index 991bc2d..a6a5bde 100644 --- a/examples/lightmap/main.lua +++ b/examples/lightmap/main.lua @@ -1,3 +1,7 @@ +package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" + +Cam3D = require( "camera3d" ) + local PLANE_SIZE = 8 local monitor = 0 @@ -19,10 +23,18 @@ function RL.init() RL.SetWindowState( RL.FLAG_VSYNC_HINT ) RL.SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) - camera = RL.CreateCamera3D() - RL.SetCamera3DPosition( camera, { 0, 8, 16 } ) - RL.SetCamera3DTarget( camera, { 0, 0, 0 } ) - RL.SetCamera3DUp( camera, { 0, 1, 0 } ) + -- camera = RL.CreateCamera3D() + -- RL.SetCamera3DPosition( camera, { 0, 8, 16 } ) + -- RL.SetCamera3DTarget( camera, { 0, 0, 0 } ) + -- RL.SetCamera3DUp( camera, { 0, 1, 0 } ) + camera = Cam3D:new() + + camera:setPosition( { 0, 8, 16 } ) + camera:setTarget( { 0, 0, 0 } ) + camera:setUp( { 0, 1, 0 } ) + camera.mode = camera.MODES.ORBITAL + -- camera.mode = camera.MODES.FREE + -- camera.mode = camera.MODES.FIRST_PERSON local ts = PLANE_SIZE local meshData = { @@ -76,13 +88,25 @@ function RL.init() matrix = RL.MatrixMultiply( RL.MatrixIdentity(), RL.MatrixTranslate( { -4, 0, -4 } ) ) end +function RL.process( delta ) + camera:process( delta ) + + if RL.IsKeyPressed( RL.KEY_SPACE ) then + if camera.mode == camera.MODES.FREE then + camera.mode = camera.MODES.FIRST_PERSON + else + camera.mode = camera.MODES.FREE + end + end +end + function RL.draw() RL.ClearBackground( { 25, 50, 50 } ) -- RL.UpdateCamera3D( camera, RL.CAMERA_ORBITAL ) -- RL.UpdateCamera3D( camera, RL.CAMERA_FREE ) - RL.UpdateCamera3D( camera, RL.CAMERA_FIRST_PERSON ) + -- RL.UpdateCamera3D( camera, RL.CAMERA_FIRST_PERSON ) - RL.BeginMode3D( camera ) + camera:beginMode3D() RL.DrawMesh( mesh, material, matrix ) - RL.EndMode3D() + camera:endMode3D() end diff --git a/examples/pixelated/main.lua b/examples/pixelated/main.lua index 49f5c42..75a9045 100644 --- a/examples/pixelated/main.lua +++ b/examples/pixelated/main.lua @@ -50,7 +50,5 @@ function RL.draw() RL.DrawTriangle( { 0, 32 }, { 32, 16 }, { 0, 0 }, RL.RED ) RL.EndTextureMode() - RL.SetTextureSource( RL.TEXTURE_SOURCE_RENDER_TEXTURE ) RL.DrawTexturePro( framebuffer, { 0, 0, res[1], -res[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, { 255, 255, 255 } ) - RL.SetTextureSource( RL.TEXTURE_SOURCE_TEXTURE ) end diff --git a/examples/platformer/main.lua b/examples/platformer/main.lua index f419423..fe594ec 100644 --- a/examples/platformer/main.lua +++ b/examples/platformer/main.lua @@ -288,15 +288,12 @@ end function RL.draw() RL.ClearBackground( RL.RED ) - RL.SetTextureSource( RL.TEXTURE_SOURCE_TEXTURE ) RL.BeginTextureMode( framebuffer ) drawMap() drawPlayer() RL.EndTextureMode() - -- RL.SetTextureSource( RL.TEXTURE_SOURCE_RENDER_TEXTURE ) - -- RL.DrawTexturePro( framebuffer, { 0, 0, res.x, -res.y }, { 0, 0, winSize.x, winSize.y }, { 0, 0 }, 0.0, RL.WHITE ) - -- RL.SetTextureSource( RL.TEXTURE_SOURCE_TEXTURE ) + RL.DrawTexturePro( 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/examples/snake/main.lua b/examples/snake/main.lua index 0f07317..4bcec1b 100644 --- a/examples/snake/main.lua +++ b/examples/snake/main.lua @@ -216,9 +216,7 @@ function RL.draw() RL.DrawText( 0, "Press Enter to\nrestart", { 10, 10 }, 10, 2, RL.WHITE ) end RL.EndTextureMode() - + -- Draw framebuffer to window. - RL.SetTextureSource( RL.TEXTURE_SOURCE_RENDER_TEXTURE ) RL.DrawTexturePro( framebuffer, { 0, 0, RESOLUTION[1], -RESOLUTION[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, RL.WHITE ) - RL.SetTextureSource( RL.TEXTURE_SOURCE_TEXTURE ) end diff --git a/include/state.h b/include/state.h index eeef14a..e1cbaed 100644 --- a/include/state.h +++ b/include/state.h @@ -8,12 +8,17 @@ typedef struct { } ModelAnimations; typedef struct { + int type; + Texture texture; + RenderTexture renderTexture; +} ReiTexture; + +typedef struct { char *exePath; bool hasWindow; bool run; lua_State *luaState; Vector2 resolution; - int textureSource; size_t guiFont; /* Resources. */ /* Images. */ @@ -21,13 +26,9 @@ typedef struct { size_t imageCount; size_t imageAlloc; /* Textures. */ - Texture **textures; + ReiTexture **textures; size_t textureCount; size_t textureAlloc; - /* RenderTextures. */ - RenderTexture **renderTextures; - size_t renderTextureCount; - size_t renderTextureAlloc; /* Fonts. */ Font **fonts; size_t fontCount; diff --git a/include/textures.h b/include/textures.h index 950d91b..386e72d 100644 --- a/include/textures.h +++ b/include/textures.h @@ -1,13 +1,15 @@ #pragma once -enum TEXTURE_SOURCES { TEXTURE_SOURCE_TEXTURE, TEXTURE_SOURCE_RENDER_TEXTURE }; +/* TEXTURE_TYPE_ALL is internal and used for accepting every type. */ +enum TEXTURE_TYPE { TEXTURE_TYPE_TEXTURE, TEXTURE_TYPE_RENDER_TEXTURE, TEXTURE_TYPE_ALL }; /* Validators. */ bool validImage( size_t id ); -bool validTexture( size_t id ); +bool validTexture( size_t id, int type ); bool validRenderTexture( size_t id ); bool validSourceTexture( size_t id ); -Texture2D* texturesGetSourceTexture( size_t index ); +Texture2D* texturesGetSourceTexture( size_t id ); +void texturesFreeTexture( size_t id ); /* Image Loading. */ int ltexturesLoadImage( lua_State *L ); int ltexturesLoadImageFromTexture( lua_State *L ); @@ -74,7 +76,6 @@ int ltexturesLoadTextureFromImage( lua_State *L ); int ltexturesLoadTextureCubemap( lua_State *L ); int ltexturesLoadRenderTexture( lua_State *L ); int ltexturesUnloadTexture( lua_State *L ); -int ltexturesUnloadRenderTexture( lua_State *L ); int ltexturesUpdateTexture( lua_State *L ); int ltexturesUpdateTextureRec( lua_State *L ); /* Texture Drawing. */ @@ -84,8 +85,7 @@ int ltexturesDrawTexturePro( lua_State *L ); int ltexturesDrawTextureNPatch( lua_State *L ); int ltexturesBeginTextureMode( lua_State *L ); int ltexturesEndTextureMode( lua_State *L ); -int ltexturesSetTextureSource( lua_State *L ); -int ltexturesGetTextureSource( lua_State *L ); +int ltexturesGetTextureType( lua_State *L ); /* Texture Configuration. */ int ltexturesGenTextureMipmaps( lua_State *L ); int ltexturesSetTextureFilter( lua_State *L ); @@ -1247,11 +1247,12 @@ int lcoreSetShaderValueTexture( lua_State *L ) { int locIndex = lua_tointeger( L, 2 ); size_t textureId = lua_tointeger( L, 3 ); - if ( !validShader( shaderId ) || !validTexture( textureId ) ) { + if ( !validShader( shaderId ) || !validTexture( textureId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } - SetShaderValueTexture( *state->shaders[ shaderId ], locIndex, *state->textures[ textureId ] ); + // SetShaderValueTexture( *state->shaders[ shaderId ], locIndex, *state->textures[ textureId ] ); + SetShaderValueTexture( *state->shaders[ shaderId ], locIndex, *texturesGetSourceTexture( textureId ) ); lua_pushboolean( L, true ); return 1; @@ -31,7 +31,9 @@ int lglBlitFramebuffer( lua_State *L ) { int mask = lua_tointeger( L, 5 ); int filter = lua_tointeger( L, 6 ); - if ( ( !validRenderTexture( srcTexId ) && srcTexId != -1 ) && ( !validRenderTexture( dstTexId ) && dstTexId != -1 ) ) { + // if ( ( !validRenderTexture( srcTexId ) && srcTexId != -1 ) && ( !validRenderTexture( dstTexId ) && dstTexId != -1 ) ) { + if ( ( !validTexture( srcTexId, TEXTURE_TYPE_RENDER_TEXTURE ) && srcTexId != -1 ) + && ( !validTexture( dstTexId, TEXTURE_TYPE_RENDER_TEXTURE ) && dstTexId != -1 ) ) { lua_pushboolean( L, false ); return 1; } @@ -40,14 +42,14 @@ int lglBlitFramebuffer( lua_State *L ) { glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 ); } else { - glBindFramebuffer( GL_READ_FRAMEBUFFER, state->renderTextures[ srcTexId ]->id ); + glBindFramebuffer( GL_READ_FRAMEBUFFER, state->textures[ srcTexId ]->renderTexture.id ); } if ( dstTexId == -1 ) { glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); } else { - glBindFramebuffer( GL_DRAW_FRAMEBUFFER, state->renderTextures[ dstTexId ]->id ); + glBindFramebuffer( GL_DRAW_FRAMEBUFFER, state->textures[ dstTexId ]->renderTexture.id ); } glBlitFramebuffer( diff --git a/src/lua_core.c b/src/lua_core.c index 8aeeafe..020f3e5 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -369,8 +369,8 @@ void defineGlobals() { assignGlobalInt( NPATCH_THREE_PATCH_VERTICAL, "NPATCH_THREE_PATCH_VERTICAL" ); assignGlobalInt( NPATCH_THREE_PATCH_HORIZONTAL, "NPATCH_THREE_PATCH_HORIZONTAL" ); /* TextureModes */ - assignGlobalInt( TEXTURE_SOURCE_TEXTURE, "TEXTURE_SOURCE_TEXTURE" ); - assignGlobalInt( TEXTURE_SOURCE_RENDER_TEXTURE, "TEXTURE_SOURCE_RENDER_TEXTURE" ); + assignGlobalInt( TEXTURE_TYPE_TEXTURE, "TEXTURE_TYPE_TEXTURE" ); + assignGlobalInt( TEXTURE_TYPE_RENDER_TEXTURE, "TEXTURE_TYPE_RENDER_TEXTURE" ); /* Colors */ assignGlobalColor( LIGHTGRAY, "LIGHTGRAY" ); assignGlobalColor( GRAY, "GRAY" ); @@ -1000,7 +1000,6 @@ void luaRegister() { assingGlobalFunction( "LoadTextureCubemap", ltexturesLoadTextureCubemap ); assingGlobalFunction( "LoadRenderTexture", ltexturesLoadRenderTexture ); assingGlobalFunction( "UnloadTexture", ltexturesUnloadTexture ); - assingGlobalFunction( "UnloadRenderTexture", ltexturesUnloadRenderTexture ); assingGlobalFunction( "UpdateTexture", ltexturesUpdateTexture ); assingGlobalFunction( "UpdateTextureRec", ltexturesUpdateTextureRec ); /* Texture Drawing. */ @@ -1010,8 +1009,7 @@ void luaRegister() { assingGlobalFunction( "DrawTextureNPatch", ltexturesDrawTextureNPatch ); assingGlobalFunction( "BeginTextureMode", ltexturesBeginTextureMode ); assingGlobalFunction( "EndTextureMode", ltexturesEndTextureMode ); - assingGlobalFunction( "SetTextureSource", ltexturesSetTextureSource ); - assingGlobalFunction( "GetTextureSource", ltexturesGetTextureSource ); + assingGlobalFunction( "GetTextureType", ltexturesGetTextureType ); /* Texture Configuration. */ assingGlobalFunction( "GenTextureMipmaps", ltexturesGenTextureMipmaps ); assingGlobalFunction( "SetTextureFilter", ltexturesSetTextureFilter ); diff --git a/src/models.c b/src/models.c index 6b0ff0a..3f69001 100644 --- a/src/models.c +++ b/src/models.c @@ -665,7 +665,7 @@ int lmodelDrawQuad3DTexture( lua_State *L ) { //TODO Normals. maybe something like Vector3Normalize(Vector3CrossProduct(Vector3Subtract(vB, vA), Vector3Subtract(vC, vA))); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -1138,7 +1138,6 @@ int lmodelsGenMeshCustom( lua_State *L ) { } lua_pop( L, 1 ); } - bool dynamic = lua_toboolean( L, 2 ); UploadMesh( &mesh, dynamic ); @@ -1575,7 +1574,7 @@ int lmodelsCreateMaterial( lua_State *L ) { if ( strcmp( "texture", (char*)lua_tostring( L, -2 ) ) == 0 && lua_isnumber( L, -1 ) ) { size_t texId = lua_tointeger( L, -1 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -1669,7 +1668,7 @@ int lmodelsSetMaterialTexture( lua_State *L ) { int mapType = lua_tointeger( L, 2 ); size_t texId = lua_tointeger( L, 3 ); - if ( !validMaterial( materialId ) || !validSourceTexture( texId ) ) { + if ( !validMaterial( materialId ) || !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -2014,7 +2013,7 @@ int lmodelsDrawBillboard( lua_State *L ) { float size = lua_tonumber( L, 4 ); Color tint = uluaGetColorIndex( L, 5 ); - if ( !validSourceTexture( texId ) || !validCamera3D( cameraId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) || !validCamera3D( cameraId ) ) { lua_pushboolean( L, false ); return 1; } @@ -2046,7 +2045,7 @@ int lmodelsDrawBillboardRec( lua_State *L ) { Vector2 size = uluaGetVector2Index( L, 5 ); Color tint = uluaGetColorIndex( L, 6 ); - if ( !validSourceTexture( texId ) || !validCamera3D( cameraId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) || !validCamera3D( cameraId ) ) { lua_pushboolean( L, false ); return 1; } diff --git a/src/shapes.c b/src/shapes.c index ef857ea..3a06bac 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -26,7 +26,7 @@ int lshapesSetShapesTexture( lua_State *L ) { size_t texId = lua_tointeger( L, 1 ); Rectangle source = uluaGetRectangleIndex( L, 2 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } diff --git a/src/state.c b/src/state.c index ed96115..6db0812 100644 --- a/src/state.c +++ b/src/state.c @@ -16,7 +16,6 @@ bool stateInit( const char *exePath ) { state->run = true; state->resolution = (Vector2){ 800, 600 }; state->luaState = NULL; - state->textureSource = TEXTURE_SOURCE_TEXTURE; state->guiFont = 0; /* Images. */ state->imageAlloc = ALLOC_PAGE_SIZE; @@ -25,11 +24,7 @@ bool stateInit( const char *exePath ) { /* Textures. */ state->textureAlloc = ALLOC_PAGE_SIZE; state->textureCount = 0; - state->textures = malloc( state->textureAlloc * sizeof( Texture2D* ) ); - /* RenderTextures. */ - state->renderTextureAlloc = ALLOC_PAGE_SIZE; - state->renderTextureCount = 0; - state->renderTextures = malloc( state->renderTextureAlloc * sizeof( RenderTexture2D* ) ); + state->textures = malloc( state->textureAlloc * sizeof( ReiTexture* ) ); /* Fonts. */ state->fontAlloc = ALLOC_PAGE_SIZE; state->fontCount = 1; @@ -82,7 +77,6 @@ bool stateInit( const char *exePath ) { for ( int i = 0; i < ALLOC_PAGE_SIZE; i++ ) { state->images[i] = NULL; state->textures[i] = NULL; - state->renderTextures[i] = NULL; state->waves[i] = NULL; state->sounds[i] = NULL; state->camera2Ds[i] = NULL; @@ -133,16 +127,10 @@ void stateFree() { } for ( int i = 0; i < state->textureCount; ++i ) { if ( state->textures[i] != NULL ) { - UnloadTexture( *state->textures[i] ); + texturesFreeTexture(i); free( state->textures[i] ); } } - for ( int i = 0; i < state->renderTextureCount; ++i ) { - if ( state->renderTextures[i] != NULL ) { - UnloadRenderTexture( *state->renderTextures[i] ); - free( state->renderTextures[i] ); - } - } for ( int i = 0; i < state->fontCount; ++i ) { if ( state->fonts[i] != NULL ) { UnloadFont( *state->fonts[i] ); @@ -232,7 +220,6 @@ void stateFree() { } free( state->images ); free( state->textures ); - free( state->renderTextures ); free( state->fonts ); free( state->waves ); free( state->sounds ); diff --git a/src/textures.c b/src/textures.c index b96bb5b..4def06e 100644 --- a/src/textures.c +++ b/src/textures.c @@ -26,7 +26,7 @@ static void checkTextureRealloc( int i ) { if ( state->textureCount == state->textureAlloc ) { state->textureAlloc += ALLOC_PAGE_SIZE; - state->textures = realloc( state->textures, state->textureAlloc * sizeof( Texture2D* ) ); + state->textures = realloc( state->textures, state->textureAlloc * sizeof( ReiTexture* ) ); for ( i = state->textureCount; i < state->textureAlloc; i++ ) { state->textures[i] = NULL; @@ -34,21 +34,6 @@ static void checkTextureRealloc( int i ) { } } -static void checkRenderTextureRealloc( int i ) { - if ( i == state->renderTextureCount ) { - state->renderTextureCount++; - } - - if ( state->renderTextureCount == state->renderTextureAlloc ) { - state->renderTextureAlloc += ALLOC_PAGE_SIZE; - state->renderTextures = realloc( state->renderTextures, state->renderTextureAlloc * sizeof( RenderTexture2D* ) ); - - for ( i = state->renderTextureCount; i < state->renderTextureAlloc; i++ ) { - state->renderTextures[i] = NULL; - } - } -} - bool validImage( size_t id ) { if ( id < 0 || state->imageCount < id || state->images[ id ] == NULL ) { TraceLog( LOG_WARNING, "%s %d", "Invalid image", id ); @@ -59,19 +44,13 @@ bool validImage( size_t id ) { } } -bool validTexture( size_t id ) { +bool validTexture( size_t id, int type ) { if ( id < 0 || state->textureCount < id || state->textures[ id ] == NULL ) { TraceLog( LOG_WARNING, "%s %d", "Invalid texture", id ); return false; } - else { - return true; - } -} - -bool validRenderTexture( size_t id ) { - if ( id < 0 || state->renderTextureCount < id || state->renderTextures[ id ] == NULL ) { - TraceLog( LOG_WARNING, "%s %d", "Invalid renderTexture", id ); + else if ( type != TEXTURE_TYPE_ALL && type != state->textures[ id ]->type ) { + TraceLog( LOG_WARNING, "%s %d", "Wrong texture type", type ); return false; } else { @@ -79,18 +58,6 @@ bool validRenderTexture( size_t id ) { } } -bool validSourceTexture( size_t id ) { - switch ( state->textureSource ) { - case TEXTURE_SOURCE_TEXTURE: - return validTexture( id ); - case TEXTURE_SOURCE_RENDER_TEXTURE: - return validRenderTexture( id ); - default: - return validTexture( id ); - break; - } -} - static int newImage() { int i = 0; @@ -105,7 +72,7 @@ static int newImage() { return i; } -static int newTexture() { +static int newTexture( int type ) { int i = 0; for ( i = 0; i < state->textureCount; i++ ) { @@ -113,24 +80,39 @@ static int newTexture() { break; } } - state->textures[i] = malloc( sizeof( Texture2D ) ); + state->textures[i] = malloc( sizeof( ReiTexture ) ); + state->textures[i]->type = type; + checkTextureRealloc( i ); return i; } -Texture2D* texturesGetSourceTexture( size_t index ) { - switch ( state->textureSource ) { - case TEXTURE_SOURCE_TEXTURE: - return state->textures[ index ]; - case TEXTURE_SOURCE_RENDER_TEXTURE: - return &state->renderTextures[ index ]->texture; - default: - return state->textures[ index ]; - break; +Texture2D* texturesGetSourceTexture( size_t id ) { + if ( state->textures[id] != NULL ) { + switch ( state->textures[id]->type ) { + case TEXTURE_TYPE_TEXTURE: + return &state->textures[id]->texture; + break; + case TEXTURE_TYPE_RENDER_TEXTURE: + return &state->textures[id]->renderTexture.texture; + break; + } } } +void texturesFreeTexture( size_t id ) { + if ( state->textures[id] != NULL ) { + switch ( state->textures[id]->type ) { + case TEXTURE_TYPE_TEXTURE: + UnloadTexture( state->textures[id]->texture ); + break; + case TEXTURE_TYPE_RENDER_TEXTURE: + UnloadRenderTexture( state->textures[id]->renderTexture ); + break; + } + } +} /* ## Textures - Image Loading */ @@ -178,7 +160,7 @@ int ltexturesLoadImageFromTexture( lua_State *L ) { } size_t texId = lua_tointeger( L, 1 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushinteger( L, -1 ); return 1; } @@ -1654,10 +1636,9 @@ int ltexturesLoadTexture( lua_State *L ) { lua_pushinteger( L, -1 ); return 1; } - if ( FileExists( lua_tostring( L, 1 ) ) ) { - int i = newTexture(); - *state->textures[i] = LoadTexture( lua_tostring( L, 1 ) ); + int i = newTexture( TEXTURE_TYPE_TEXTURE ); + state->textures[i]->texture = LoadTexture( lua_tostring( L, 1 ) ); lua_pushinteger( L, i ); return 1; } @@ -1687,8 +1668,8 @@ int ltexturesLoadTextureFromImage( lua_State *L ) { lua_pushboolean( L, false ); return 1; } - int i = newTexture(); - *state->textures[i] = LoadTextureFromImage( *state->images[ imageId ] ); + int i = newTexture( TEXTURE_TYPE_TEXTURE ); + state->textures[i]->texture = LoadTextureFromImage( *state->images[ imageId ] ); lua_pushinteger( L, i ); return 1; @@ -1715,8 +1696,8 @@ int ltexturesLoadTextureCubemap( lua_State *L ) { lua_pushboolean( L, false ); return 1; } - int i = newTexture(); - *state->textures[i] = LoadTextureCubemap( *state->images[ imageId ], layout ); + int i = newTexture( TEXTURE_TYPE_TEXTURE ); + state->textures[i]->texture = LoadTextureCubemap( *state->images[ imageId ], layout ); lua_pushinteger( L, i ); return 1; @@ -1737,17 +1718,10 @@ int ltexturesLoadRenderTexture( lua_State *L ) { return 1; } Vector2 size = uluaGetVector2Index( L, 1 ); - int i = 0; - for ( i = 0; i < state->renderTextureCount; i++ ) { - if ( state->renderTextures[i] == NULL ) { - break; - } - } - state->renderTextures[i] = malloc( sizeof( RenderTexture2D ) ); - *state->renderTextures[i] = LoadRenderTexture( (int)size.x, (int)size.y ); + int i = newTexture( TEXTURE_TYPE_RENDER_TEXTURE ); + state->textures[i]->renderTexture = LoadRenderTexture( (int)size.x, (int)size.y ); lua_pushinteger( L, i ); - checkRenderTextureRealloc( i ); return 1; } @@ -1768,39 +1742,13 @@ int ltexturesUnloadTexture( lua_State *L ) { } size_t id = lua_tointeger( L, 1 ); - if ( !validTexture( id ) ) { - lua_pushboolean( L, false ); - return 1; - } - UnloadTexture( *state->textures[ id ] ); - state->textures[ id ] = NULL; - lua_pushboolean( L, true ); - - return 1; -} - -/* -> success = RL.UnloadRenderTexture( RenderTexture2D target ) - -Unload render texture from GPU memory ( VRAM ) - -- Failure return false -- Success return true -*/ -int ltexturesUnloadRenderTexture( lua_State *L ) { - if ( !lua_isnumber( L, 1 ) ) { - TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.UnloadRenderTexture( RenderTexture2D target )" ); - lua_pushboolean( L, false ); - return 1; - } - size_t id = lua_tointeger( L, 1 ); - - if ( !validRenderTexture( id ) ) { + if ( !validTexture( id, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } - UnloadRenderTexture( *state->renderTextures[ id ] ); - state->renderTextures[ id ] = NULL; + // UnloadTexture( *state->textures[ id ] ); + texturesFreeTexture( id ); + // state->textures[ id ] = NULL; lua_pushboolean( L, true ); return 1; @@ -1810,7 +1758,7 @@ int ltexturesUnloadRenderTexture( lua_State *L ) { > success = RL.UpdateTexture( Texture2D texture, int{} pixels ) Update GPU texture with new data -NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format +NOTE! Should be TEXTURE_TYPE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format - Failure return false - Success return true @@ -1823,7 +1771,7 @@ int ltexturesUpdateTexture( lua_State *L ) { } size_t texId = lua_tointeger( L, 1 ); - if ( !validTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_TEXTURE ) ) { lua_pushboolean( L, false ); return 1; } @@ -1850,7 +1798,8 @@ int ltexturesUpdateTexture( lua_State *L ) { i++; lua_pop( L, 1 ); } - UpdateTexture( *state->textures[ texId ], pixels ); + // UpdateTexture( *state->textures[ texId ], pixels ); + UpdateTexture( state->textures[ texId ]->texture, pixels ); lua_pushboolean( L, true ); free( pixels ); @@ -1862,7 +1811,7 @@ int ltexturesUpdateTexture( lua_State *L ) { > success = RL.UpdateTextureRec( Texture2D texture, Rectangle rec, int{} pixels ) Update GPU texture rectangle with new data -NOTE! Should be TEXTURE_SOURCE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format +NOTE! Should be TEXTURE_TYPE_TEXTURE. Pixel should be in format { { 255, 255, 255, 255 }... } depending on the pixel format - Failure return false - Success return true @@ -1875,7 +1824,7 @@ int ltexturesUpdateTextureRec( lua_State *L ) { } size_t texId = lua_tointeger( L, 1 ); - if ( !validTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_TEXTURE ) ) { lua_pushboolean( L, false ); return 1; } @@ -1906,7 +1855,7 @@ int ltexturesUpdateTextureRec( lua_State *L ) { Rectangle rec = uluaGetRectangleIndex( L, 2 ); - UpdateTextureRec( *state->textures[ texId ], rec, pixels ); + UpdateTextureRec( state->textures[ texId ]->texture, rec, pixels ); lua_pushboolean( L, true ); free( pixels ); @@ -1936,7 +1885,7 @@ int ltexturesDrawTexture( lua_State *L ) { Vector2 pos = uluaGetVector2Index( L, 2 ); Color color = uluaGetColorIndex( L, 3 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -1966,7 +1915,7 @@ int ltexturesDrawTextureRec( lua_State *L ) { Vector2 pos = uluaGetVector2Index( L, 3 ); Color tint = uluaGetColorIndex( L, 4 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -1999,7 +1948,7 @@ int ltexturesDrawTexturePro( lua_State *L ) { float rot = lua_tonumber( L, 5 ); Color color = uluaGetColorIndex( L, 6 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -2032,7 +1981,7 @@ int ltexturesDrawTextureNPatch( lua_State *L ) { float rotation = lua_tonumber( L, 5 ); Color tint = uluaGetColorIndex( L, 6 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -2058,12 +2007,12 @@ int ltexturesBeginTextureMode( lua_State *L ) { } size_t texId = lua_tointeger( L, 1 ); - if ( !validRenderTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_RENDER_TEXTURE ) ) { lua_pushboolean( L, false ); return 1; } - BeginTextureMode( *state->renderTextures[ texId ] ); + BeginTextureMode( state->textures[ texId ]->renderTexture ); lua_pushboolean( L, true ); return 1; @@ -2081,42 +2030,26 @@ int ltexturesEndTextureMode( lua_State *L ) { } /* -> success = RL.SetTextureSource( int textureSource ) +> type = RL.GetTextureType( Texture2D texture ) -Set what texture source to use ( TEXTURE_SOURCE_TEXTURE or TEXTURE_SOURCE_RENDER_TEXTURE ) +Get texture type ( TEXTURE_TYPE_TEXTURE or TEXTURE_TYPE_RENDER_TEXTURE ) - Failure return false -- Success return true +- Success return int */ -int ltexturesSetTextureSource( lua_State *L ) { +int ltexturesGetTextureType( lua_State *L ) { if ( !lua_isnumber( L, 1 ) ) { - TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.SetTextureSource( int textureSource )" ); + TraceLog( LOG_WARNING, "%s", "Bad call of function. RL.GetTextureType( Texture2D texture )" ); lua_pushboolean( L, false ); return 1; } - int texSource = lua_tointeger( L, 1 ); + size_t texId = lua_tointeger( L, 1 ); - if ( texSource != TEXTURE_SOURCE_TEXTURE && texSource != TEXTURE_SOURCE_RENDER_TEXTURE ) { - TraceLog( LOG_WARNING, "%s %d", "Invalid source texture", texSource ); + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } - - state->textureSource = texSource; - lua_pushboolean( L, true ); - - return 1; -} - -/* -> textureSource = RL.GetTextureSource() - -Get current texture source type ( TEXTURE_SOURCE_TEXTURE or TEXTURE_SOURCE_RENDER_TEXTURE ) - -- Success return int -*/ -int ltexturesGetTextureSource( lua_State *L ) { - lua_pushinteger( L, state->textureSource ); + lua_pushinteger( L, state->textures[ texId ]->type ); return 1; } @@ -2141,11 +2074,10 @@ int ltexturesGenTextureMipmaps( lua_State *L ) { } size_t texId = lua_tointeger( L, 1 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } - GenTextureMipmaps( texturesGetSourceTexture( texId ) ); lua_pushboolean( L, true ); @@ -2169,7 +2101,7 @@ int ltexturesSetTextureFilter( lua_State *L ) { size_t texId = lua_tointeger( L, 1 ); int filter = lua_tointeger( L, 2 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -2197,7 +2129,7 @@ int ltexturesSetTextureWrap( lua_State *L ) { size_t texId = lua_tointeger( L, 1 ); int wrap = lua_tointeger( L, 2 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } @@ -2224,7 +2156,7 @@ int ltexturesGetTextureSize( lua_State *L ) { } size_t texId = lua_tointeger( L, 1 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushnil( L ); return 1; } @@ -2568,7 +2500,7 @@ int ltexturesGetPixelColor( lua_State *L ) { size_t texId = lua_tointeger( L, 1 ); Vector2 pos = uluaGetVector2Index( L, 2 ); - if ( !validSourceTexture( texId ) ) { + if ( !validTexture( texId, TEXTURE_TYPE_ALL ) ) { lua_pushboolean( L, false ); return 1; } |
