More color and rectangle lib functionality.

This commit is contained in:
jussi
2023-11-10 00:18:34 +02:00
parent 8effd050ff
commit e06d98e0ed
6 changed files with 161 additions and 83 deletions

View File

@@ -50,6 +50,7 @@ DETAILED CHANGES:
- ADDED: LoadMaterials. - ADDED: LoadMaterials.
- CHANGED: Organized core functions. - CHANGED: Organized core functions.
- CHANGED: Organized shapes, textures, audio, text, lights and gui functions. - CHANGED: Organized shapes, textures, audio, text, lights and gui functions.
- ADDED: More color and rectangle lib functionality.
------------------------------------------------------------------------ ------------------------------------------------------------------------
Release: ReiLua version 0.5.0 Using Raylib 4.5 Release: ReiLua version 0.5.0 Using Raylib 4.5

View File

@@ -15,8 +15,6 @@ Backlog {
* More Textures management functions. * More Textures management functions.
* Shader buffer storage object management (ssbo). * Shader buffer storage object management (ssbo).
* Extend color lib functionality.
* Examples * Examples
* Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads. * Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads.
* Platformer example physics process for true framerate independence. * Platformer example physics process for true framerate independence.

View File

@@ -1,35 +1,40 @@
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
Color = {} Color = {}
Color.meta = { Color.meta = {
__index = Color, __index = Color,
__tostring = function( r ) __tostring = function( c )
return "{"..tostring( r.r )..", "..tostring( r.g )..", "..tostring( r.b )..", "..tostring( r.a ).."}" return "{"..math.floor( c.r )..", "..math.floor( c.g )..", "..math.floor( c.b )..", "..math.floor( c.a ).."}"
end,
__add = function( c1, c2 )
return Color:new( c1.r + c2.r, c1.g + c2.g, c1.b + c2.b, c1.a + c2.a )
end,
__sub = function( c1, c2 )
return Color:new( c1.r - c2.r, c1.g - c2.g, c1.b - c2.b, c1.a - c2.a )
end,
__mul = function( c1, c2 )
return Color:new( c1.r * c2.r, c1.g * c2.g, c1.b * c2.b, c1.a * c2.a )
end,
__div = function( c1, c2 )
return Color:new( c1.r / c2.r, c1.g / c2.g, c1.b / c2.b, c1.a / c2.a )
end,
__mod = function( c, v )
return Color:new( c.r % v, c.g % v, c.b % v, c.a % v )
end,
__pow = function( c, v )
return Color:new( c.r ^ v, c.g ^ v, c.b ^ v, c.a ^ v )
end,
__idiv = function( c, v )
return Color:new( c.r // v, c.g // v, c.b // v, c.a // v )
end,
__len = function( _ )
return 4
end, end,
-- __add = function( v1, v2 )
-- return Vector2:new( v1.x + v2.x, v1.y + v2.y )
-- end,
-- __sub = function( v1, v2 )
-- return Vector2:new( v1.x - v2.x, v1.y - v2.y )
-- end,
-- __mul = function( v1, v2 )
-- return Vector2:new( v1.x * v2.x, v1.y * v2.y )
-- end,
-- __div = function( v1, v2 )
-- return Vector2:new( v1.x / v2.x, v1.y / v2.y )
-- end,
-- __mod = function( v, value )
-- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) )
-- end,
-- __pow = function( v, value )
-- return Vector2:new( v.x ^ value, v.y ^ value )
-- end,
-- __unm = function( v )
-- return Vector2:new( -v.x, -v.y )
-- end,
-- __idiv = function( v, value )
-- return Vector2:new( v.x // value, v.y // value )
-- end,
__eq = function( c1, c2 ) __eq = function( c1, c2 )
return c1.r == c2.r and c1.g == c2.g and c1.b == c2.b return c1.r == c2.r and c1.g == c2.g and c1.b == c2.b and c1.a == c2.a
end, end,
} }
@@ -80,7 +85,55 @@ function Color:unpack()
end end
function Color:clone() function Color:clone()
return Color:new( self.r, self.g, self.b, self.a ) return Color:new( self )
end
function Color:scale( scalar )
return Color:new( math.floor( self.r * scalar ), math.floor( self.g * scalar ), math.floor( self.b * scalar ), math.floor( self.a * scalar ) )
end
function Color:fade( alpha )
return Color:new( RL.Fade( self, alpha ) )
end
function Color:toHex()
return RL.ColorToInt( self )
end
function Color:fromHex( hexValue )
return Color:new( RL.GetColor( hexValue ) )
end
function Color:getNormalized()
return RL.ColorNormalize( self )
end
function Color:fromNormalized( normalized )
return Color:new( RL.ColorFromNormalized( normalized ) )
end
function Color:toHSV()
return Vector3:new( RL.ColorToHSV( self ) )
end
function Color:fromHSV( hue, saturation, value )
return Color:new( RL.ColorFromHSV( hue, saturation, value ) )
end
function Color:tint( tint )
return Color:new( RL.ColorTint( self, tint ) )
end
function Color:brightness( factor )
return Color:new( RL.ColorBrightness( self, factor ) )
end
function Color:contrast( contrast )
return Color:new( RL.ColorContrast( self, contrast ) )
end
function Color:alphaBlend( dst, src, tint )
return Color:new( RL.ColorAlphaBlend( dst, src, tint ) )
end end
return Color return Color

View File

@@ -1,47 +1,45 @@
-- For luaJit compatibility.
if table.unpack == nil then
table.unpack = unpack
end
Rectangle = {} Rectangle = {}
-- Rectangle.TYPE = "Rectangle"
Rectangle.meta = { Rectangle.meta = {
__index = Rectangle, __index = Rectangle,
__tostring = function( r ) __tostring = function( r )
return "{"..tostring( r.x )..", "..tostring( r.y )..", "..tostring( r.width )..", "..tostring( r.height ).."}" return "{"..tostring( r.x )..", "..tostring( r.y )..", "..tostring( r.width )..", "..tostring( r.height ).."}"
end, end,
-- __add = function( v1, v2 ) __add = function( r1, r2 )
-- return Vector2:new( v1.x + v2.x, v1.y + v2.y ) return Rectangle:new( r1.x + r2.x, r1.y + r2.y, r1.width + r2.width, r1.height + r2.height )
-- end, end,
-- __sub = function( v1, v2 ) __sub = function( r1, r2 )
-- return Vector2:new( v1.x - v2.x, v1.y - v2.y ) return Rectangle:new( r1.x - r2.x, r1.y - r2.y, r1.width - r2.width, r1.height - r2.height )
-- end, end,
-- __mul = function( v1, v2 ) __mul = function( r1, r2 )
-- return Vector2:new( v1.x * v2.x, v1.y * v2.y ) return Rectangle:new( r1.x * r2.x, r1.y * r2.y, r1.width * r2.width, r1.height * r2.height )
-- end, end,
-- __div = function( v1, v2 ) __div = function( r1, r2 )
-- return Vector2:new( v1.x / v2.x, v1.y / v2.y ) return Rectangle:new( r1.x / r2.x, r1.y / r2.y, r1.width / r2.width, r1.height / r2.height )
-- end, end,
-- __mod = function( v, value ) __mod = function( r, v )
-- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) ) return Rectangle:new( math.fmod( r.x, v ), math.fmod( r.y, v ), math.fmod( r.width, v ), math.fmod( r.height, v ) )
-- end, end,
-- __pow = function( v, value ) __pow = function( r, v )
-- return Vector2:new( v.x ^ value, v.y ^ value ) return Rectangle:new( r.x ^ v, r.y ^ v, r.width ^ v, r.height ^ v )
-- end, end,
-- __unm = function( v ) __unm = function( r )
-- return Vector2:new( -v.x, -v.y ) return Rectangle:new( -r.x, -r.y, -r.width, -r.height )
-- end, end,
-- __idiv = function( v, value ) __idiv = function( r, v )
-- return Vector2:new( v.x // value, v.y // value ) return Rectangle:new( r.x // v, r.y // v, r.width // v, r.height // v )
-- end, end,
-- __len = function( v ) __len = function( _ )
-- local len = 0 return 4
end,
-- for _, _ in pairs( v ) do __eq = function( r1, r2 )
-- len = len + 1 return RL.Vector2Equals( { r1.x, r1.y }, { r2.x, r2.y } ) and RL.Vector2Equals( { r1.width, r1.height }, { r2.width, r2.height } )
-- end end,
-- return len
-- end,
-- __eq = function( v1, v2 )
-- return v1.x == v2.x and v1.y == v2.y
-- end,
} }
function Rectangle:new( x, y, width, height ) function Rectangle:new( x, y, width, height )
@@ -86,4 +84,44 @@ function Rectangle:clone()
return Rectangle:new( self.x, self.y, self.width, self.height ) return Rectangle:new( self.x, self.y, self.width, self.height )
end end
function Rectangle:scale( scalar )
return Rectangle:new( self.x, self.y, self.width * scalar, self.height * scalar )
end
function Rectangle:min( rec )
return Rectangle:new( self.x, self.y, math.min( self.width, rec.width ), math.min( self.height, rec.height ) )
end
function Rectangle:max( rec )
return Rectangle:new( self.x, self.y, math.max( self.width, rec.width ), math.max( self.height, rec.height ) )
end
function Rectangle:floor()
return Rectangle:new( math.floor( self.x ), math.floor( self.y ), math.floor( self.width ), math.floor( self.height ) )
end
function Rectangle:ceil()
return Rectangle:new( math.ceil( self.x ), math.ceil( self.y ), math.ceil( self.width ), math.ceil( self.height ) )
end
function Rectangle:area()
return self.width * self.height
end
function Rectangle:checkCollisionRec( rec )
return RL.CheckCollisionRecs( self, rec )
end
function Rectangle:checkCollisionCircle( center, radius )
return RL.CheckCollisionCircleRec( center, radius, self )
end
function Rectangle:checkCollisionPoint( point )
return RL.CheckCollisionPointRec( point, self )
end
function Rectangle:getCollisionRec( rec )
return Rectangle:new( RL.GetCollisionRec( self, rec ) )
end
return Rectangle return Rectangle

View File

@@ -30,14 +30,8 @@ Vector2.meta = {
__unm = function( v ) __unm = function( v )
return Vector2:new( -v.x, -v.y ) return Vector2:new( -v.x, -v.y )
end, end,
__len = function( v ) __len = function( _ )
local len = 0 return 2
for _, _ in pairs( v ) do
len = len + 1
end
return len
end, end,
__eq = function( v1, v2 ) __eq = function( v1, v2 )
return RL.Vector2Equals( v1, v2 ) == 1 return RL.Vector2Equals( v1, v2 ) == 1

View File

@@ -30,14 +30,8 @@ Vector3.meta = {
__unm = function( v ) __unm = function( v )
return Vector3:new( -v.x, -v.y, -v.z ) return Vector3:new( -v.x, -v.y, -v.z )
end, end,
__len = function( v ) __len = function( _ )
local len = 0 return 3
for _, _ in pairs( v ) do
len = len + 1
end
return len
end, end,
__eq = function( v1, v2 ) __eq = function( v1, v2 )
return RL.Vector3Equals( v1, v2 ) == 1 return RL.Vector3Equals( v1, v2 ) == 1