diff options
| -rw-r--r-- | API.md | 8 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | ReiLua_API.lua | 12 | ||||
| -rw-r--r-- | changelog | 2 | ||||
| -rw-r--r-- | devnotes | 1 | ||||
| -rw-r--r-- | examples/n-patches/main.lua | 40 | ||||
| -rw-r--r-- | examples/resources/images/LICENCE | 3 | ||||
| -rw-r--r-- | examples/resources/images/nPatch.png | bin | 0 -> 1131 bytes | |||
| -rw-r--r-- | examples/resources/lib/camera3d.lua | 6 | ||||
| -rw-r--r-- | include/textures.h | 1 | ||||
| -rw-r--r-- | src/lua_core.c | 1 | ||||
| -rw-r--r-- | src/models.c | 2 | ||||
| -rw-r--r-- | src/textures.c | 218 |
13 files changed, 278 insertions, 18 deletions
@@ -6125,6 +6125,12 @@ Draws a texture (or part of it) that stretches or shrinks nicely --- +> RL.DrawTextureNPatchRepeat( Texture texture, NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint ) + +Draws a texture (or part of it) that repeats nicely + +--- + ## Textures - RenderTexture configuration functions --- @@ -6554,7 +6560,7 @@ Draw a plane XZ > RL.DrawQuad3DTexture( Texture texture, Vector3{} vertices, Vector2{} texCoords, Color{} colors ) -Draw 3D textured quad. (Texture coordinates opengl style 0.0 - 1.0). +Draw 3D textured quad. (Texture coordinates opengl style 0.0 - 1.0) --- @@ -8,7 +8,7 @@ Reilua means fair in finnish. ## Status -ReiLua is WIP and some planned raylib functionality is still missing but it already has over 700 functions and should include all functions to make most 2d and 3d games. Current Raylib version 4.5.0. +ReiLua is WIP and some planned raylib functionality is still missing but it already has over 800 functions and should include all functions to make most 2D and 3D games. Current Raylib version 5.0. Included submodules. diff --git a/ReiLua_API.lua b/ReiLua_API.lua index b2d2787..9d7158f 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -3514,6 +3514,16 @@ function RL.DrawTexturePro( texture, source, dest, origin, rotation, tint ) end ---@return any RL.DrawTextureNPatch function RL.DrawTextureNPatch( texture, nPatchInfo, dest, origin, rotation, tint ) end +---Draws a texture (or part of it) that repeats nicely +---@param texture any +---@param nPatchInfo any +---@param dest table +---@param origin table +---@param rotation number +---@param tint table +---@return any RL.DrawTextureNPatchRepeat +function RL.DrawTextureNPatchRepeat( texture, nPatchInfo, dest, origin, rotation, tint ) end + -- Textures - RenderTexture configuration functions ---Get OpenGL framebuffer object id @@ -3955,7 +3965,7 @@ function RL.DrawCapsuleWires( startPos, endPos, radius, slices, rings, color ) ---@return any RL.DrawPlane function RL.DrawPlane( centerPos, size, color ) end ----Draw 3D textured quad. (Texture coordinates opengl style 0.0 - 1.0). +---Draw 3D textured quad. (Texture coordinates opengl style 0.0 - 1.0) ---@param texture any ---@param vertices table ---@param texCoords table @@ -25,6 +25,7 @@ KEY CHANGES: - ADDED: 2D lightmap example. - ADDED: RLGL Hello triangle example. - ADDED: Rest of rlRenderBatch functions. + - ADDED: DrawTextureNPatchRepeat. DETAILED CHANGES: - REMOVED: DrawLineBezierQuad, DrawLineBezierCubic. @@ -49,6 +50,7 @@ DETAILED CHANGES: - ADDED: GetShaderId. - FIXED: rlSetVertexAttribute pointer offset. - ADDED: GetImageData. + - FIXED: CameraLib. Slow lateral movement. ------------------------------------------------------------------------ Release: ReiLua version 0.6.0 Using Raylib 4.5 @@ -28,7 +28,6 @@ Backlog { } Bugs { - * CameraLib. Lateral movement is slower if looking down or up. } Needs Testing { diff --git a/examples/n-patches/main.lua b/examples/n-patches/main.lua index a4491bc..013a930 100644 --- a/examples/n-patches/main.lua +++ b/examples/n-patches/main.lua @@ -1,10 +1,19 @@ -local dstRec = { 160.0, 160.0, 8.0, 8.0 }; -local origin = { 0.0, 0.0 } +package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" --- local ninePatchInfo = { { 0.0, 0.0, 24.0, 24.0 }, 8, 8, 8, 8, NPATCH_NINE_PATCH } -local ninePatchInfo = { source = { 0, 0, 24.0, 24.0 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH } +Vec2 = require( "vector2" ) +Rect = require( "rectangle" ) -local nPatchTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/ui_border.png" ) +local dstRec = Rect:new( 100.0, 100.0, 8.0, 8.0 ) +local origin = Vec2:new( 0.0, 0.0 ) +local stretched = true + +-- local ninePatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH } +-- local nPatchTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/ui_border.png" ) + +local ninePatchInfo = { source = { 0, 0, 96, 96 }, left = 32, top = 32, right = 32, bottom = 32, layout = RL.NPATCH_NINE_PATCH } +-- local ninePatchInfo = { source = { 0, 0, 96, 96 }, left = 32, top = 32, right = 32, bottom = 32, layout = RL.NPATCH_THREE_PATCH_VERTICAL } +-- local ninePatchInfo = { source = { 0, 0, 96, 96 }, left = 32, top = 32, right = 32, bottom = 32, layout = RL.NPATCH_THREE_PATCH_HORIZONTAL } +local nPatchTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/nPatch.png" ) function RL.init() RL.SetWindowTitle( "N-Patches" ) @@ -12,14 +21,27 @@ function RL.init() end function RL.process( delta ) - local mousePosition = RL.GetMousePosition(); + local mousePosition = Vec2:new( RL.GetMousePosition() ) -- Resize the n-patch based on mouse position - dstRec[3] = mousePosition[1] - dstRec[1]; - dstRec[4] = mousePosition[2] - dstRec[2]; + dstRec.width = mousePosition.x - dstRec.x; + dstRec.height = mousePosition.y - dstRec.y; + + if RL.IsKeyPressed( RL.KEY_SPACE ) then + stretched = not stretched + end end function RL.draw() RL.ClearBackground( RL.RAYWHITE ) - RL.DrawTextureNPatch( nPatchTexture, ninePatchInfo, dstRec, origin, 0.0, RL.WHITE ) + local modeText = "Stretched" + + if stretched then + RL.DrawTextureNPatch( nPatchTexture, ninePatchInfo, dstRec, origin, 0.0, RL.WHITE ) + else + RL.DrawTextureNPatchRepeat( nPatchTexture, ninePatchInfo, dstRec, origin, 0.0, RL.WHITE ) + modeText = "Repeat" + end + + RL.DrawText( "Press space to toggle mode: "..modeText, { 20, 20 }, 20, RL.BLACK ) end diff --git a/examples/resources/images/LICENCE b/examples/resources/images/LICENCE index 414d253..8666043 100644 --- a/examples/resources/images/LICENCE +++ b/examples/resources/images/LICENCE @@ -7,7 +7,8 @@ snake.png Jussi Viitala CC0 ui_border.png Jussi Viitala CC0 ui_bgr.png Jussi Viitala CC0 gradient.png Jussi Viitala CC0 -light.png Jussi Viitala CC0 +light.png Jussi Viitala CC0 +nPatch.png Jussi Viitala CC0 check-mark.png Delapouite Creative Commons 3.0 https://game-icons.net Resized circle.png Delapouite Creative Commons 3.0 https://game-icons.net Resized plain-circle.png Delapouite Creative Commons 3.0 https://game-icons.net Resized diff --git a/examples/resources/images/nPatch.png b/examples/resources/images/nPatch.png Binary files differnew file mode 100644 index 0000000..9a6eaa6 --- /dev/null +++ b/examples/resources/images/nPatch.png diff --git a/examples/resources/lib/camera3d.lua b/examples/resources/lib/camera3d.lua index cea6e8d..8d3c884 100644 --- a/examples/resources/lib/camera3d.lua +++ b/examples/resources/lib/camera3d.lua @@ -135,11 +135,11 @@ function Camera3D:process( delta ) elseif RL.IsKeyDown( self.KEYS.BACKWARD ) then RL.Camera3DMoveForward( self.camera, -distance, false ) end - + if RL.IsKeyDown( self.KEYS.RIGHT ) then - RL.Camera3DMoveRight( self.camera, distance, false ) + RL.Camera3DMoveRight( self.camera, distance, true ) elseif RL.IsKeyDown( self.KEYS.LEFT ) then - RL.Camera3DMoveRight( self.camera, -distance, false ) + RL.Camera3DMoveRight( self.camera, -distance, true ) end elseif self.mode == self.MODES.ORBITAL then RL.Camera3DYaw( self.camera, self.ORBITAL_SPEED * delta, true ) diff --git a/include/textures.h b/include/textures.h index 288a536..b1d9b8f 100644 --- a/include/textures.h +++ b/include/textures.h @@ -97,6 +97,7 @@ int ltexturesDrawTexture( lua_State *L ); int ltexturesDrawTextureRec( lua_State *L ); int ltexturesDrawTexturePro( lua_State *L ); int ltexturesDrawTextureNPatch( lua_State *L ); +int ltexturesDrawTextureNPatchRepeat( lua_State *L ); /* RenderTexture configuration functions. */ int ltexturesGetRenderTextureId( lua_State *L ); int ltexturesGetRenderTextureTexture( lua_State *L ); diff --git a/src/lua_core.c b/src/lua_core.c index 61f08b5..0da51dc 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -1559,6 +1559,7 @@ void luaRegister() { assingGlobalFunction( "DrawTextureRec", ltexturesDrawTextureRec ); assingGlobalFunction( "DrawTexturePro", ltexturesDrawTexturePro ); assingGlobalFunction( "DrawTextureNPatch", ltexturesDrawTextureNPatch ); + assingGlobalFunction( "DrawTextureNPatchRepeat", ltexturesDrawTextureNPatchRepeat ); /* RenderTexture configuration functions. */ assingGlobalFunction( "GetRenderTextureId", ltexturesGetRenderTextureId ); assingGlobalFunction( "GetRenderTextureTexture", ltexturesGetRenderTextureTexture ); diff --git a/src/models.c b/src/models.c index 490ac13..b65fccf 100644 --- a/src/models.c +++ b/src/models.c @@ -377,7 +377,7 @@ int lmodelsDrawPlane( lua_State *L ) { /* > RL.DrawQuad3DTexture( Texture texture, Vector3{} vertices, Vector2{} texCoords, Color{} colors ) -Draw 3D textured quad. (Texture coordinates opengl style 0.0 - 1.0). +Draw 3D textured quad. (Texture coordinates opengl style 0.0 - 1.0) */ int lmodelDrawQuad3DTexture( lua_State *L ) { Texture *texture = uluaGetTexture( L, 1 ); diff --git a/src/textures.c b/src/textures.c index 8a99b34..24c8232 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1518,6 +1518,224 @@ int ltexturesDrawTextureNPatch( lua_State *L ) { } /* +> RL.DrawTextureNPatchRepeat( Texture texture, NPatchInfo nPatchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint ) + +Draws a texture (or part of it) that repeats nicely +*/ +inline static void drawNPatchTile( Vector4 src, Vector4 dst, Vector2 texSize ) { + rlTexCoord2f( src.x / texSize.x, src.w / texSize.y ); rlVertex2f( dst.x, dst.w ); // Bottom-left corner for texture and quad + rlTexCoord2f( src.z / texSize.x, src.w / texSize.y ); rlVertex2f( dst.z, dst.w ); // Bottom-right corner for texture and quad + rlTexCoord2f( src.z / texSize.x, src.y / texSize.y ); rlVertex2f( dst.z, dst.y ); // Top-right corner for texture and quad + rlTexCoord2f( src.x / texSize.x, src.y / texSize.y ); rlVertex2f( dst.x, dst.y ); // Top-left corner for texture and quad +} + +inline static void drawNPatchArea( Vector4 src, Vector4 dst, Vector2 texSize ) { + Vector2 tileSize = { src.z - src.x, src.w - src.y }; + Vector2 areaSize = { dst.z - dst.x, dst.w - dst.y }; + int width = ceil( areaSize.x / tileSize.x ); + int height = ceil( areaSize.y / tileSize.y ); + + for ( int y = 0; y < height; y++ ) { + for ( int x = 0; x < width; x++ ) { + Vector2 rTileSize = { + fmin( tileSize.x, areaSize.x - x * tileSize.x ), + fmin( tileSize.y, areaSize.y - y * tileSize.y ) + }; + Vector4 tileSrc = { + src.x, + src.y, + src.x + rTileSize.x, + src.y + rTileSize.y + }; + Vector4 tileDst = { + dst.x + x * tileSize.x, + dst.y + y * tileSize.y, + dst.x + x * tileSize.x + rTileSize.x, + dst.y + y * tileSize.y + rTileSize.y + }; + drawNPatchTile( tileSrc, tileDst, texSize ); + } + } +} + +int ltexturesDrawTextureNPatchRepeat( lua_State *L ) { + Texture *texture = uluaGetTexture( L, 1 ); + NPatchInfo nPatchInfo = uluaGetNPatchInfo( L, 2 ); + Rectangle dest = uluaGetRectangle( L, 3 ); + Vector2 origin = uluaGetVector2( L, 4 ); + float rotation = luaL_checknumber( L, 5 ); + Color tint = uluaGetColor( L, 6 ); + + if ( texture->id < 0 || dest.width <= 0 || dest.height <= 0 ) { + return 0; + } + Vector2 texSize = { texture->width, texture->height }; + float leftBorder = (float)nPatchInfo.left; + float topBorder = (float)nPatchInfo.top; + float rightBorder = (float)nPatchInfo.right; + float bottomBorder = (float)nPatchInfo.bottom; + bool drawHMiddle = leftBorder + rightBorder <= dest.width; + bool drawVMiddle = topBorder + bottomBorder <= dest.height; + bool drawCenter = drawHMiddle && drawVMiddle; + + if ( !drawHMiddle ) { + leftBorder = ( leftBorder / ( leftBorder + rightBorder ) ) * dest.width; + rightBorder = dest.width - leftBorder; + } + if ( !drawVMiddle ) { + topBorder = ( topBorder / ( topBorder + bottomBorder ) ) * dest.height; + bottomBorder = dest.height - topBorder; + } + Vector2 vertA, vertB, vertC, vertD; + vertA.x = 0.0f; // outer left + vertA.y = 0.0f; // outer top + vertB.x = leftBorder; // inner left + vertB.y = topBorder; // inner top + vertC.x = dest.width - rightBorder; // inner right + vertC.y = dest.height - bottomBorder; // inner bottom + vertD.x = dest.width; // outer right + vertD.y = dest.height; // outer bottom + + Vector2 coordA, coordB, coordC, coordD; + coordA.x = nPatchInfo.source.x; + coordA.y = nPatchInfo.source.y; + coordB.x = nPatchInfo.source.x + leftBorder; + coordB.y = nPatchInfo.source.y + topBorder; + coordC.x = nPatchInfo.source.x + nPatchInfo.source.width - rightBorder; + coordC.y = nPatchInfo.source.y + nPatchInfo.source.height - bottomBorder; + coordD.x = nPatchInfo.source.x + nPatchInfo.source.width; + coordD.y = nPatchInfo.source.y + nPatchInfo.source.height; + + rlSetTexture( texture->id ); + + rlPushMatrix(); + rlTranslatef( dest.x, dest.y, 0.0f ); + rlRotatef( rotation, 0.0f, 0.0f, 1.0f ); + rlTranslatef( -origin.x, -origin.y, 0.0f ); + + rlBegin( RL_QUADS ); + rlColor4ub( tint.r, tint.g, tint.b, tint.a ); + rlNormal3f( 0.0f, 0.0f, 1.0f ); // Normal vector pointing towards viewer + + switch ( nPatchInfo.layout ) { + case NPATCH_NINE_PATCH: + if ( drawCenter ) { + // MIDDLE-CENTER QUAD + drawNPatchArea( + (Vector4){ coordB.x, coordB.y, coordC.x, coordC.y }, + (Vector4){ vertB.x, vertB.y, vertC.x, vertC.y }, + texSize + ); + } + if ( drawHMiddle ) { + // TOP-CENTER QUAD + drawNPatchArea( + (Vector4){ coordB.x, coordA.y, coordC.x, coordB.y }, + (Vector4){ vertB.x, vertA.y, vertC.x, vertB.y }, + texSize + ); + // BOTTOM-CENTER QUAD + drawNPatchArea( + (Vector4){ coordB.x, coordC.y, coordC.x, coordD.y }, + (Vector4){ vertB.x, vertC.y, vertC.x, vertD.y }, + texSize + ); + } + if ( drawVMiddle ) { + // LEFT-CENTER QUAD + drawNPatchArea( + (Vector4){ coordA.x, coordB.y, coordB.x, coordC.y }, + (Vector4){ vertA.x, vertB.y, vertB.x, vertC.y }, + texSize + ); + // RIGHT-CENTER QUAD + drawNPatchArea( + (Vector4){ coordC.x, coordB.y, coordD.x, coordC.y }, + (Vector4){ vertC.x, vertB.y, vertD.x, vertC.y }, + texSize + ); + } + // TOP-LEFT QUAD + drawNPatchTile( + (Vector4){ coordA.x, coordA.y, coordB.x, coordB.y }, + (Vector4){ vertA.x, vertA.y, vertB.x, vertB.y }, + texSize + ); + // TOP-RIGHT QUAD + drawNPatchTile( + (Vector4){ coordC.x, coordA.y, coordD.x, coordB.y }, + (Vector4){ vertC.x, vertA.y, vertD.x, vertB.y }, + texSize + ); + // BOTTOM-LEFT QUAD + drawNPatchTile( + (Vector4){ coordA.x, coordC.y, coordB.x, coordD.y }, + (Vector4){ vertA.x, vertC.y, vertB.x, vertD.y }, + texSize + ); + // BOTTOM-RIGHT QUAD + drawNPatchTile( + (Vector4){ coordC.x, coordC.y, coordD.x, coordD.y }, + (Vector4){ vertC.x, vertC.y, vertD.x, vertD.y }, + texSize + ); + break; + case NPATCH_THREE_PATCH_VERTICAL: + // TOP QUAD + drawNPatchArea( + (Vector4){ coordA.x, coordA.y, coordD.x, coordB.y }, + (Vector4){ vertA.x, vertA.y, vertD.x, vertB.y }, + texSize + ); + if ( drawVMiddle ) { + // MIDDLE QUAD + drawNPatchArea( + (Vector4){ coordA.x, coordB.y, coordD.x, coordC.y }, + (Vector4){ vertA.x, vertB.y, vertD.x, vertC.y }, + texSize + ); + } + // BOTTOM QUAD + drawNPatchArea( + (Vector4){ coordA.x, coordC.y, coordD.x, coordD.y }, + (Vector4){ vertA.x, vertC.y, vertD.x, vertD.y }, + texSize + ); + break; + case NPATCH_THREE_PATCH_HORIZONTAL: + // LEFT QUAD + drawNPatchArea( + (Vector4){ coordA.x, coordA.y, coordB.x, coordD.y }, + (Vector4){ vertA.x, vertA.y, vertB.x, vertD.y }, + texSize + ); + if ( drawHMiddle ) { + // MIDDLE QUAD + drawNPatchArea( + (Vector4){ coordB.x, coordA.y, coordC.x, coordD.y }, + (Vector4){ vertB.x, vertA.y, vertC.x, vertD.y }, + texSize + ); + } + // RIGHT QUAD + drawNPatchArea( + (Vector4){ coordC.x, coordA.y, coordD.x, coordD.y }, + (Vector4){ vertC.x, vertA.y, vertD.x, vertD.y }, + texSize + ); + break; + default: + break; + } + + rlEnd(); + rlPopMatrix(); + rlSetTexture(0); + + return 0; +} + +/* ## Textures - RenderTexture configuration functions */ |
