Bitwise operations for cross Lua compatibility.

This commit is contained in:
jussi
2024-05-01 15:21:28 +03:00
parent bdd660be01
commit 4452bccfa6
14 changed files with 261 additions and 112 deletions

View File

@@ -41,8 +41,11 @@ DETAILED CHANGES:
- ADDED: Raygui lib tree view. - ADDED: Raygui lib tree view.
- ADDED: Raygui lib examples file browser. - ADDED: Raygui lib examples file browser.
- CHANGE: Position argument added for GetCodepoint, GetCodepointNext and GetCodepointPrevious. - CHANGE: Position argument added for GetCodepoint, GetCodepointNext and GetCodepointPrevious.
- ADDED glEnable, glDisable, glGetString and glClear. - ADDED: glEnable, glDisable, glGetString and glClear.
- ADDED Stencil reflection example. - 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 Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0

View File

@@ -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

View File

@@ -1,6 +1,5 @@
Util = require( "utillib" ) local Vec2 = require( "vector2" )
Vec2 = require( "vector2" ) local Vec3 = require( "vector3" )
Vec3 = require( "vector3" )
Camera3D = {} Camera3D = {}
Camera3D.meta = { Camera3D.meta = {
@@ -17,6 +16,8 @@ Camera3D.KEYS = {
BACKWARD = RL.KEY_S, BACKWARD = RL.KEY_S,
RIGHT = RL.KEY_D, RIGHT = RL.KEY_D,
LEFT = RL.KEY_A, LEFT = RL.KEY_A,
UP = RL.KEY_R,
DOWN = RL.KEY_F,
PAN = RL.KEY_LEFT_SHIFT, PAN = RL.KEY_LEFT_SHIFT,
FAST = RL.KEY_LEFT_SHIFT, FAST = RL.KEY_LEFT_SHIFT,
} }
@@ -91,6 +92,8 @@ function Camera3D:getUpward()
end end
function Camera3D:update( delta ) function Camera3D:update( delta )
delta = 1 / 60 -- Hack for framerate independance.
if self.mode == self.MODES.FREE then if self.mode == self.MODES.FREE then
if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_MIDDLE ) then if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_MIDDLE ) then
local mouseDelta = Vec2:newT( RL.GetMouseDelta() ) local mouseDelta = Vec2:newT( RL.GetMouseDelta() )
@@ -100,8 +103,8 @@ function Camera3D:update( delta )
local forward = RL.GetCamera3DForward( self.camera )[2] local forward = RL.GetCamera3DForward( self.camera )[2]
RL.Camera3DMoveRight( self.camera, -mouseDelta.x, true ) RL.Camera3DMoveRight( self.camera, -mouseDelta.x, true )
RL.Camera3DMoveUp( self.camera, mouseDelta.y * -( ( 1 - math.abs( forward ) ) * Util.sign( forward ) ) ) RL.Camera3DMoveUp( self.camera, mouseDelta.y * ( 1 - math.abs( forward ) ) )
RL.Camera3DMoveForward( self.camera, mouseDelta.y * math.abs( forward ), true ) RL.Camera3DMoveForward( self.camera, mouseDelta.y * -forward, true )
else else
mouseDelta = mouseDelta:scale( self.TURN_SPEED * delta ) mouseDelta = mouseDelta:scale( self.TURN_SPEED * delta )
@@ -125,6 +128,7 @@ function Camera3D:update( delta )
RL.SetMousePosition( Vec2:newT( RL.GetScreenSize() ):scale( 0.5 ) ) RL.SetMousePosition( Vec2:newT( RL.GetScreenSize() ):scale( 0.5 ) )
local distance = self.KEYBOARD_MOVE_SPEED * delta local distance = self.KEYBOARD_MOVE_SPEED * delta
local forward = RL.GetCamera3DForward( self.camera )[2]
if RL.IsKeyDown( self.KEYS.FAST ) then if RL.IsKeyDown( self.KEYS.FAST ) then
distance = distance * self.KEYBOARD_MOVE_SPEED_MULTI distance = distance * self.KEYBOARD_MOVE_SPEED_MULTI
@@ -141,6 +145,14 @@ function Camera3D:update( delta )
elseif RL.IsKeyDown( self.KEYS.LEFT ) then elseif RL.IsKeyDown( self.KEYS.LEFT ) then
RL.Camera3DMoveRight( self.camera, -distance, true ) RL.Camera3DMoveRight( self.camera, -distance, true )
end 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 elseif self.mode == self.MODES.ORBITAL then
RL.Camera3DYaw( self.camera, self.ORBITAL_SPEED * delta, true ) RL.Camera3DYaw( self.camera, self.ORBITAL_SPEED * delta, true )
end end

