From cf2c2eb05bd5d30169771b0087df84a53124f766 Mon Sep 17 00:00:00 2001 From: jussi Date: Wed, 20 Nov 2024 17:10:42 +0200 Subject: Type class updates. --- API.md | 76 +++++++++++++++++++++++++++++++++++ ReiLua_API.lua | 65 ++++++++++++++++++++++++++++++ devnotes | 4 +- docgen.lua | 1 + examples/resources/lib/color.lua | 23 ++++++++--- examples/resources/lib/matrix.lua | 74 +++++++++++++++++++++++----------- examples/resources/lib/quaternion.lua | 2 +- examples/resources/lib/rectangle.lua | 33 ++++++++++++++- 8 files changed, 245 insertions(+), 33 deletions(-) diff --git a/API.md b/API.md index 5ac494d..c48f8d5 100644 --- a/API.md +++ b/API.md @@ -11273,6 +11273,82 @@ Ease elastic in out --- +## Bitwise Operations - Arithmetic + +--- + +> result = RL.BitAnd( int a, int b ) + +Equivalent to a & b in C + +- Success return int + +--- + +> result = RL.BitOr( int a, int b ) + +Equivalent to a | b in C + +- Success return int + +--- + +> result = RL.BitXor( int a, int b ) + +Equivalent to a ^ b in C + +- Success return int + +--- + +> result = RL.BitNot( int v ) + +Equivalent to ~v in C + +- Success return int + +--- + +> result = RL.BitShiftLeft( int v, int n ) + +Equivalent to v << n in C + +- Success return int + +--- + +> result = RL.BitShiftRight( int v, int n ) + +Equivalent to v >> n in C + +- Success return int + +--- + +> result = RL.BitSet( int v, int i, bool b ) + +Set bit in index i to state b in value v + +- Success return int + +--- + +> bit = RL.BitGet( int v, int i ) + +Get bit in index i from value v + +- Success return bool + +--- + +> result = RL.BitToggle( int v, int i ) + +Toggle bit in index i in value v + +- Success return int + +--- + ## GLFW Core - Input-related functions: keyboard --- diff --git a/ReiLua_API.lua b/ReiLua_API.lua index 3df128b..114756b 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -8049,6 +8049,71 @@ function RL.EaseElasticOut( t, b, c, d ) end ---@return any value function RL.EaseElasticInOut( t, b, c, d ) end +-- Bitwise Operations - Arithmetic + +---Equivalent to a & b in C +---- Success return int +---@param a integer +---@param b integer +---@return any result +function RL.BitAnd( a, b ) end + +---Equivalent to a | b in C +---- Success return int +---@param a integer +---@param b integer +---@return any result +function RL.BitOr( a, b ) end + +---Equivalent to a ^ b in C +---- Success return int +---@param a integer +---@param b integer +---@return any result +function RL.BitXor( a, b ) end + +---Equivalent to ~v in C +---- Success return int +---@param v integer +---@return any result +function RL.BitNot( v ) end + +---Equivalent to v << n in C +---- Success return int +---@param v integer +---@param n integer +---@return any result +function RL.BitShiftLeft( v, n ) end + +---Equivalent to v >> n in C +---- Success return int +---@param v integer +---@param n integer +---@return any result +function RL.BitShiftRight( v, n ) end + +---Set bit in index i to state b in value v +---- Success return int +---@param v integer +---@param i integer +---@param b boolean +---@return any result +function RL.BitSet( v, i, b ) end + +---Get bit in index i from value v +---- Success return bool +---@param v integer +---@param i integer +---@return any bit +function RL.BitGet( v, i ) end + +---Toggle bit in index i in value v +---- Success return int +---@param v integer +---@param i integer +---@return any result +function RL.BitToggle( v, i ) end + -- GLFW Core - Input-related functions: keyboard ---This function returns the name of the specified printable key, encoded as UTF-8. diff --git a/devnotes b/devnotes index 3c0ee4e..555404e 100644 --- a/devnotes +++ b/devnotes @@ -2,6 +2,8 @@ Current { } Backlog { + * Symbols + * Expose symbols on Windows. * Raygui * ICON_ defines. * Raygui lib @@ -15,7 +17,7 @@ Backlog { * Models * Mesh bone weight management? * CBuffer - * Check endianess. + * Swap endianess. * SDL2 platform specific functions. * Textures diff --git a/docgen.lua b/docgen.lua index 48d9f37..b68d2e4 100644 --- a/docgen.lua +++ b/docgen.lua @@ -371,6 +371,7 @@ sourceFiles = { "rlgl", "gl", "easings", + "bitwiseOp", "platforms/core_desktop", -- "platforms/core_desktop_sdl", } 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