From cf2c2eb05bd5d30169771b0087df84a53124f766 Mon Sep 17 00:00:00 2001 From: jussi Date: Wed, 20 Nov 2024 17:10:42 +0200 Subject: Type class updates. --- examples/resources/lib/color.lua | 23 ++++++++--- examples/resources/lib/matrix.lua | 74 +++++++++++++++++++++++------------ examples/resources/lib/quaternion.lua | 2 +- examples/resources/lib/rectangle.lua | 33 +++++++++++++++- 4 files changed, 100 insertions(+), 32 deletions(-) (limited to 'examples/resources') diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua index e16837b..ee69415 100644 --- a/examples/resources/lib/color.lua +++ b/examples/resources/lib/color.lua @@ -3,6 +3,8 @@ if table.unpack == nil then table.unpack = unpack end +local Vector3 = Vector3 or require( "vector3" ) + local Color = {} local metatable = { __index = Color, @@ -45,8 +47,8 @@ function Color:new( r, g, b, a ) local object = setmetatable( {}, metatable ) object.r = r or 255 - object.g = g or 255 - object.b = b or 255 + object.g = g or object.r + object.b = b or object.g object.a = a or 255 return object @@ -73,8 +75,8 @@ end function Color:set( r, g, b, a ) self.r = r or 255 - self.g = g or 255 - self.b = b or 255 + self.g = g or self.r + self.b = b or self.g self.a = a or 255 end @@ -171,6 +173,15 @@ function Color:round() ) end +function Color:mix( c ) + return Color:temp( + ( self.r + c.r ) / 2, + ( self.g + c.g ) / 2, + ( self.b + c.b ) / 2, + ( self.a + c.a ) / 2 + ):round() +end + function Color:invert() return Color:new( 255 - self.r, 255 - self.g, 255 - self.b, self.a ) end @@ -191,8 +202,8 @@ function Color:temp( r, g, b, a ) curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.r = r or 255 - object.g = g or 255 - object.b = b or 255 + object.g = g or object.r + object.b = b or object.g object.a = a or 255 return object diff --git a/examples/resources/lib/matrix.lua b/examples/resources/lib/matrix.lua index e94cb10..2ed0ffb 100644 --- a/examples/resources/lib/matrix.lua +++ b/examples/resources/lib/matrix.lua @@ -3,31 +3,15 @@ if table.unpack == nil then table.unpack = unpack end -local function copyMatrix( orig ) - local copy = RL.MatrixIdentity() - - if orig ~= nil then - for y = 1, 4 do - for x = 1, 4 do - if orig[x][y] ~= nil then - copy[x][y] = orig[x][y] - end - end - end - end - - return copy -end - local Matrix = {} local metatable = { __index = Matrix, __tostring = function( m ) return "{\n" - .." {"..m.m[1][1]..", "..m.m[1][2]..", "..m.m[1][3]..", "..m.m[1][4].."},\n" - .." {"..m.m[2][1]..", "..m.m[2][2]..", "..m.m[2][3]..", "..m.m[2][4].."},\n" - .." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n" - .." {"..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].."},\n" + .." {"..m[1][1]..", "..m[1][2]..", "..m[1][3]..", "..m[1][4].."},\n" + .." {"..m[2][1]..", "..m[2][2]..", "..m[2][3]..", "..m[2][4].."},\n" + .." {"..m[3][1]..", "..m[3][2]..", "..m[3][3]..", "..m[3][4].."},\n" + .." {"..m[4][1]..", "..m[4][2]..", "..m[4][3]..", "..m[4][4].."},\n" .."}" end, __add = function( m1, m2 ) @@ -44,22 +28,40 @@ local metatable = { end, } +function Matrix:copyMatrix( orig ) + if orig ~= nil then + for y = 1, 4 do + for x = 1, 4 do + if orig[x][y] ~= nil then + self[x][y] = orig[x][y] + end + end + end + end +end + function Matrix:new( m ) local object = setmetatable( {}, metatable ) - object.m = copyMatrix( m ) + -- Raylib style transposed matrix. + for x = 1, 4 do + object[x] = {} + for y = 1, 4 do + table.insert( object[x], m[x][y] ) + end + end return object end function Matrix:set( m ) - self.m = copyMatrix( m ) + self:copyMatrix( m ) end function Matrix:serialize() local str = { "Matrix:new({" } - for i, row in ipairs( self.m ) do + for i, row in ipairs( self ) do table.insert( str, "{" ) for c, v in ipairs( row ) do @@ -79,7 +81,7 @@ function Matrix:serialize() end function Matrix:clone() - return Matrix:new( self.m ) + return Matrix:new( self ) end function Matrix:determinant() @@ -154,4 +156,28 @@ function Matrix:lookAt( eye, target, up ) return Matrix:new( RL.MatrixLookAt( eye, target, up ) ) end +-- Temp pre generated objects to avoid "slow" table generation. + +local TEMP_COUNT = 100 +local tempPool = {} +local curTemp = 1 + +for _ = 1, TEMP_COUNT do + table.insert( tempPool, Matrix:new( RL.MatrixIdentity() ) ) +end + +function Matrix:temp( m ) + local object = tempPool[ curTemp ] + + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 + + object:copyMatrix( m ) + + return object +end + +function Matrix:getTempId() + return curTemp +end + return Matrix diff --git a/examples/resources/lib/quaternion.lua b/examples/resources/lib/quaternion.lua index 708fb34..e04c352 100644 --- a/examples/resources/lib/quaternion.lua +++ b/examples/resources/lib/quaternion.lua @@ -151,7 +151,7 @@ function Quaternion:fromMatrix( mat ) end function Quaternion:toMatrix() - return Matrix:newT( RL.QuaternionToMatrix( self ) ) + return Matrix:new( RL.QuaternionToMatrix( self ) ) end function Quaternion:fromAxisAngle( axis, angle ) diff --git a/examples/resources/lib/rectangle.lua b/examples/resources/lib/rectangle.lua index 3de80e4..6e08f52 100644 --- a/examples/resources/lib/rectangle.lua +++ b/examples/resources/lib/rectangle.lua @@ -2,7 +2,6 @@ if table.unpack == nil then table.unpack = unpack end - local Vector2 = Vector2 or require( "vector2" ) local Rectangle = {} @@ -73,6 +72,17 @@ function Rectangle:newR( r ) return object end +function Rectangle:newV( position, size ) + local object = setmetatable( {}, metatable ) + + object.x = position.x + object.y = position.y + object.width = size.x + object.height = size.y + + return object +end + function Rectangle:set( x, y, width, height ) self.x = x or 0 self.y = y or self.x @@ -91,6 +101,13 @@ function Rectangle:setR( r ) self.height = r.height end +function Rectangle:setV( position, size ) + self.x = position.x + self.y = position.y + self.width = size.x + self.height = size.y +end + function Rectangle:serialize() return "Rectangle:new("..self.x..","..self.y..","..self.width..","..self.height..")" end @@ -221,6 +238,20 @@ function Rectangle:tempR( r ) return object end +function Rectangle:tempV( position, size ) + local object = tempPool[ curTemp ] + + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 + + object.x = position.x + object.y = position.y + object.width = size.x + object.height = size.y + + return object +end + + function Rectangle:getTempId() return curTemp end -- cgit v1.2.3