View File

@@ -175,11 +175,8 @@ end
function Color:temp( r, g, b, a ) function Color:temp( r, g, b, a )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.r = r or 255 object.r = r or 255
object.g = g or 255 object.g = g or 255
@@ -191,11 +188,8 @@ end
function Color:tempT( t ) function Color:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.r, object.g, object.b, object.a = table.unpack( t ) object.r, object.g, object.b, object.a = table.unpack( t )
@@ -204,11 +198,8 @@ end
function Color:tempC( c ) function Color:tempC( c )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.r = c.r object.r = c.r
object.g = c.g object.g = c.g

View File

@@ -183,11 +183,8 @@ end
function Quaternion:temp( x, y, z, w ) function Quaternion:temp( x, y, z, w )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or 0
@@ -199,11 +196,8 @@ end
function Quaternion:tempT( t ) function Quaternion:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x, object.y, object.z, object.w = table.unpack( t ) object.x, object.y, object.z, object.w = table.unpack( t )
@@ -212,11 +206,8 @@ end
function Quaternion:tempQ( q ) function Quaternion:tempQ( q )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = q.x object.x = q.x
object.y = q.y object.y = q.y

View File

@@ -47,9 +47,9 @@ function Rectangle:new( x, y, width, height )
local object = setmetatable( {}, Rectangle.meta ) local object = setmetatable( {}, Rectangle.meta )
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or object.x
object.width = width or 0 object.width = width or 0
object.height = height or 0 object.height = height or object.width
return object return object
end end
@@ -75,9 +75,9 @@ end
function Rectangle:set( x, y, width, height ) function Rectangle:set( x, y, width, height )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or self.x
self.width = width or 0 self.width = width or 0
self.height = height or 0 self.height = height or self.width
end end
function Rectangle:setT( t ) function Rectangle:setT( t )
@@ -183,27 +183,21 @@ end
function Rectangle:temp( x, y, width, height ) function Rectangle:temp( x, y, width, height )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or object.x
object.width = width or 0 object.width = width or 0
object.height = height or 0 object.height = height or object.width
return object return object
end end
function Rectangle:tempT( t ) function Rectangle:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x, object.y, object.width, object.height = table.unpack( t ) object.x, object.y, object.width, object.height = table.unpack( t )
@@ -212,11 +206,8 @@ end
function Rectangle:tempR( r ) function Rectangle:tempR( r )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = r.x object.x = r.x
object.y = r.y object.y = r.y

View File

