diff options
| author | jussi | 2024-05-01 15:21:28 +0300 |
|---|---|---|
| committer | jussi | 2024-05-01 15:21:28 +0300 |
| commit | 4452bccfa63cf86c134aa616ee0bebcc66beca03 (patch) | |
| tree | a40befeb0393fd5599240ccb85a4500781d78c17 /src | |
| parent | bdd660be01f3742befe15dff26929a77eeefe61d (diff) | |
| download | reilua-enhanced-4452bccfa63cf86c134aa616ee0bebcc66beca03.tar.gz reilua-enhanced-4452bccfa63cf86c134aa616ee0bebcc66beca03.tar.bz2 reilua-enhanced-4452bccfa63cf86c134aa616ee0bebcc66beca03.zip | |
Bitwise operations for cross Lua compatibility.
Diffstat (limited to 'src')
| -rw-r--r-- | src/bitwiseOp.c | 153 | ||||
| -rw-r--r-- | src/lua_core.c | 14 | ||||
| -rw-r--r-- | src/rmath.c | 15 |
3 files changed, 182 insertions, 0 deletions
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 @@ -31,6 +31,21 @@ int lmathRound( lua_State* L ) { } /* +> 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 ) Clamp float value |
