Object libraries like Vector2 optimizations.

This commit is contained in:
jussi
2024-04-17 00:35:58 +03:00
parent 41b6739824
commit 70b40f6782
31 changed files with 527 additions and 370 deletions

View File

@@ -47,41 +47,33 @@ local metatable = {
}
function Color:new( r, g, b, a )
if type( r ) == "table" then
r, g, b, a = table.unpack( r )
elseif type( r ) == "nil" then
r, g, b, a = 0, 0, 0, 255
end
if a == nil then
a = 255
end
local object = setmetatable( {}, metatable )
object.r = r
object.g = g
object.b = b
object.a = a
object.r = r or 255
object.g = g or 255
object.b = b or 255
object.a = a or 255
return object
return object
end
function Color:newT( t )
local object = setmetatable( {}, metatable )
object.r, object.g, object.b, object.a = table.unpack( t )
return object
end
function Color:set( r, g, b, a )
if type( r ) == "table" then
r, g, b, a = table.unpack( r )
elseif type( r ) == "nil" then
r, g, b, a = 0, 0, 0, 255
end
self.r = r or 255
self.g = g or 255
self.b = b or 255
self.a = a or 255
end
if a == nil then
a = 255
end
self.r = r
self.g = g
self.b = b
self.a = a
function Color:setT( t )
self.r, self.g, self.b, self.a = table.unpack( t )
end
function Color:arr()
@@ -101,7 +93,7 @@ function Color:scale( scalar )
end
function Color:fade( alpha )
return Color:new( RL.Fade( self, alpha ) )
return Color:newT( RL.Fade( self, alpha ) )
end
function Color:toHex()
@@ -109,7 +101,7 @@ function Color:toHex()
end
function Color:fromHex( hexValue )
return Color:new( RL.GetColor( hexValue ) )
return Color:newT( RL.GetColor( hexValue ) )
end
function Color:getNormalized()
@@ -117,31 +109,31 @@ function Color:getNormalized()
end
function Color:fromNormalized( normalized )
return Color:new( RL.ColorFromNormalized( normalized ) )
return Color:newT( RL.ColorFromNormalized( normalized ) )
end
function Color:toHSV()
return Vector3:new( RL.ColorToHSV( self ) )
return Vector3:newT( RL.ColorToHSV( self ) )
end
function Color:fromHSV( hue, saturation, value )
return Color:new( RL.ColorFromHSV( hue, saturation, value ) )
return Color:newT( RL.ColorFromHSV( hue, saturation, value ) )
end
function Color:tint( tint )
return Color:new( RL.ColorTint( self, tint ) )
return Color:newT( RL.ColorTint( self, tint ) )
end
function Color:brightness( factor )
return Color:new( RL.ColorBrightness( self, factor ) )
return Color:newT( RL.ColorBrightness( self, factor ) )
end
function Color:contrast( contrast )
return Color:new( RL.ColorContrast( self, contrast ) )
return Color:newT( RL.ColorContrast( self, contrast ) )
end
function Color:alphaBlend( dst, src, tint )
return Color:new( RL.ColorAlphaBlend( dst, src, tint ) )
return Color:newT( RL.ColorAlphaBlend( dst, src, tint ) )
end
function Color:lerp( color, amount )
@@ -153,4 +145,47 @@ function Color:lerp( color, amount )
)
end
local TEMP_COUNT = 100
local tempPool = {}
local curTemp = 1
for _ = 1, TEMP_COUNT do
table.insert( tempPool, Color:new( 255, 255, 255, 255 ) )
end
-- Uses pre generated objects to avoid "slow" table generation.
function Color:temp( r, g, b, a )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then
curTemp = 1
end
object.r = r or 255
object.g = g or 255
object.b = b or 255
object.a = a or 255
return object
end
-- Uses pre generated objects to avoid "slow" table generation.
function Color:tempT( t )
local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then
curTemp = 1
end
object.r, object.g, object.b, object.a = table.unpack( t )
return object
end
function Color:getTempId()
return curTemp
end
return Color