@@ -7,11 +7,6 @@ end
local utillib = {} local utillib = {}
-- Does not work with dictionaries.
function utillib.arrayClone( org )
return { table.unpack( org ) }
end
function utillib.deepCopy( orig ) function utillib.deepCopy( orig )
local copy local copy
@@ -156,7 +151,8 @@ function utillib.colorLerp( a, b, f )
return { return {
utillib.round( utillib.lerp( a[1], b[1], f ) ), utillib.round( utillib.lerp( a[1], b[1], f ) ),
utillib.round( utillib.lerp( a[2], b[2], 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 end
-- Move secuence of elements inside table. -- Move secuence of elements inside table.
@@ -176,4 +172,15 @@ function utillib.randomFloat( min, max )
return min + math.random() * ( max - min ); return min + math.random() * ( max - min );
end 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 return utillib

View File

@@ -45,7 +45,7 @@ function Vector2:new( x, y )
local object = setmetatable( {}, metatable ) local object = setmetatable( {}, metatable )
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or object.x
return object return object
end end
@@ -70,7 +70,7 @@ end
function Vector2:set( x, y ) function Vector2:set( x, y )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or self.x
end end
function Vector2:setT( t ) function Vector2:setT( t )
@@ -230,25 +230,19 @@ end
function Vector2:temp( x, y ) function Vector2:temp( x, y )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or object.x
return object return object
end end
function Vector2:tempT( t ) function Vector2:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x, object.y = table.unpack( t ) object.x, object.y = table.unpack( t )
@@ -257,11 +251,8 @@ end
function Vector2:tempV( v ) function Vector2:tempV( v )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = v.x object.x = v.x
object.y = v.y object.y = v.y

View File

@@ -47,8 +47,8 @@ function Vector3:new( x, y, z )
local object = setmetatable( {}, metatable ) local object = setmetatable( {}, metatable )
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or object.x
object.z = z or 0 object.z = z or object.y
return object return object
end end
@@ -73,8 +73,8 @@ end
function Vector3:set( x, y, z ) function Vector3:set( x, y, z )
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or self.x
self.z = z or 0 self.z = z or self.y
end end
function Vector3:setT( t ) function Vector3:setT( t )
@@ -260,26 +260,20 @@ end
function Vector3:temp( x, y, z ) function Vector3:temp( x, y, z )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = x or 0 object.x = x or 0
object.y = y or 0 object.y = y or object.x
object.z = z or 0 object.z = z or object.y
return object return object
end end
function Vector3:tempT( t ) function Vector3:tempT( t )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x, object.y, object.z = table.unpack( t ) object.x, object.y, object.z = table.unpack( t )
@@ -288,11 +282,8 @@ end
function Vector3:tempV( v ) function Vector3:tempV( v )
local object = tempPool[ curTemp ] local object = tempPool[ curTemp ]
curTemp = curTemp + 1
if TEMP_COUNT < curTemp then curTemp = curTemp < TEMP_COUNT and curTemp + 1 or 1
curTemp = 1
end
object.x = v.x object.x = v.x
object.y = v.y object.y = v.y

12
include/bitwiseOp.h Normal file
View File

@@ -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 );

View File

@@ -5,6 +5,7 @@ int imax( int a, int b );
/* Utils. */ /* Utils. */
int lmathRound( lua_State* L ); int lmathRound( lua_State* L );
int lmathSign( lua_State* L );
int lmathClamp( lua_State* L ); int lmathClamp( lua_State* L );
int lmathLerp( lua_State* L ); int lmathLerp( lua_State* L );
int lmathNormalize( lua_State* L ); int lmathNormalize( lua_State* L );

153
src/bitwiseOp.c Normal file
View File

@@ -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;
}

View File

@@ -13,6 +13,7 @@
#include "lrlgl.h" #include "lrlgl.h"
#include "lgl.h" #include "lgl.h"
#include "reasings.h" #include "reasings.h"
#include "bitwiseOp.h"
#ifdef PLATFORM_DESKTOP #ifdef PLATFORM_DESKTOP
#include "platforms/core_desktop.c" #include "platforms/core_desktop.c"
@@ -1933,6 +1934,7 @@ void luaRegister() {
/* Math. */ /* Math. */
/* Utils. */ /* Utils. */
assingGlobalFunction( "Round", lmathRound ); assingGlobalFunction( "Round", lmathRound );
assingGlobalFunction( "Sign", lmathSign );
assingGlobalFunction( "Clamp", lmathClamp ); assingGlobalFunction( "Clamp", lmathClamp );
assingGlobalFunction( "Lerp", lmathLerp ); assingGlobalFunction( "Lerp", lmathLerp );
assingGlobalFunction( "Normalize", lmathNormalize ); assingGlobalFunction( "Normalize", lmathNormalize );
@@ -2350,6 +2352,18 @@ void luaRegister() {
assingGlobalFunction( "EaseElasticOut", leasingsEaseElasticOut ); assingGlobalFunction( "EaseElasticOut", leasingsEaseElasticOut );
assingGlobalFunction( "EaseElasticInOut", leasingsEaseElasticInOut ); 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 ); lua_pop( L, -1 );
} }

View File

@@ -30,6 +30,21 @@ int lmathRound( lua_State* L ) {
return 1; 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 ) > result = RL.Clamp( float value, float min, float max )