summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjussi2022-05-25 23:17:13 +0300
committerjussi2022-05-25 23:17:13 +0300
commit3aec701f80478e6d26f132486163b62451e3d0e9 (patch)
tree633723c5800e3a86d02dc56b95e59e7776644ef9
parent06f99406824b8bb03db17029279a0d139808cf6c (diff)
downloadreilua-enhanced-3aec701f80478e6d26f132486163b62451e3d0e9.tar.gz
reilua-enhanced-3aec701f80478e6d26f132486163b62451e3d0e9.tar.bz2
reilua-enhanced-3aec701f80478e6d26f132486163b62451e3d0e9.zip
Texture update functions.
-rw-r--r--API.md116
-rw-r--r--README.md2
-rw-r--r--include/textures.h15
-rw-r--r--src/lua_core.c15
-rw-r--r--src/textures.c391
5 files changed, 339 insertions, 200 deletions
diff --git a/API.md b/API.md
index e5528cd..d2ead49 100644
--- a/API.md
+++ b/API.md
@@ -2123,7 +2123,7 @@ Get collision rectangle for two rectangles collision
---
-## Textures - Load
+## Textures - Image Loading
---
@@ -2180,51 +2180,6 @@ Export image as code file defining an array of bytes, returns true on success
---
-> texture = RL_LoadTexture( string fileName )
-
-Load texture from file into GPU memory ( VRAM )
-
-- Failure return -1
-- Success return int
-
----
-
-> texture = RL_LoadTextureFromImage( Image image )
-
-Load texture from image data
-
-- Failure return -1
-- Success return int
-
----
-
-> success = RL_UnloadTexture( Texture2D texture )
-
-Unload texture from GPU memory ( VRAM )
-
-- Failure return false
-- Success return true
-
----
-
-> renderTexture = RL_LoadRenderTexture( Vector2 size )
-
-Load texture for rendering ( framebuffer )
-
-- Failure return -1
-- Success return int
-
----
-
-> success = RL_UnloadRenderTexture( RenderTexture2D target )
-
-Unload render texture from GPU memory ( VRAM )
-
-- Failure return false
-- Success return true
-
----
-
## Textures - Image Generation
---
@@ -2664,6 +2619,75 @@ Get image data format ( PixelFormat type )
---
+## Textures - Texture Loading
+
+---
+
+> texture = RL_LoadTexture( string fileName )
+
+Load texture from file into GPU memory ( VRAM )
+
+- Failure return -1
+- Success return int
+
+---
+
+> texture = RL_LoadTextureFromImage( Image image )
+
+Load texture from image data
+
+- Failure return -1
+- Success return int
+
+---
+
+> success = RL_UnloadTexture( Texture2D texture )
+
+Unload texture from GPU memory ( VRAM )
+
+- Failure return false
+- Success return true
+
+---
+
+> renderTexture = RL_LoadRenderTexture( Vector2 size )
+
+Load texture for rendering ( framebuffer )
+
+- Failure return -1
+- Success return int
+
+---
+
+> 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
+
+- Failure return false
+- Success return true
+
+---
+
+> 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
+
+- Failure return false
+- Success return true
+
+---
+
## Textures - Texture Drawing
---
diff --git a/README.md b/README.md
index 47ca96c..2653e18 100644
--- a/README.md
+++ b/README.md
@@ -21,8 +21,6 @@ List of some MISSING features that are planned to be included. For specific func
* Core
* VR stereo config functions for VR simulator
-* Textures
- * Texture update functions
* Audio
* Wave
* AudioStream management functions
diff --git a/include/textures.h b/include/textures.h
index 06947f6..b0f1458 100644
--- a/include/textures.h
+++ b/include/textures.h
@@ -8,18 +8,13 @@ bool validTexture( size_t id );
bool validRenderTexture( size_t id );
bool validSourceTexture( size_t id );
Texture2D* texturesGetSourceTexture( size_t index );
-/* File. */
+/* Image Loading. */
int ltexturesLoadImage( lua_State *L );
int ltexturesLoadImageFromTexture( lua_State *L );
int ltexturesLoadImageFromScreen( lua_State *L );
int ltexturesUnloadImage( lua_State *L );
int ltexturesExportImage( lua_State *L );
int ltexturesExportImageAsCode( lua_State *L );
-int ltexturesLoadTexture( lua_State *L );
-int ltexturesLoadTextureFromImage( lua_State *L );
-int ltexturesUnloadTexture( lua_State *L );
-int ltexturesLoadRenderTexture( lua_State *L );
-int ltexturesUnloadRenderTexture( lua_State *L );
/* Image Generation. */
int ltexturesGenImageColor( lua_State *L );
int ltexturesGenImageGradientV( lua_State *L );
@@ -71,6 +66,14 @@ int ltexturesImageDrawTextEx( lua_State *L );
int ltexturesGetImageSize( lua_State *L );
int ltexturesGetImageMipmaps( lua_State *L );
int ltexturesGetImageFormat( lua_State *L );
+/* Texture Loading. */
+int ltexturesLoadTexture( lua_State *L );
+int ltexturesLoadTextureFromImage( lua_State *L );
+int ltexturesUnloadTexture( lua_State *L );
+int ltexturesLoadRenderTexture( lua_State *L );
+int ltexturesUnloadRenderTexture( lua_State *L );
+int ltexturesUpdateTexture( lua_State *L );
+int ltexturesUpdateTextureRec( lua_State *L );
/* Texture Drawing. */
int ltexturesDrawTexture( lua_State *L );
int ltexturesDrawTextureRec( lua_State *L );
diff --git a/src/lua_core.c b/src/lua_core.c
index f109f1b..65ed85b 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -651,18 +651,13 @@ void luaRegister() {
lua_register( L, "RL_GetCollisionRec", lshapesGetCollisionRec );
/* Textures. */
- /* File. */
+ /* Image Loading. */
lua_register( L, "RL_LoadImage", ltexturesLoadImage );
lua_register( L, "RL_LoadImageFromTexture", ltexturesLoadImageFromTexture );
lua_register( L, "RL_LoadImageFromScreen", ltexturesLoadImageFromScreen );
lua_register( L, "RL_UnloadImage", ltexturesUnloadImage );
lua_register( L, "RL_ExportImage", ltexturesExportImage );
lua_register( L, "RL_ExportImageAsCode", ltexturesExportImageAsCode );
- lua_register( L, "RL_LoadTexture", ltexturesLoadTexture );
- lua_register( L, "RL_LoadTextureFromImage", ltexturesLoadTextureFromImage );
- lua_register( L, "RL_UnloadTexture", ltexturesUnloadTexture );
- lua_register( L, "RL_LoadRenderTexture", ltexturesLoadRenderTexture );
- lua_register( L, "RL_UnloadRenderTexture", ltexturesUnloadRenderTexture );
/* Image Generation. */
lua_register( L, "RL_GenImageColor", ltexturesGenImageColor );
lua_register( L, "RL_GenImageGradientV", ltexturesGenImageGradientV );
@@ -714,6 +709,14 @@ void luaRegister() {
lua_register( L, "RL_GetImageSize", ltexturesGetImageSize );
lua_register( L, "RL_GetImageMipmaps", ltexturesGetImageMipmaps );
lua_register( L, "RL_GetImageFormat", ltexturesGetImageFormat );
+ /* Texture Loading. */
+ lua_register( L, "RL_LoadTexture", ltexturesLoadTexture );
+ lua_register( L, "RL_LoadTextureFromImage", ltexturesLoadTextureFromImage );
+ lua_register( L, "RL_UnloadTexture", ltexturesUnloadTexture );
+ lua_register( L, "RL_LoadRenderTexture", ltexturesLoadRenderTexture );
+ lua_register( L, "RL_UnloadRenderTexture", ltexturesUnloadRenderTexture );
+ lua_register( L, "RL_UpdateTexture", ltexturesUpdateTexture );
+ lua_register( L, "RL_UpdateTextureRec", ltexturesUpdateTextureRec );
/* Texture Drawing. */
lua_register( L, "RL_DrawTexture", ltexturesDrawTexture );
lua_register( L, "RL_DrawTextureRec", ltexturesDrawTextureRec );
diff --git a/src/textures.c b/src/textures.c
index afc20c4..f75a6f2 100644
--- a/src/textures.c
+++ b/src/textures.c
@@ -132,7 +132,7 @@ Texture2D* texturesGetSourceTexture( size_t index ) {
}
/*
-## Textures - Load
+## Textures - Image Loading
*/
/*
@@ -283,144 +283,6 @@ int ltexturesExportImageAsCode( lua_State *L ) {
}
/*
-> texture = RL_LoadTexture( string fileName )
-
-Load texture from file into GPU memory ( VRAM )
-
-- Failure return -1
-- Success return int
-*/
-int ltexturesLoadTexture( lua_State *L ) {
- if ( !lua_isstring( L, -1 ) ) {
- TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadTexture( string fileName )" );
- lua_pushinteger( L, -1 );
- return 1;
- }
-
- if ( FileExists( lua_tostring( L, -1 ) ) ) {
- int i = newTexture();
- *state->textures[i] = LoadTexture( lua_tostring( L, -1 ) );
- lua_pushinteger( L, i );
- return 1;
- }
- else {
- lua_pushinteger( L, -1 );
- return 1;
- }
-}
-
-/*
-> texture = RL_LoadTextureFromImage( Image image )
-
-Load texture from image data
-
-- Failure return -1
-- Success return int
-*/
-int ltexturesLoadTextureFromImage( lua_State *L ) {
- if ( !lua_isnumber( L, -1 ) ) {
- TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadTextureFromImage( Image image )" );
- lua_pushinteger( L, -1 );
- return 1;
- }
- size_t imageId = lua_tointeger( L, -1 );
-
- if ( !validImage( imageId ) ) {
- lua_pushboolean( L, false );
- return 1;
- }
- int i = newTexture();
- *state->textures[i] = LoadTextureFromImage( *state->images[ imageId ] );
- lua_pushinteger( L, i );
-
- return 1;
-}
-
-/*
-> success = RL_UnloadTexture( Texture2D texture )
-
-Unload texture from GPU memory ( VRAM )
-
-- Failure return false
-- Success return true
-*/
-int ltexturesUnloadTexture( lua_State *L ) {
- if ( !lua_isnumber( L, -1 ) ) {
- TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UnloadTexture( Texture2D texture )" );
- lua_pushboolean( L, false );
- return 1;
- }
- 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;
-}
-
-/*
-> renderTexture = RL_LoadRenderTexture( Vector2 size )
-
-Load texture for rendering ( framebuffer )
-
-- Failure return -1
-- Success return int
-*/
-int ltexturesLoadRenderTexture( lua_State *L ) {
- if ( !lua_istable( L, -1 ) ) {
- TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadRenderTexture( Vector2 size )" );
- lua_pushinteger( L, -1 );
- return 1;
- }
- Vector2 size = uluaGetVector2( L );
- 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 );
- lua_pushinteger( L, i );
- checkRenderTextureRealloc( i );
-
- 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 ) ) {
- lua_pushboolean( L, false );
- return 1;
- }
- UnloadRenderTexture( *state->renderTextures[ id ] );
- state->renderTextures[ id ] = NULL;
- lua_pushboolean( L, true );
-
- return 1;
-}
-
-/*
## Textures - Image Generation
*/
@@ -1789,6 +1651,256 @@ int ltexturesGetImageFormat( lua_State *L ) {
}
/*
+## Textures - Texture Loading
+*/
+
+/*
+> texture = RL_LoadTexture( string fileName )
+
+Load texture from file into GPU memory ( VRAM )
+
+- Failure return -1
+- Success return int
+*/
+int ltexturesLoadTexture( lua_State *L ) {
+ if ( !lua_isstring( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadTexture( string fileName )" );
+ lua_pushinteger( L, -1 );
+ return 1;
+ }
+
+ if ( FileExists( lua_tostring( L, -1 ) ) ) {
+ int i = newTexture();
+ *state->textures[i] = LoadTexture( lua_tostring( L, -1 ) );
+ lua_pushinteger( L, i );
+ return 1;
+ }
+ else {
+ lua_pushinteger( L, -1 );
+ return 1;
+ }
+}
+
+/*
+> texture = RL_LoadTextureFromImage( Image image )
+
+Load texture from image data
+
+- Failure return -1
+- Success return int
+*/
+int ltexturesLoadTextureFromImage( lua_State *L ) {
+ if ( !lua_isnumber( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadTextureFromImage( Image image )" );
+ lua_pushinteger( L, -1 );
+ return 1;
+ }
+ size_t imageId = lua_tointeger( L, -1 );
+
+ if ( !validImage( imageId ) ) {
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ int i = newTexture();
+ *state->textures[i] = LoadTextureFromImage( *state->images[ imageId ] );
+ lua_pushinteger( L, i );
+
+ return 1;
+}
+
+/*
+> success = RL_UnloadTexture( Texture2D texture )
+
+Unload texture from GPU memory ( VRAM )
+
+- Failure return false
+- Success return true
+*/
+int ltexturesUnloadTexture( lua_State *L ) {
+ if ( !lua_isnumber( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UnloadTexture( Texture2D texture )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ 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;
+}
+
+/*
+> renderTexture = RL_LoadRenderTexture( Vector2 size )
+
+Load texture for rendering ( framebuffer )
+
+- Failure return -1
+- Success return int
+*/
+int ltexturesLoadRenderTexture( lua_State *L ) {
+ if ( !lua_istable( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_LoadRenderTexture( Vector2 size )" );
+ lua_pushinteger( L, -1 );
+ return 1;
+ }
+ Vector2 size = uluaGetVector2( L );
+ 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 );
+ lua_pushinteger( L, i );
+ checkRenderTextureRealloc( i );
+
+ 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 ) ) {
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ UnloadRenderTexture( *state->renderTextures[ id ] );
+ state->renderTextures[ id ] = NULL;
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> 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
+
+- Failure return false
+- Success return true
+*/
+int ltexturesUpdateTexture( lua_State *L ) {
+ if ( !lua_isnumber( L, -2 ) || !lua_istable( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UpdateTexture( Texture2D texture, int pixels{ {} } )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ size_t texId = lua_tointeger( L, -2 );
+
+ if ( !validTexture( texId ) ) {
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ size_t len = uluaGetTableLen( L );
+ unsigned char *pixels = malloc( len * 4 * sizeof( unsigned char ) );
+
+ int t = lua_gettop( L );
+ int i = 0;
+ lua_pushnil( L );
+
+ while ( lua_next( L, t ) != 0 ) {
+ size_t colLen = uluaGetTableLen( L );
+
+ int t2 = lua_gettop( L );
+ int j = 0;
+ lua_pushnil( L );
+
+ while ( lua_next( L, t2 ) != 0 ) {
+ *( pixels + ( i * colLen ) + j ) = lua_tointeger( L, -1 );
+
+ j++;
+ lua_pop( L, 1 );
+ }
+ i++;
+ lua_pop( L, 1 );
+ }
+ UpdateTexture( *state->textures[ texId ], pixels );
+ lua_pushboolean( L, true );
+
+ free( pixels );
+
+ return 1;
+}
+
+/*
+> 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
+
+- Failure return false
+- Success return true
+*/
+int ltexturesUpdateTextureRec( lua_State *L ) {
+ if ( !lua_isnumber( L, -3 ) || !lua_istable( L, -2 ) || !lua_istable( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_UpdateTextureRec( Texture2D texture, Rectangle rec, int pixels{ {} } )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ size_t texId = lua_tointeger( L, -3 );
+
+ if ( !validTexture( texId ) ) {
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ size_t len = uluaGetTableLen( L );
+ unsigned char *pixels = malloc( len * 4 * sizeof( unsigned char ) );
+
+ int t = lua_gettop( L );
+ int i = 0;
+ lua_pushnil( L );
+
+ while ( lua_next( L, t ) != 0 ) {
+ size_t colLen = uluaGetTableLen( L );
+
+ int t2 = lua_gettop( L );
+ int j = 0;
+ lua_pushnil( L );
+
+ while ( lua_next( L, t2 ) != 0 ) {
+ *( pixels + ( i * colLen ) + j ) = lua_tointeger( L, -1 );
+
+ j++;
+ lua_pop( L, 1 );
+ }
+ i++;
+ lua_pop( L, 1 );
+ }
+ lua_pop( L, 1 ); /* Pixels arg. */
+
+ Rectangle rec = uluaGetRectangle( L );
+
+ UpdateTextureRec( *state->textures[ texId ], rec, pixels );
+ lua_pushboolean( L, true );
+
+ free( pixels );
+
+ return 1;
+}
+
+/*
## Textures - Texture Drawing
*/
@@ -2490,7 +2602,6 @@ int ltexturesGetPixelColor( lua_State *L ) {
lua_pop( L, 1 );
size_t texId = lua_tointeger( L, -2 );
- // if ( !validTexture( texId ) ) {
if ( !validSourceTexture( texId ) ) {
lua_pushboolean( L, false );
return 1;