From 4452bccfa63cf86c134aa616ee0bebcc66beca03 Mon Sep 17 00:00:00 2001 From: jussi Date: Wed, 1 May 2024 15:21:28 +0300 Subject: Bitwise operations for cross Lua compatibility. --- changelog | 7 +- examples/resources/lib/bitlib.lua | 23 ----- examples/resources/lib/camera3d.lua | 24 ++++-- examples/resources/lib/color.lua | 15 +--- examples/resources/lib/quaternion.lua | 15 +--- examples/resources/lib/rectangle.lua | 27 ++---- examples/resources/lib/utillib.lua | 19 +++-- examples/resources/lib/vector2.lua | 21 ++--- examples/resources/lib/vector3.lua | 27 ++---- include/bitwiseOp.h | 12 +++ include/rmath.h | 1 + src/bitwiseOp.c | 153 ++++++++++++++++++++++++++++++++++ src/lua_core.c | 14 ++++ src/rmath.c | 15 ++++ 14 files changed, 261 insertions(+), 112 deletions(-) delete mode 100644 examples/resources/lib/bitlib.lua create mode 100644 include/bitwiseOp.h create mode 100644 src/bitwiseOp.c diff --git a/changelog b/changelog index c298294..a7f3f14 100644 --- a/changelog +++ b/changelog @@ -41,8 +41,11 @@ DETAILED CHANGES: - ADDED: Raygui lib tree view. - ADDED: Raygui lib examples file browser. - CHANGE: Position argument added for GetCodepoint, GetCodepointNext and GetCodepointPrevious. - - ADDED glEnable, glDisable, glGetString and glClear. - - ADDED Stencil reflection example. + - ADDED: glEnable, glDisable, glGetString and glClear. + - ADDED: Stencil reflection example. + - ADDED: Math Sign. + - FIXED: Camera3D lib Fix up down movement invertion when looking up + - ADDED: Bitwise operations for cross Lua compatibility. ------------------------------------------------------------------------ Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0 diff --git a/examples/resources/lib/bitlib.lua b/examples/resources/lib/bitlib.lua deleted file mode 100644 index 246ac29..0000000 --- a/examples/resources/lib/bitlib.lua +++ /dev/null @@ -1,23 +0,0 @@ -local bitlib = {} - -function bitlib.setBit( v, i, b ) - if b then - return v | 1 << i - else - return v & ~( 1 << i ) - end -end - -function bitlib.toggleBit( v, i ) - return v ~ ( 1 << i ) -end - -function bitlib.getBit( v, i ) - if v == nil then - return false - end - - return 0 < v & ( 1 << i ) -end - -return bitlib diff --git a/examples/resources/lib/camera3d.lua b/examples/resources/lib/camera3d.lua index 2c84b31..8c15fa7 100644 --- a/examples/resources/lib/camera3d.lua +++ b/examples/resources/lib/camera3d.lua @@ -1,6 +1,5 @@ -Util = require( "utillib" ) -Vec2 = require( "vector2" ) -Vec3 = require( "vector3" ) +local Vec2 = require( "vector2" ) +local Vec3 = require( "vector3" ) Camera3D = {} Camera3D.meta = { @@ -17,6 +16,8 @@ Camera3D.KEYS = { BACKWARD = RL.KEY_S, RIGHT = RL.KEY_D, LEFT = RL.KEY_A, + UP = RL.KEY_R, + DOWN = RL.KEY_F, PAN = RL.KEY_LEFT_SHIFT, FAST = RL.KEY_LEFT_SHIFT, } @@ -91,17 +92,19 @@ function Camera3D:getUpward() end function Camera3D:update( delta ) + delta = 1 / 60 -- Hack for framerate independance. + if self.mode == self.MODES.FREE then if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_MIDDLE ) then local mouseDelta = Vec2:newT( RL.GetMouseDelta() ) - + if RL.IsKeyDown( self.KEYS.PAN ) then mouseDelta = mouseDelta:scale( self.MOUSE_MOVE_SPEED * delta ) local forward = RL.GetCamera3DForward( self.camera )[2] RL.Camera3DMoveRight( self.camera, -mouseDelta.x, true ) - RL.Camera3DMoveUp( self.camera, mouseDelta.y * -( ( 1 - math.abs( forward ) ) * Util.sign( forward ) ) ) - RL.Camera3DMoveForward( self.camera, mouseDelta.y * math.abs( forward ), true ) + RL.Camera3DMoveUp( self.camera, mouseDelta.y * ( 1 - math.abs( forward ) ) ) + RL.Camera3DMoveForward( self.camera, mouseDelta.y * -forward, true ) else mouseDelta = mouseDelta:scale( self.TURN_SPEED * delta ) @@ -125,6 +128,7 @@ function Camera3D:update( delta ) RL.SetMousePosition( Vec2:newT( RL.GetScreenSize() ):scale( 0.5 ) ) local distance = self.KEYBOARD_MOVE_SPEED * delta + local forward = RL.GetCamera3DForward( self.camera )[2] if RL.IsKeyDown( self.KEYS.FAST ) then distance = distance * self.KEYBOARD_MOVE_SPEED_MULTI @@ -141,6 +145,14 @@ function Camera3D:update( delta ) elseif RL.IsKeyDown( self.KEYS.LEFT ) then RL.Camera3DMoveRight( self.camera, -distance, true ) end + + if RL.IsKeyDown( self.KEYS.UP ) then + RL.Camera3DMoveUp( self.camera, distance * ( 1 - math.abs( forward ) ) ) + RL.Camera3DMoveForward( self.camera, distance * -forward, true ) + elseif RL.IsKeyDown( self.KEYS.DOWN ) then + RL.Camera3DMoveUp( self.camera, -distance * ( 1 - math.abs( forward ) ) ) + RL.Camera3DMoveForward( self.camera, -distance * -forward, true ) + end elseif self.mode == self.MODES.ORBITAL then RL.Camera3DYaw( self.camera, self.ORBITAL_SPEED * delta, true ) end diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua index 1a222e8..7c98e92 100644 --- a/examples/resources/lib/color.lua +++ b/examples/resources/lib/color.lua @@ -175,11 +175,8 @@ end function Color:temp( r, g, b, a ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.r = r or 255 object.g = g or 255 @@ -191,11 +188,8 @@ end function Color:tempT( t ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.r, object.g, object.b, object.a = table.unpack( t ) @@ -204,11 +198,8 @@ end function Color:tempC( c ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.r = c.r object.g = c.g diff --git a/examples/resources/lib/quaternion.lua b/examples/resources/lib/quaternion.lua index 47d6ee1..db700d8 100644 --- a/examples/resources/lib/quaternion.lua +++ b/examples/resources/lib/quaternion.lua @@ -183,11 +183,8 @@ end function Quaternion:temp( x, y, z, w ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = x or 0 object.y = y or 0 @@ -199,11 +196,8 @@ end function Quaternion:tempT( t ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x, object.y, object.z, object.w = table.unpack( t ) @@ -212,11 +206,8 @@ end function Quaternion:tempQ( q ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = q.x object.y = q.y diff --git a/examples/resources/lib/rectangle.lua b/examples/resources/lib/rectangle.lua index a167939..7413f53 100644 --- a/examples/resources/lib/rectangle.lua +++ b/examples/resources/lib/rectangle.lua @@ -47,9 +47,9 @@ function Rectangle:new( x, y, width, height ) local object = setmetatable( {}, Rectangle.meta ) object.x = x or 0 - object.y = y or 0 + object.y = y or object.x object.width = width or 0 - object.height = height or 0 + object.height = height or object.width return object end @@ -75,9 +75,9 @@ end function Rectangle:set( x, y, width, height ) self.x = x or 0 - self.y = y or 0 + self.y = y or self.x self.width = width or 0 - self.height = height or 0 + self.height = height or self.width end function Rectangle:setT( t ) @@ -183,27 +183,21 @@ end function Rectangle:temp( x, y, width, height ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = x or 0 - object.y = y or 0 + object.y = y or object.x object.width = width or 0 - object.height = height or 0 + object.height = height or object.width return object end function Rectangle:tempT( t ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x, object.y, object.width, object.height = table.unpack( t ) @@ -212,11 +206,8 @@ end function Rectangle:tempR( r ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = r.x object.y = r.y diff --git a/examples/resources/lib/utillib.lua b/examples/resources/lib/utillib.lua index a661f60..4054006 100644 --- a/examples/resources/lib/utillib.lua +++ b/examples/resources/lib/utillib.lua @@ -7,11 +7,6 @@ end local utillib = {} --- Does not work with dictionaries. -function utillib.arrayClone( org ) - return { table.unpack( org ) } -end - function utillib.deepCopy( orig ) local copy @@ -156,7 +151,8 @@ function utillib.colorLerp( a, b, f ) return { utillib.round( utillib.lerp( a[1], b[1], f ) ), utillib.round( utillib.lerp( a[2], b[2], f ) ), - utillib.round( utillib.lerp( a[3], b[3], f ) ) } + utillib.round( utillib.lerp( a[3], b[3], f ) ) + } end -- Move secuence of elements inside table. @@ -176,4 +172,15 @@ function utillib.randomFloat( min, max ) return min + math.random() * ( max - min ); end +function utillib.printBin( v ) + for i = 31, 0, -1 do + if RL.BitGet( v, i ) then + io.write( "1" ) + else + io.write( "0" ) + end + end + print() +end + return utillib diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua index c9c8433..68adc2b 100644 --- a/examples/resources/lib/vector2.lua +++ b/examples/resources/lib/vector2.lua @@ -45,7 +45,7 @@ function Vector2:new( x, y ) local object = setmetatable( {}, metatable ) object.x = x or 0 - object.y = y or 0 + object.y = y or object.x return object end @@ -70,7 +70,7 @@ end function Vector2:set( x, y ) self.x = x or 0 - self.y = y or 0 + self.y = y or self.x end function Vector2:setT( t ) @@ -230,25 +230,19 @@ end function Vector2:temp( x, y ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = x or 0 - object.y = y or 0 + object.y = y or object.x return object end function Vector2:tempT( t ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x, object.y = table.unpack( t ) @@ -257,11 +251,8 @@ end function Vector2:tempV( v ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = v.x object.y = v.y diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua index 13f621c..86f5270 100644 --- a/examples/resources/lib/vector3.lua +++ b/examples/resources/lib/vector3.lua @@ -47,8 +47,8 @@ function Vector3:new( x, y, z ) local object = setmetatable( {}, metatable ) object.x = x or 0 - object.y = y or 0 - object.z = z or 0 + object.y = y or object.x + object.z = z or object.y return object end @@ -73,8 +73,8 @@ end function Vector3:set( x, y, z ) self.x = x or 0 - self.y = y or 0 - self.z = z or 0 + self.y = y or self.x + self.z = z or self.y end function Vector3:setT( t ) @@ -260,26 +260,20 @@ end function Vector3:temp( x, y, z ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = x or 0 - object.y = y or 0 - object.z = z or 0 + object.y = y or object.x + object.z = z or object.y return object end function Vector3:tempT( t ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x, object.y, object.z = table.unpack( t ) @@ -288,11 +282,8 @@ end function Vector3:tempV( v ) local object = tempPool[ curTemp ] - curTemp = curTemp + 1 - if TEMP_COUNT < curTemp then - curTemp = 1 - end + curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1 object.x = v.x object.y = v.y diff --git a/include/bitwiseOp.h b/include/bitwiseOp.h new file mode 100644 index 0000000..e2d3c42 --- /dev/null +++ b/include/bitwiseOp.h @@ -0,0 +1,12 @@ +#pragma once + +/* Arithmetic. */ +int lbitAnd( lua_State* L ); +int lbitOr( lua_State* L ); +int lbitXor( lua_State* L ); +int lbitNot( lua_State* L ); +int lbitShiftLeft( lua_State* L ); +int lbitShiftRight( lua_State* L ); +int lbitSet( lua_State* L ); +int lbitGet( lua_State* L ); +int lbitToggle( lua_State* L ); diff --git a/include/rmath.h b/include/rmath.h index 249bcce..07132aa 100644 --- a/include/rmath.h +++ b/include/rmath.h @@ -5,6 +5,7 @@ int imax( int a, int b ); /* Utils. */ int lmathRound( lua_State* L ); +int lmathSign( lua_State* L ); int lmathClamp( lua_State* L ); int lmathLerp( lua_State* L ); int lmathNormalize( lua_State* L ); diff --git a/src/bitwiseOp.c b/src/bitwiseOp.c new file mode 100644 index 0000000..1e1aeb1 --- /dev/null +++ b/src/bitwiseOp.c @@ -0,0 +1,153 @@ +#include "main.h" +#include "state.h" +#include "lua_core.h" +#include "core.h" +#include "bitwiseOp.h" + +/* +## Bitwise Operations - Arithmetic +*/ + +/* +> result = RL.BitAnd( int a, int b ) + +Equivalent to a & b in C + +- Success return int +*/ +int lbitAnd( lua_State* L ) { + int a = luaL_checkinteger( L, 1 ); + int b = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, a & b ); + + return 1; +} + +/* +> result = RL.BitOr( int a, int b ) + +Equivalent to a | b in C + +- Success return int +*/ +int lbitOr( lua_State* L ) { + int a = luaL_checkinteger( L, 1 ); + int b = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, a | b ); + + return 1; +} + +/* +> result = RL.BitXor( int a, int b ) + +Equivalent to a ^ b in C + +- Success return int +*/ +int lbitXor( lua_State* L ) { + int a = luaL_checkinteger( L, 1 ); + int b = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, a ^ b ); + + return 1; +} + +/* +> result = RL.BitNot( int v ) + +Equivalent to ~v in C + +- Success return int +*/ +int lbitNot( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + + lua_pushinteger( L, ~v ); + + return 1; +} + +/* +> result = RL.BitShiftLeft( int v, int n ) + +Equivalent to v << n in C + +- Success return int +*/ +int lbitShiftLeft( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int n = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, v << n ); + + return 1; +} + +/* +> result = RL.BitShiftRight( int v, int n ) + +Equivalent to v >> n in C + +- Success return int +*/ +int lbitShiftRight( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int n = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, v >> n ); + + return 1; +} + +/* +> result = RL.BitSet( int v, int i, bool b ) + +Set bit in index i to state b in value v + +- Success return int +*/ +int lbitSet( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int i = luaL_checkinteger( L, 2 ); + bool b = uluaGetBoolean( L, 3 ); + + lua_pushinteger( L, b ? v | 1 << i : v & ~( 1 << i ) ); + + return 1; +} + +/* +> bit = RL.BitGet( int v, int i ) + +Get bit in index i from value v + +- Success return bool +*/ +int lbitGet( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int i = luaL_checkinteger( L, 2 ); + + lua_pushboolean( L, 0 < ( v & ( 1 << i ) ) ); + + return 1; +} + +/* +> result = RL.BitToggle( int v, int i ) + +Toggle bit in index i in value v + +- Success return int +*/ +int lbitToggle( lua_State* L ) { + int v = luaL_checkinteger( L, 1 ); + int i = luaL_checkinteger( L, 2 ); + + lua_pushinteger( L, v ^ 1 << i ); + + return 1; +} diff --git a/src/lua_core.c b/src/lua_core.c index cdaabe2..991fb4d 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -13,6 +13,7 @@ #include "lrlgl.h" #include "lgl.h" #include "reasings.h" +#include "bitwiseOp.h" #ifdef PLATFORM_DESKTOP #include "platforms/core_desktop.c" @@ -1933,6 +1934,7 @@ void luaRegister() { /* Math. */ /* Utils. */ assingGlobalFunction( "Round", lmathRound ); + assingGlobalFunction( "Sign", lmathSign ); assingGlobalFunction( "Clamp", lmathClamp ); assingGlobalFunction( "Lerp", lmathLerp ); assingGlobalFunction( "Normalize", lmathNormalize ); @@ -2350,6 +2352,18 @@ void luaRegister() { assingGlobalFunction( "EaseElasticOut", leasingsEaseElasticOut ); assingGlobalFunction( "EaseElasticInOut", leasingsEaseElasticInOut ); + /* Bitwise Operations */ + /* Arithmetic. */ + assingGlobalFunction( "BitAnd", lbitAnd ); + assingGlobalFunction( "BitOr", lbitOr ); + assingGlobalFunction( "BitXor", lbitXor ); + assingGlobalFunction( "BitNot", lbitNot ); + assingGlobalFunction( "BitShiftLeft", lbitShiftLeft ); + assingGlobalFunction( "BitShiftRight", lbitShiftRight ); + assingGlobalFunction( "BitSet", lbitSet ); + assingGlobalFunction( "BitGet", lbitGet ); + assingGlobalFunction( "BitToggle", lbitToggle ); + lua_pop( L, -1 ); } diff --git a/src/rmath.c b/src/rmath.c index 7617d45..7287d66 100644 --- a/src/rmath.c +++ b/src/rmath.c @@ -30,6 +30,21 @@ int lmathRound( lua_State* L ) { return 1; } +/* +> result = RL.Sign( float value ) + +Sign of value + +- Success return int +*/ +int lmathSign( lua_State* L ) { + float value = luaL_checknumber( L, 1 ); + + lua_pushinteger( L, 0 <= value ? 1 : -1 ); + + return 1; +} + /* > result = RL.Clamp( float value, float min, float max ) -- cgit v1.2.3