From 4c0eb17a9c234bfee73af408faa38e38f2e450d9 Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 21 Nov 2024 00:11:31 +0200 Subject: New raylib 5.5 functions. --- src/core.c | 184 +++++++++-- src/lua_core.c | 44 ++- src/models.c | 64 ++++ src/platforms/core_desktop.c | 656 -------------------------------------- src/platforms/core_desktop_glfw.c | 656 ++++++++++++++++++++++++++++++++++++++ src/shapes.c | 57 +++- src/state.c | 1 + src/text.c | 30 ++ src/textures.c | 199 +++++++++++- 9 files changed, 1194 insertions(+), 697 deletions(-) delete mode 100644 src/platforms/core_desktop.c create mode 100644 src/platforms/core_desktop_glfw.c (limited to 'src') diff --git a/src/core.c b/src/core.c index 3473251..c3c07c5 100644 --- a/src/core.c +++ b/src/core.c @@ -559,6 +559,19 @@ int lcoreGetClipboardText( lua_State* L ) { return 1; } +/* +> image = RL.GetClipboardImage() + +Get clipboard image content + +- Success return Image +*/ +int lcoreGetClipboardImage( lua_State* L ) { + uluaPushImage( L, GetClipboardImage() ); + + return 1; +} + /* > RL.EnableEventWaiting() @@ -1782,6 +1795,60 @@ int lcoreGetApplicationDirectory( lua_State* L ) { return 1; } +/* +> success = RL.MakeDirectory( string dirPath ) + +Create directories (including full path requested), returns 0 on success + +- Success return int +*/ +int lcoreMakeDirectory( lua_State* L ) { + const char *dirPath = luaL_checkstring( L, 1 ); + + lua_pushinteger( L, MakeDirectory( dirPath ) ); + + return 1; +} + +/* +> success = RL.ChangeDirectory( string directory ) + +Change working directory, return true on success + +- Success return bool +*/ +int lcoreChangeDirectory( lua_State* L ) { + lua_pushboolean( L, ChangeDirectory( luaL_checkstring( L, 1 ) ) ); + + return 1; +} + +/* +> isFile = RL.IsPathFile( string path ) + +Check if a given path is a file or a directory + +- Success return bool +*/ +int lcoreIsPathFile( lua_State* L ) { + lua_pushboolean( L, IsPathFile( luaL_checkstring( L, 1 ) ) ); + + return 1; +} + +/* +> isValid = RL.IsFileNameValid( string fileName ) + +Check if fileName is valid for the platform/OS + +- Success return bool +*/ +int lcoreIsFileNameValid( lua_State* L ) { + lua_pushboolean( L, IsFileNameValid( luaL_checkstring( L, 1 ) ) ); + + return 1; +} + /* > fileNames = RL.LoadDirectoryFiles( string dirPath ) @@ -1831,32 +1898,6 @@ int lcoreLoadDirectoryFilesEx( lua_State* L ) { return 1; } -/* -> success = RL.ChangeDirectory( string directory ) - -Change working directory, return true on success - -- Success return bool -*/ -int lcoreChangeDirectory( lua_State* L ) { - lua_pushboolean( L, ChangeDirectory( luaL_checkstring( L, 1 ) ) ); - - return 1; -} - -/* -> isFile = RL.IsPathFile( string path ) - -Check if a given path is a file or a directory - -- Success return bool -*/ -int lcoreIsPathFile( lua_State* L ) { - lua_pushboolean( L, IsPathFile( luaL_checkstring( L, 1 ) ) ); - - return 1; -} - /* > iSFileDropped = RL.IsFileDropped() @@ -1997,6 +2038,81 @@ int lcoreDecodeDataBase64( lua_State* L ) { return 2; } +/* +> code = RL.ComputeCRC32( Buffer data ) + +Compute CRC32 hash code. Note! Buffer should be type BUFFER_UNSIGNED_CHAR + +- Failure return false +- Success return int +*/ +int lcoreComputeCRC32( lua_State* L ) { + Buffer* buffer = uluaGetBuffer( L, 1 ); + + if ( buffer->type == BUFFER_UNSIGNED_CHAR ) { + lua_pushinteger( L, ComputeCRC32( buffer->data, buffer->size ) ); + } + else { + lua_pushboolean( L, false ); + } + + return 1; +} + +/* +> code = RL.ComputeMD5( Buffer data ) + +Compute MD5 hash code, returns static int[4] (16 bytes). Note! Buffer should be type BUFFER_UNSIGNED_CHAR + +- Failure return false +- Success return int{4} +*/ +int lcoreComputeMD5( lua_State* L ) { + Buffer* buffer = uluaGetBuffer( L, 1 ); + + if ( buffer->type == BUFFER_UNSIGNED_CHAR ) { + unsigned int* code = ComputeMD5( buffer->data, buffer->size ); + lua_createtable( L, 4, 0 ); + + for ( unsigned int i = 0; i < 4; i++ ) { + lua_pushinteger( L, code[i] ); + lua_rawseti( L, -2, i + 1 ); + } + } + else { + lua_pushboolean( L, false ); + } + + return 1; +} + +/* +> code = RL.ComputeSHA1( Buffer data ) + +Compute SHA1 hash code, returns static int[5] (20 bytes). Note! Buffer should be type BUFFER_UNSIGNED_CHAR + +- Failure return false +- Success return int{5} +*/ +int lcoreComputeSHA1( lua_State* L ) { + Buffer* buffer = uluaGetBuffer( L, 1 ); + + if ( buffer->type == BUFFER_UNSIGNED_CHAR ) { + unsigned int* code = ComputeSHA1( buffer->data, buffer->size ); + lua_createtable( L, 5, 0 ); + + for ( unsigned int i = 0; i < 5; i++ ) { + lua_pushinteger( L, code[i] ); + lua_rawseti( L, -2, i + 1 ); + } + } + else { + lua_pushboolean( L, false ); + } + + return 1; +} + /* ## Core - Automation events functionality */ @@ -2490,6 +2606,22 @@ int lcoreSetGamepadMappings( lua_State* L ) { return 1; } +/* +> RL.SetGamepadVibration( int gamepad, float leftMotor, float rightMotor, float duration ) + +Set gamepad vibration for both motors (duration in seconds) +*/ +int lcoreSetGamepadVibration( lua_State* L ) { + int gamepad = luaL_checkinteger( L, 1 ); + float leftMotor = luaL_checknumber( L, 2 ); + float rightMotor = luaL_checknumber( L, 2 ); + float duration = luaL_checknumber( L, 2 ); + + SetGamepadVibration( gamepad, leftMotor, rightMotor, duration ); + + return 0; +} + /* ## Core - Input-related functions: mouse */ diff --git a/src/lua_core.c b/src/lua_core.c index b7a2197..8a6a8f9 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -16,7 +16,7 @@ #include "bitwiseOp.h" #ifdef PLATFORM_DESKTOP - #include "platforms/core_desktop.c" + #include "platforms/core_desktop_glfw.c" #elif PLATFORM_DESKTOP_SDL #include "platforms/core_desktop_sdl.c" #elif PLATFORM_WEB @@ -1310,6 +1310,7 @@ void luaRegister() { assingGlobalFunction( "GetMonitorName", lcoreGetMonitorName ); assingGlobalFunction( "SetClipboardText", lcoreSetClipboardText ); assingGlobalFunction( "GetClipboardText", lcoreGetClipboardText ); + assingGlobalFunction( "GetClipboardImage", lcoreGetClipboardImage ); assingGlobalFunction( "EnableEventWaiting", lcoreEnableEventWaiting ); assingGlobalFunction( "DisableEventWaiting", lcoreDisableEventWaiting ); /* Cursor-related functions. */ @@ -1401,10 +1402,12 @@ void luaRegister() { assingGlobalFunction( "GetPrevDirectoryPath", lcoreGetPrevDirectoryPath ); assingGlobalFunction( "GetWorkingDirectory", lcoreGetWorkingDirectory ); assingGlobalFunction( "GetApplicationDirectory", lcoreGetApplicationDirectory ); - assingGlobalFunction( "LoadDirectoryFiles", lcoreLoadDirectoryFiles ); - assingGlobalFunction( "LoadDirectoryFilesEx", lcoreLoadDirectoryFilesEx ); + assingGlobalFunction( "MakeDirectory", lcoreMakeDirectory ); assingGlobalFunction( "ChangeDirectory", lcoreChangeDirectory ); assingGlobalFunction( "IsPathFile", lcoreIsPathFile ); + assingGlobalFunction( "IsFileNameValid", lcoreIsFileNameValid ); + assingGlobalFunction( "LoadDirectoryFiles", lcoreLoadDirectoryFiles ); + assingGlobalFunction( "LoadDirectoryFilesEx", lcoreLoadDirectoryFilesEx ); assingGlobalFunction( "IsFileDropped", lcoreIsFileDropped ); assingGlobalFunction( "LoadDroppedFiles", lcoreLoadDroppedFiles ); assingGlobalFunction( "GetFileModTime", lcoreGetFileModTime ); @@ -1413,6 +1416,9 @@ void luaRegister() { assingGlobalFunction( "DecompressData", lcoreDecompressData ); assingGlobalFunction( "EncodeDataBase64", lcoreEncodeDataBase64 ); assingGlobalFunction( "DecodeDataBase64", lcoreDecodeDataBase64 ); + assingGlobalFunction( "ComputeCRC32", lcoreComputeCRC32 ); + assingGlobalFunction( "ComputeMD5", lcoreComputeMD5 ); + assingGlobalFunction( "ComputeSHA1", lcoreComputeSHA1 ); /* Automation events functionality. */ assingGlobalFunction( "LoadAutomationEventList", lcoreLoadAutomationEventList ); assingGlobalFunction( "UnloadAutomationEventList", lcoreUnloadAutomationEventList ); @@ -1448,6 +1454,7 @@ void luaRegister() { assingGlobalFunction( "GetGamepadAxisCount", lcoreGetGamepadAxisCount ); assingGlobalFunction( "GetGamepadAxisMovement", lcoreGetGamepadAxisMovement ); assingGlobalFunction( "SetGamepadMappings", lcoreSetGamepadMappings ); + assingGlobalFunction( "SetGamepadVibration", lcoreSetGamepadVibration ); /* Input-related functions: mouse. */ assingGlobalFunction( "IsMouseButtonPressed", lcoreIsMouseButtonPressed ); assingGlobalFunction( "IsMouseButtonDown", lcoreIsMouseButtonDown ); @@ -1531,6 +1538,8 @@ void luaRegister() { /* Shapes. */ /* Basic shapes drawing functions. */ assingGlobalFunction( "SetShapesTexture", lshapesSetShapesTexture ); + assingGlobalFunction( "GetShapesTexture", lshapesGetShapesTexture ); + assingGlobalFunction( "GetShapesTextureRectangle", lshapesGetShapesTextureRectangle ); assingGlobalFunction( "DrawPixel", lshapesDrawPixel ); assingGlobalFunction( "DrawLine", lshapesDrawLine ); assingGlobalFunction( "DrawLineBezier", lshapesDrawLineBezier ); @@ -1582,6 +1591,7 @@ void luaRegister() { assingGlobalFunction( "CheckCollisionRecs", lshapesCheckCollisionRecs ); assingGlobalFunction( "CheckCollisionCircles", lshapesCheckCollisionCircles ); assingGlobalFunction( "CheckCollisionCircleRec", lshapesCheckCollisionCircleRec ); + assingGlobalFunction( "CheckCollisionCircleLine", lshapesCheckCollisionCircleLine ); assingGlobalFunction( "CheckCollisionPointRec", lshapesCheckCollisionPointRec ); assingGlobalFunction( "CheckCollisionPointCircle", lshapesCheckCollisionPointCircle ); assingGlobalFunction( "CheckCollisionPointTriangle", lshapesCheckCollisionPointTriangle ); @@ -1595,6 +1605,7 @@ void luaRegister() { assingGlobalFunction( "LoadImage", ltexturesLoadImage ); assingGlobalFunction( "LoadImageRaw", ltexturesLoadImageRaw ); assingGlobalFunction( "LoadImageAnim", ltexturesLoadImageAnim ); + assingGlobalFunction( "LoadImageAnimFromMemory", ltexturesLoadImageAnimFromMemory ); assingGlobalFunction( "LoadImageFromMemory", ltexturesLoadImageFromMemory ); assingGlobalFunction( "LoadImageFromData", ltexturesLoadImageFromData ); assingGlobalFunction( "LoadImageFromTexture", ltexturesLoadImageFromTexture ); @@ -1617,6 +1628,7 @@ void luaRegister() { /* Image manipulation functions. */ assingGlobalFunction( "ImageCopy", ltexturesImageCopy ); assingGlobalFunction( "ImageFromImage", ltexturesImageFromImage ); + assingGlobalFunction( "ImageFromChannel", ltexturesImageFromChannel ); assingGlobalFunction( "ImageText", ltexturesImageText ); assingGlobalFunction( "ImageTextEx", ltexturesImageTextEx ); assingGlobalFunction( "ImageFormat", ltexturesImageFormat ); @@ -1627,6 +1639,7 @@ void luaRegister() { assingGlobalFunction( "ImageAlphaMask", ltexturesImageAlphaMask ); assingGlobalFunction( "ImageAlphaPremultiply", ltexturesImageAlphaPremultiply ); assingGlobalFunction( "ImageBlurGaussian", ltexturesImageBlurGaussian ); + assingGlobalFunction( "ImageKernelConvolution", ltexturesImageKernelConvolution ); assingGlobalFunction( "ImageResize", ltexturesImageResize ); assingGlobalFunction( "ImageResizeNN", ltexturesImageResizeNN ); assingGlobalFunction( "ImageResizeCanvas", ltexturesImageResizeCanvas ); @@ -1656,10 +1669,16 @@ void luaRegister() { assingGlobalFunction( "ImageClearBackground", ltexturesImageClearBackground ); assingGlobalFunction( "ImageDrawPixel", ltexturesImageDrawPixel ); assingGlobalFunction( "ImageDrawLine", ltexturesImageDrawLine ); + assingGlobalFunction( "ImageDrawLineEx", ltexturesImageDrawLineEx ); assingGlobalFunction( "ImageDrawCircle", ltexturesImageDrawCircle ); assingGlobalFunction( "ImageDrawCircleLines", ltexturesImageDrawCircleLines ); assingGlobalFunction( "ImageDrawRectangle", ltexturesImageDrawRectangle ); assingGlobalFunction( "ImageDrawRectangleLines", ltexturesImageDrawRectangleLines ); + assingGlobalFunction( "ImageDrawTriangle", ltexturesImageDrawTriangle ); + assingGlobalFunction( "ImageDrawTriangleEx", ltexturesImageDrawTriangleEx ); + assingGlobalFunction( "ImageDrawTriangleLines", ltexturesImageDrawTriangleLines ); + assingGlobalFunction( "ImageDrawTriangleFan", ltexturesImageDrawTriangleFan ); + assingGlobalFunction( "ImageDrawTriangleStrip", ltexturesImageDrawTriangleStrip ); assingGlobalFunction( "ImageDraw", ltexturesImageDraw ); assingGlobalFunction( "ImageDrawText", ltexturesImageDrawText ); assingGlobalFunction( "ImageDrawTextEx", ltexturesImageDrawTextEx ); @@ -1697,6 +1716,7 @@ void luaRegister() { assingGlobalFunction( "GetRenderTextureTexture", ltexturesGetRenderTextureTexture ); assingGlobalFunction( "GetRenderTextureDepthTexture", ltexturesGetRenderTextureDepthTexture ); /* Color/pixel related functions */ + assingGlobalFunction( "ColorIsEqual", ltexturesColorIsEqual ); assingGlobalFunction( "Fade", ltexturesFade ); assingGlobalFunction( "ColorToInt", ltexturesColorToInt ); assingGlobalFunction( "ColorNormalize", ltexturesColorNormalize ); @@ -1708,6 +1728,7 @@ void luaRegister() { assingGlobalFunction( "ColorContrast", ltexturesColorContrast ); assingGlobalFunction( "ColorAlpha", ltexturesColorAlpha ); assingGlobalFunction( "ColorAlphaBlend", ltexturesColorAlphaBlend ); + assingGlobalFunction( "ColorLerp", ltexturesColorLerp ); assingGlobalFunction( "GetColor", ltexturesGetColor ); assingGlobalFunction( "GetPixelDataSize", ltexturesGetPixelDataSize ); @@ -1759,6 +1780,8 @@ void luaRegister() { assingGlobalFunction( "DrawModelEx", lmodelsDrawModelEx ); assingGlobalFunction( "DrawModelWires", lmodelsDrawModelWires ); assingGlobalFunction( "DrawModelWiresEx", lmodelsDrawModelWiresEx ); + assingGlobalFunction( "DrawModelPoints", lmodelsDrawModelPoints ); + assingGlobalFunction( "DrawModelPointsEx", lmodelsDrawModelPointsEx ); assingGlobalFunction( "DrawBoundingBox", lmodelsDrawBoundingBox ); assingGlobalFunction( "DrawBillboard", lmodelsDrawBillboard ); assingGlobalFunction( "DrawBillboardRec", lmodelsDrawBillboardRec ); @@ -1770,6 +1793,7 @@ void luaRegister() { assingGlobalFunction( "DrawMeshInstanced", lmodelsDrawMeshInstanced ); assingGlobalFunction( "SetMeshColor", lmodelsSetMeshColor ); assingGlobalFunction( "ExportMesh", lmodelsExportMesh ); + assingGlobalFunction( "ExportMeshAsCode", lmodelsExportMeshAsCode ); assingGlobalFunction( "GetMeshBoundingBox", lmodelsGetMeshBoundingBox ); assingGlobalFunction( "GenMeshTangents", lmodelsGenMeshTangents ); assingGlobalFunction( "GetMeshData", lmodelsGetMeshData ); @@ -1806,6 +1830,7 @@ void luaRegister() { /* Model animations management functions. */ assingGlobalFunction( "LoadModelAnimations", lmodelsLoadModelAnimations ); assingGlobalFunction( "UpdateModelAnimation", lmodelsUpdateModelAnimation ); + assingGlobalFunction( "UpdateModelAnimationBones", lmodelsUpdateModelAnimationBones ); assingGlobalFunction( "UnloadModelAnimation", lmodelsUnloadModelAnimation ); assingGlobalFunction( "UnloadModelAnimations", lmodelsUnloadModelAnimations ); assingGlobalFunction( "IsModelAnimationValid", lmodelsIsModelAnimationValid ); @@ -1891,6 +1916,8 @@ void luaRegister() { assingGlobalFunction( "TextSplit", ltextTextSplit ); assingGlobalFunction( "TextFindIndex", ltextTextFindIndex ); assingGlobalFunction( "TextToPascal", ltextTextToPascal ); + assingGlobalFunction( "TextToSnake", ltextTextToSnake ); + assingGlobalFunction( "TextToCamel", ltextTextToCamel ); /* Audio. */ /* Audio device management functions. */ @@ -3423,6 +3450,17 @@ AutomationEventList* uluaGetAutomationEventList( lua_State* L, int index ) { } } +void getVector2Array( lua_State* L, int index, Vector2 points[] ) { + int t = index, i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + points[i] = uluaGetVector2( L, lua_gettop( L ) ); + i++; + lua_pop( L, 1 ); + } +} + /* Push types. */ void uluaPushColor( lua_State* L, Color color ) { diff --git a/src/models.c b/src/models.c index 1271713..0caae47 100644 --- a/src/models.c +++ b/src/models.c @@ -982,6 +982,40 @@ int lmodelsDrawModelWiresEx( lua_State* L ) { return 0; } +/* +> RL.DrawModelPoints( Model model, Vector3 position, float scale, Color tint ) + +Draw a model as points +*/ +int lmodelsDrawModelPoints( lua_State* L ) { + Model* model = uluaGetModel( L, 1 ); + Vector3 position = uluaGetVector3( L, 2 ); + float scale = luaL_checknumber( L, 3 ); + Color tint = uluaGetColor( L, 4 ); + + DrawModelPoints( *model, position, scale, tint ); + + return 0; +} + +/* +> RL.DrawModelPointsEx( Model model, Vector3 position, Vector3 rotationAxis, float rotationAngle, Vector3 scale, Color tint ) + +Draw a model as points with extended parameters +*/ +int lmodelsDrawModelPointsEx( lua_State* L ) { + Model* model = uluaGetModel( L, 1 ); + Vector3 position = uluaGetVector3( L, 2 ); + Vector3 rotationAxis = uluaGetVector3( L, 3 ); + float rotationAngle = luaL_checknumber( L, 4 ); + Vector3 scale = uluaGetVector3( L, 5 ); + Color tint = uluaGetColor( L, 6 ); + + DrawModelPointsEx( *model, position, rotationAxis, rotationAngle, scale, tint ); + + return 0; +} + /* > RL.DrawBoundingBox( BoundingBox box, Color color ) @@ -1286,6 +1320,21 @@ int lmodelsExportMesh( lua_State* L ) { return 1; } +/* +> success = RL.ExportMeshAsCode( Mesh mesh, string fileName ) + +Export mesh as code file (.h) defining multiple arrays of vertex attributes + +- Success return bool +*/ +int lmodelsExportMeshAsCode( lua_State* L ) { + Mesh* mesh = uluaGetMesh( L, 1 ); + + lua_pushboolean( L, ExportMeshAsCode( *mesh, luaL_checkstring( L, 2 ) ) ); + + return 1; +} + /* > boundingBox = RL.GetMeshBoundingBox( Mesh mesh ) @@ -2190,6 +2239,21 @@ int lmodelsUpdateModelAnimation( lua_State* L ) { return 0; } +/* +> RL.UpdateModelAnimationBones( Model model, ModelAnimation animation, int frame ) + +Update model animation mesh bone matrices (GPU skinning) +*/ +int lmodelsUpdateModelAnimationBones( lua_State* L ) { + Model* model = uluaGetModel( L, 1 ); + ModelAnimation* animation = uluaGetModelAnimation( L, 2 ); + int frame = luaL_checkinteger( L, 3 ); + + UpdateModelAnimationBones( *model, *animation, frame ); + + return 0; +} + /* > RL.UnloadModelAnimation( ModelAnimation animation ) diff --git a/src/platforms/core_desktop.c b/src/platforms/core_desktop.c deleted file mode 100644 index 8d9f01c..0000000 --- a/src/platforms/core_desktop.c +++ /dev/null @@ -1,656 +0,0 @@ -#include "main.h" -#include "lua_core.h" -#include "core.h" -#include "platforms/core_desktop.h" - -void platformDefineGlobals() { - lua_State* L = state->luaState; - - lua_getglobal( L, "RL" ); -/*DOC_DEFINES_START*/ - /* Keyboard keys (US keyboard layout) */ - assignGlobalInt( GLFW_KEY_UNKNOWN, "GLFW_KEY_UNKNOWN" ); // Key: Unknown - /* GLFW API tokens. */ - assignGlobalInt( GLFW_RELEASE, "GLFW_RELEASE" ); // The key or mouse button was released - assignGlobalInt( GLFW_PRESS, "GLFW_PRESS" ); // The key or mouse button was pressed - assignGlobalInt( GLFW_REPEAT, "GLFW_REPEAT" ); // The key was held down until it repeated - assignGlobalInt( GLFW_CONNECTED, "GLFW_CONNECTED" ); // Joystick connected - assignGlobalInt( GLFW_DISCONNECTED, "GLFW_DISCONNECTED" ); // Joystick disconnected - /* GLFW Window Events. */ - assignGlobalInt( GLFW_WINDOW_SIZE_EVENT, "GLFW_WINDOW_SIZE_EVENT" ); // GLFW event window size changed - assignGlobalInt( GLFW_WINDOW_MAXIMIZE_EVENT, "GLFW_WINDOW_MAXIMIZE_EVENT" ); // GLFW event window maximize - assignGlobalInt( GLFW_WINDOW_ICONYFY_EVENT, "GLFW_WINDOW_ICONYFY_EVENT" ); // GLFW event window iconify - assignGlobalInt( GLFW_WINDOW_FOCUS_EVENT, "GLFW_WINDOW_FOCUS_EVENT" ); // GLFW event window focus - assignGlobalInt( GLFW_WINDOW_DROP_EVENT, "GLFW_WINDOW_DROP_EVENT" ); // GLFW event window drop - /* GLFW Input Events. */ - assignGlobalInt( GLFW_KEY_EVENT, "GLFW_KEY_EVENT" ); // GLFW event keyboard key - assignGlobalInt( GLFW_CHAR_EVENT, "GLFW_CHAR_EVENT" ); // GLFW event Unicode character - assignGlobalInt( GLFW_MOUSE_BUTTON_EVENT, "GLFW_MOUSE_BUTTON_EVENT" ); // GLFW event mouse button - assignGlobalInt( GLFW_MOUSE_CURSOR_POS_EVENT, "GLFW_MOUSE_CURSOR_POS_EVENT" ); // GLFW event cursor position - assignGlobalInt( GLFW_MOUSE_SCROLL_EVENT, "GLFW_MOUSE_SCROLL_EVENT" ); // GLFW event mouse scroll - assignGlobalInt( GLFW_CURSOR_ENTER_EVENT, "GLFW_CURSOR_ENTER_EVENT" ); // GLFW event cursor enter/leave - assignGlobalInt( GLFW_JOYSTICK_EVENT, "GLFW_JOYSTICK_EVENT" ); // GLFW event joystick - /* GLFW Pen Tablet Events. NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445. */ - // assignGlobalInt( GLFW_PEN_TABLET_DATA_EVENT, "GLFW_PEN_TABLET_DATA_EVENT" ); // GLFW event pen tablet data - // assignGlobalInt( GLFW_PEN_TABLET_CURSOR_EVENT, "GLFW_PEN_TABLET_CURSOR_EVENT" ); // GLFW event pen tablet cursor - // assignGlobalInt( GLFW_PEN_TABLET_PROXIMITY_EVENT, "GLFW_PEN_TABLET_PROXIMITY_EVENT" ); // GLFW event pen tablet proximity -/*DOC_DEFINES_END*/ - lua_pop( L, -1 ); -} - -/* Functions. */ - -/* -## GLFW Core - Input-related functions: keyboard -*/ - -/* -> keyName = RL.GetKeyName( int key, int scancode ) - -This function returns the name of the specified printable key, encoded as UTF-8. -If the key is KEY_UNKNOWN, the scancode is used to identify the key, -otherwise the scancode is ignored. If you specify a non-printable key, -or KEY_UNKNOWN and a scancode that maps to a non-printable key, -this function returns nil but does not emit an error. - -- Success return string or nil -*/ -int lcoreGetKeyName( lua_State* L ) { - int key = luaL_checkinteger( L, 1 ); - int scancode = luaL_checkinteger( L, 2 ); - - const char* keyName = glfwGetKeyName( key, scancode ); - - if ( keyName != NULL ) { - lua_pushstring( L, keyName ); - } - else { - lua_pushnil( L ); - } - - return 1; -} - -/* -> scancode = RL.GetKeyScancode( int key ) - -This function returns the platform-specific scancode of the specified key. -If the key is KEY_UNKNOWN or does not exist on the keyboard this method will return -1. - -- Success return int -*/ -int lcoreGetKeyScancode( lua_State* L ) { - int key = luaL_checkinteger( L, 1 ); - - lua_pushinteger( L, glfwGetKeyScancode( key ) ); - - return 1; -} - -/* Events. */ - -/* -## Window events -*/ - -/* -> GLFWwindowsizeEvent = { int type, int width, int height } - -Called when the window is resized. Type GLFW_WINDOW_SIZE_EVENT -*/ -static void windowSizeEvent( GLFWwindow* window, int width, int height ) { -// GLFWwindowsizefun windowSizeEvent( GLFWwindow* window, int width, int height ) { - /* Pass through to raylib callback. */ - state->raylibWindowSizeCallback( window, width, height ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 3, 0 ); - lua_pushinteger( L, GLFW_WINDOW_SIZE_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, width ); - lua_setfield( L, -2, "width" ); - lua_pushinteger( L, height ); - lua_setfield( L, -2, "height" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -#if !defined( PLATFORM_WEB ) - -/* -> GLFWwindowmaximizeEvent = { int type, int maximized } - -Called when the window is maximized or restored. Type GLFW_WINDOW_MAXIMIZE_EVENT -*/ -static void windowMaximizeEvent( GLFWwindow* window, int maximized ) { - /* Pass through to raylib callback. */ - state->raylibWindowMaximizeCallback( window, maximized ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_WINDOW_MAXIMIZE_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, maximized ); - lua_setfield( L, -2, "maximized" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -#endif - -/* -> GLFWwindowiconifyEvent = { int type, int iconified } - -Called when the window is iconified or restored. Type GLFW_WINDOW_ICONYFY_EVENT -*/ -static void windowIconyfyEvent( GLFWwindow* window, int iconified ) { - /* Pass through to raylib callback. */ - state->raylibWindowIconifyCallback( window, iconified ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_WINDOW_ICONYFY_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, iconified ); - lua_setfield( L, -2, "iconified" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWwindowfocusEvent = { int type, int focused } - -Called when the window gains or loses input focus. Type GLFW_WINDOW_FOCUS_EVENT -*/ -static void windowFocusEvent( GLFWwindow* window, int focused ) { - /* Pass through to raylib callback. */ - state->raylibWindowFocusCallback( window, focused ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_WINDOW_FOCUS_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, focused ); - lua_setfield( L, -2, "focused" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWdropEvent = { int type, int count, string{} paths } - -Called when files are dropped to the window. Type GLFW_WINDOW_DROP_EVENT -*/ -static void windowDropEvent( GLFWwindow* window, int count, const char** paths ) { - /* Pass through to raylib callback. */ - state->raylibWindowDropCallback( window, count, paths ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 3, 0 ); - lua_pushinteger( L, GLFW_WINDOW_DROP_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, count ); - lua_setfield( L, -2, "count" ); - - lua_createtable( L, count, 0 ); - - for ( int i = 0; i < count; ++i ) { - lua_pushstring( L, paths[i] ); - lua_rawseti( L, -2, i+1 ); - } - lua_setfield( L, -2, "paths" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -## Input events -*/ - -/* -> GLFWkeyEvent = { int type, int key, int scancode, int action, int mods } - -Called when a physical key is pressed or released or when it repeats. Type GLFW_KEY_EVENT -*/ -static void keyInputEvent( GLFWwindow* window, int key, int scancode, int action, int mods ) { - /* Pass through to raylib callback. */ - state->raylibKeyCallback( window, key, scancode, action, mods ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 5, 0 ); - lua_pushinteger( L, GLFW_KEY_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, key ); - lua_setfield( L, -2, "key" ); - lua_pushinteger( L, scancode ); - lua_setfield( L, -2, "scancode" ); - lua_pushinteger( L, action ); - lua_setfield( L, -2, "action" ); - lua_pushinteger( L, mods ); - lua_setfield( L, -2, "mods" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWcharEvent = { int type, int key } - -Unicode code points for key events that would have led to regular text input and generally behaves as a standard text field on that platform. Type GLFW_CHAR_EVENT -*/ -static void charInputEvent( GLFWwindow* window, unsigned int key ) { - /* Pass through to raylib callback. */ - state->raylibCharCallback( window, key ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_CHAR_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, key ); - lua_setfield( L, -2, "key" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWmousebuttonEvent = { int type, int button, int action, int mods } - -Called when a mouse button is pressed or released. Type GLFW_MOUSE_BUTTON_EVENT -*/ -static void mouseButtonInputEvent( GLFWwindow* window, int button, int action, int mods ) { - /* Pass through to raylib callback. */ - state->raylibMouseButtonCallback( window, button, action, mods ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 4, 0 ); - lua_pushinteger( L, GLFW_MOUSE_BUTTON_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, button ); - lua_setfield( L, -2, "button" ); - lua_pushinteger( L, action ); - lua_setfield( L, -2, "action" ); - lua_pushinteger( L, mods ); - lua_setfield( L, -2, "mods" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWcursorposEvent = { int type, float x, float y } - -Called when the cursor moves over the window. Type GLFW_MOUSE_CURSOR_POS_EVENT -*/ -static void mouseCursorPosInputEvent( GLFWwindow* window, double x, double y ) { - /* Pass through to raylib callback. */ - state->raylibMouseCursorPosCallback( window, x, y ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 3, 0 ); - lua_pushinteger( L, GLFW_MOUSE_CURSOR_POS_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushnumber( L, x ); - lua_setfield( L, -2, "x" ); - lua_pushnumber( L, y ); - lua_setfield( L, -2, "y" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWscrollEvent = { int type, float xoffset, float yoffset } - -Called when the user scrolls, whether with a mouse wheel or touchpad gesture. Type GLFW_MOUSE_SCROLL_EVENT -*/ -static void mouseScrollInputEvent( GLFWwindow* window, double xoffset, double yoffset ) { - /* Pass through to raylib callback. */ - state->raylibMouseScrollCallback( window, xoffset, yoffset ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 3, 0 ); - lua_pushinteger( L, GLFW_MOUSE_SCROLL_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushnumber( L, xoffset ); - lua_setfield( L, -2, "xoffset" ); - lua_pushnumber( L, yoffset ); - lua_setfield( L, -2, "yoffset" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWcursorenterEvent = { int type, int enter } - -Called when the cursor enters or leaves the content area of a window. Type GLFW_CURSOR_ENTER_EVENT -*/ -static void cursorEnterInputEvent( GLFWwindow* window, int enter ) { - /* Pass through to raylib callback. */ - state->raylibCursorEnterCallback( window, enter ); - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_CURSOR_ENTER_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, enter ); - lua_setfield( L, -2, "enter" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWjoystickEvent = { int type, int jid, int event } - -Called when a joystick is connected or disconnected. Type GLFW_JOYSTICK_EVENT -*/ -static void joystickEvent( int jid, int event ) { - /* Pass through to raylib callback. */ - if ( state->raylibJoystickCallback != NULL ) { - state->raylibJoystickCallback( jid, event ); - } - - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 3, 0 ); - lua_pushinteger( L, GLFW_JOYSTICK_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, jid ); - lua_setfield( L, -2, "jid" ); - lua_pushinteger( L, event ); - lua_setfield( L, -2, "event" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWpentabletdataEvent = { int type, float x, float y, float z, float pressure, float pitch, float yaw, float roll } - -Called when the pen tablet data is updated. Type GLFW_PEN_TABLET_DATA_EVENT -NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 -*/ -static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) { - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 8, 0 ); - lua_pushinteger( L, GLFW_PEN_TABLET_DATA_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushnumber( L, x ); - lua_setfield( L, -2, "x" ); - lua_pushnumber( L, y ); - lua_setfield( L, -2, "y" ); - lua_pushnumber( L, z ); - lua_setfield( L, -2, "z" ); - lua_pushnumber( L, pressure ); - lua_setfield( L, -2, "pressure" ); - lua_pushnumber( L, pitch ); - lua_setfield( L, -2, "pitch" ); - lua_pushnumber( L, yaw ); - lua_setfield( L, -2, "yaw" ); - lua_pushnumber( L, roll ); - lua_setfield( L, -2, "roll" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWpentabletcursorEvent = { int type, int identifier } - -Called when the pen tablet cursor has changed. Type GLFW_PEN_TABLET_CURSOR_EVENT -NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 -*/ -static void penTabletCursorEvent( unsigned int identifier ) { - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_PEN_TABLET_CURSOR_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, identifier ); - lua_setfield( L, -2, "identifier" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -/* -> GLFWpentabletproximityEvent = { int type, int proxState } - -Called when the pen tablet proximity has changed. Type GLFW_PEN_TABLET_PROXIMITY_EVENT -NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 -*/ -static void penTabletProximityEvent( int proxState ) { - lua_State* L = state->luaState; - - lua_pushcfunction( L, luaTraceback ); - int tracebackidx = lua_gettop( L ); - - lua_getglobal( L, "RL" ); - lua_getfield( L, -1, "event" ); - - if ( lua_isfunction( L, -1 ) ) { - lua_createtable( L, 2, 0 ); - lua_pushinteger( L, GLFW_PEN_TABLET_PROXIMITY_EVENT ); - lua_setfield( L, -2, "type" ); - lua_pushinteger( L, proxState ); - lua_setfield( L, -2, "state" ); - - if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { - TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); - state->run = false; - } - } - lua_pop( L, -1 ); -} - -static void platformRegisterEvents() { - /* Window events. */ - state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), (GLFWwindowsizefun)windowSizeEvent ); -#if !defined( PLATFORM_WEB ) - state->raylibWindowMaximizeCallback = glfwSetWindowMaximizeCallback( GetWindowHandle(), windowMaximizeEvent ); -#endif - state->raylibWindowIconifyCallback = glfwSetWindowIconifyCallback( GetWindowHandle(), windowIconyfyEvent ); - state->raylibWindowFocusCallback = glfwSetWindowFocusCallback( GetWindowHandle(), windowFocusEvent ); - state->raylibWindowDropCallback = glfwSetDropCallback( GetWindowHandle(), windowDropEvent ); - - /* Input events. */ - state->raylibKeyCallback = glfwSetKeyCallback( GetWindowHandle(), keyInputEvent ); - state->raylibCharCallback = glfwSetCharCallback( GetWindowHandle(), charInputEvent ); - state->raylibMouseButtonCallback = glfwSetMouseButtonCallback( GetWindowHandle(), mouseButtonInputEvent ); - state->raylibMouseCursorPosCallback = glfwSetCursorPosCallback( GetWindowHandle(), mouseCursorPosInputEvent ); - state->raylibMouseScrollCallback = glfwSetScrollCallback( GetWindowHandle(), mouseScrollInputEvent ); - state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); - state->raylibJoystickCallback = glfwSetJoystickCallback( joystickEvent ); - /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ - // state->glfwTabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); - // state->glfwTabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); - // state->glfwTabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); -} - -void luaPlatformRegister() { - lua_State* L = state->luaState; - lua_getglobal( L, "RL" ); - - /* Input-related functions: keyboard. */ - assingGlobalFunction( "GetKeyName", lcoreGetKeyName ); - assingGlobalFunction( "GetKeyScancode", lcoreGetKeyScancode ); - - lua_pop( L, -1 ); - -#ifdef LUA_EVENTS - platformRegisterEvents(); -#endif -} diff --git a/src/platforms/core_desktop_glfw.c b/src/platforms/core_desktop_glfw.c new file mode 100644 index 0000000..8d9f01c --- /dev/null +++ b/src/platforms/core_desktop_glfw.c @@ -0,0 +1,656 @@ +#include "main.h" +#include "lua_core.h" +#include "core.h" +#include "platforms/core_desktop.h" + +void platformDefineGlobals() { + lua_State* L = state->luaState; + + lua_getglobal( L, "RL" ); +/*DOC_DEFINES_START*/ + /* Keyboard keys (US keyboard layout) */ + assignGlobalInt( GLFW_KEY_UNKNOWN, "GLFW_KEY_UNKNOWN" ); // Key: Unknown + /* GLFW API tokens. */ + assignGlobalInt( GLFW_RELEASE, "GLFW_RELEASE" ); // The key or mouse button was released + assignGlobalInt( GLFW_PRESS, "GLFW_PRESS" ); // The key or mouse button was pressed + assignGlobalInt( GLFW_REPEAT, "GLFW_REPEAT" ); // The key was held down until it repeated + assignGlobalInt( GLFW_CONNECTED, "GLFW_CONNECTED" ); // Joystick connected + assignGlobalInt( GLFW_DISCONNECTED, "GLFW_DISCONNECTED" ); // Joystick disconnected + /* GLFW Window Events. */ + assignGlobalInt( GLFW_WINDOW_SIZE_EVENT, "GLFW_WINDOW_SIZE_EVENT" ); // GLFW event window size changed + assignGlobalInt( GLFW_WINDOW_MAXIMIZE_EVENT, "GLFW_WINDOW_MAXIMIZE_EVENT" ); // GLFW event window maximize + assignGlobalInt( GLFW_WINDOW_ICONYFY_EVENT, "GLFW_WINDOW_ICONYFY_EVENT" ); // GLFW event window iconify + assignGlobalInt( GLFW_WINDOW_FOCUS_EVENT, "GLFW_WINDOW_FOCUS_EVENT" ); // GLFW event window focus + assignGlobalInt( GLFW_WINDOW_DROP_EVENT, "GLFW_WINDOW_DROP_EVENT" ); // GLFW event window drop + /* GLFW Input Events. */ + assignGlobalInt( GLFW_KEY_EVENT, "GLFW_KEY_EVENT" ); // GLFW event keyboard key + assignGlobalInt( GLFW_CHAR_EVENT, "GLFW_CHAR_EVENT" ); // GLFW event Unicode character + assignGlobalInt( GLFW_MOUSE_BUTTON_EVENT, "GLFW_MOUSE_BUTTON_EVENT" ); // GLFW event mouse button + assignGlobalInt( GLFW_MOUSE_CURSOR_POS_EVENT, "GLFW_MOUSE_CURSOR_POS_EVENT" ); // GLFW event cursor position + assignGlobalInt( GLFW_MOUSE_SCROLL_EVENT, "GLFW_MOUSE_SCROLL_EVENT" ); // GLFW event mouse scroll + assignGlobalInt( GLFW_CURSOR_ENTER_EVENT, "GLFW_CURSOR_ENTER_EVENT" ); // GLFW event cursor enter/leave + assignGlobalInt( GLFW_JOYSTICK_EVENT, "GLFW_JOYSTICK_EVENT" ); // GLFW event joystick + /* GLFW Pen Tablet Events. NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445. */ + // assignGlobalInt( GLFW_PEN_TABLET_DATA_EVENT, "GLFW_PEN_TABLET_DATA_EVENT" ); // GLFW event pen tablet data + // assignGlobalInt( GLFW_PEN_TABLET_CURSOR_EVENT, "GLFW_PEN_TABLET_CURSOR_EVENT" ); // GLFW event pen tablet cursor + // assignGlobalInt( GLFW_PEN_TABLET_PROXIMITY_EVENT, "GLFW_PEN_TABLET_PROXIMITY_EVENT" ); // GLFW event pen tablet proximity +/*DOC_DEFINES_END*/ + lua_pop( L, -1 ); +} + +/* Functions. */ + +/* +## GLFW Core - Input-related functions: keyboard +*/ + +/* +> keyName = RL.GetKeyName( int key, int scancode ) + +This function returns the name of the specified printable key, encoded as UTF-8. +If the key is KEY_UNKNOWN, the scancode is used to identify the key, +otherwise the scancode is ignored. If you specify a non-printable key, +or KEY_UNKNOWN and a scancode that maps to a non-printable key, +this function returns nil but does not emit an error. + +- Success return string or nil +*/ +int lcoreGetKeyName( lua_State* L ) { + int key = luaL_checkinteger( L, 1 ); + int scancode = luaL_checkinteger( L, 2 ); + + const char* keyName = glfwGetKeyName( key, scancode ); + + if ( keyName != NULL ) { + lua_pushstring( L, keyName ); + } + else { + lua_pushnil( L ); + } + + return 1; +} + +/* +> scancode = RL.GetKeyScancode( int key ) + +This function returns the platform-specific scancode of the specified key. +If the key is KEY_UNKNOWN or does not exist on the keyboard this method will return -1. + +- Success return int +*/ +int lcoreGetKeyScancode( lua_State* L ) { + int key = luaL_checkinteger( L, 1 ); + + lua_pushinteger( L, glfwGetKeyScancode( key ) ); + + return 1; +} + +/* Events. */ + +/* +## Window events +*/ + +/* +> GLFWwindowsizeEvent = { int type, int width, int height } + +Called when the window is resized. Type GLFW_WINDOW_SIZE_EVENT +*/ +static void windowSizeEvent( GLFWwindow* window, int width, int height ) { +// GLFWwindowsizefun windowSizeEvent( GLFWwindow* window, int width, int height ) { + /* Pass through to raylib callback. */ + state->raylibWindowSizeCallback( window, width, height ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 3, 0 ); + lua_pushinteger( L, GLFW_WINDOW_SIZE_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, width ); + lua_setfield( L, -2, "width" ); + lua_pushinteger( L, height ); + lua_setfield( L, -2, "height" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +#if !defined( PLATFORM_WEB ) + +/* +> GLFWwindowmaximizeEvent = { int type, int maximized } + +Called when the window is maximized or restored. Type GLFW_WINDOW_MAXIMIZE_EVENT +*/ +static void windowMaximizeEvent( GLFWwindow* window, int maximized ) { + /* Pass through to raylib callback. */ + state->raylibWindowMaximizeCallback( window, maximized ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_WINDOW_MAXIMIZE_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, maximized ); + lua_setfield( L, -2, "maximized" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +#endif + +/* +> GLFWwindowiconifyEvent = { int type, int iconified } + +Called when the window is iconified or restored. Type GLFW_WINDOW_ICONYFY_EVENT +*/ +static void windowIconyfyEvent( GLFWwindow* window, int iconified ) { + /* Pass through to raylib callback. */ + state->raylibWindowIconifyCallback( window, iconified ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_WINDOW_ICONYFY_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, iconified ); + lua_setfield( L, -2, "iconified" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWwindowfocusEvent = { int type, int focused } + +Called when the window gains or loses input focus. Type GLFW_WINDOW_FOCUS_EVENT +*/ +static void windowFocusEvent( GLFWwindow* window, int focused ) { + /* Pass through to raylib callback. */ + state->raylibWindowFocusCallback( window, focused ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_WINDOW_FOCUS_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, focused ); + lua_setfield( L, -2, "focused" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWdropEvent = { int type, int count, string{} paths } + +Called when files are dropped to the window. Type GLFW_WINDOW_DROP_EVENT +*/ +static void windowDropEvent( GLFWwindow* window, int count, const char** paths ) { + /* Pass through to raylib callback. */ + state->raylibWindowDropCallback( window, count, paths ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 3, 0 ); + lua_pushinteger( L, GLFW_WINDOW_DROP_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, count ); + lua_setfield( L, -2, "count" ); + + lua_createtable( L, count, 0 ); + + for ( int i = 0; i < count; ++i ) { + lua_pushstring( L, paths[i] ); + lua_rawseti( L, -2, i+1 ); + } + lua_setfield( L, -2, "paths" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +## Input events +*/ + +/* +> GLFWkeyEvent = { int type, int key, int scancode, int action, int mods } + +Called when a physical key is pressed or released or when it repeats. Type GLFW_KEY_EVENT +*/ +static void keyInputEvent( GLFWwindow* window, int key, int scancode, int action, int mods ) { + /* Pass through to raylib callback. */ + state->raylibKeyCallback( window, key, scancode, action, mods ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 5, 0 ); + lua_pushinteger( L, GLFW_KEY_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, key ); + lua_setfield( L, -2, "key" ); + lua_pushinteger( L, scancode ); + lua_setfield( L, -2, "scancode" ); + lua_pushinteger( L, action ); + lua_setfield( L, -2, "action" ); + lua_pushinteger( L, mods ); + lua_setfield( L, -2, "mods" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWcharEvent = { int type, int key } + +Unicode code points for key events that would have led to regular text input and generally behaves as a standard text field on that platform. Type GLFW_CHAR_EVENT +*/ +static void charInputEvent( GLFWwindow* window, unsigned int key ) { + /* Pass through to raylib callback. */ + state->raylibCharCallback( window, key ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_CHAR_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, key ); + lua_setfield( L, -2, "key" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWmousebuttonEvent = { int type, int button, int action, int mods } + +Called when a mouse button is pressed or released. Type GLFW_MOUSE_BUTTON_EVENT +*/ +static void mouseButtonInputEvent( GLFWwindow* window, int button, int action, int mods ) { + /* Pass through to raylib callback. */ + state->raylibMouseButtonCallback( window, button, action, mods ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 4, 0 ); + lua_pushinteger( L, GLFW_MOUSE_BUTTON_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, button ); + lua_setfield( L, -2, "button" ); + lua_pushinteger( L, action ); + lua_setfield( L, -2, "action" ); + lua_pushinteger( L, mods ); + lua_setfield( L, -2, "mods" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWcursorposEvent = { int type, float x, float y } + +Called when the cursor moves over the window. Type GLFW_MOUSE_CURSOR_POS_EVENT +*/ +static void mouseCursorPosInputEvent( GLFWwindow* window, double x, double y ) { + /* Pass through to raylib callback. */ + state->raylibMouseCursorPosCallback( window, x, y ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 3, 0 ); + lua_pushinteger( L, GLFW_MOUSE_CURSOR_POS_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushnumber( L, x ); + lua_setfield( L, -2, "x" ); + lua_pushnumber( L, y ); + lua_setfield( L, -2, "y" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWscrollEvent = { int type, float xoffset, float yoffset } + +Called when the user scrolls, whether with a mouse wheel or touchpad gesture. Type GLFW_MOUSE_SCROLL_EVENT +*/ +static void mouseScrollInputEvent( GLFWwindow* window, double xoffset, double yoffset ) { + /* Pass through to raylib callback. */ + state->raylibMouseScrollCallback( window, xoffset, yoffset ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 3, 0 ); + lua_pushinteger( L, GLFW_MOUSE_SCROLL_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushnumber( L, xoffset ); + lua_setfield( L, -2, "xoffset" ); + lua_pushnumber( L, yoffset ); + lua_setfield( L, -2, "yoffset" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWcursorenterEvent = { int type, int enter } + +Called when the cursor enters or leaves the content area of a window. Type GLFW_CURSOR_ENTER_EVENT +*/ +static void cursorEnterInputEvent( GLFWwindow* window, int enter ) { + /* Pass through to raylib callback. */ + state->raylibCursorEnterCallback( window, enter ); + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_CURSOR_ENTER_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, enter ); + lua_setfield( L, -2, "enter" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWjoystickEvent = { int type, int jid, int event } + +Called when a joystick is connected or disconnected. Type GLFW_JOYSTICK_EVENT +*/ +static void joystickEvent( int jid, int event ) { + /* Pass through to raylib callback. */ + if ( state->raylibJoystickCallback != NULL ) { + state->raylibJoystickCallback( jid, event ); + } + + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 3, 0 ); + lua_pushinteger( L, GLFW_JOYSTICK_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, jid ); + lua_setfield( L, -2, "jid" ); + lua_pushinteger( L, event ); + lua_setfield( L, -2, "event" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWpentabletdataEvent = { int type, float x, float y, float z, float pressure, float pitch, float yaw, float roll } + +Called when the pen tablet data is updated. Type GLFW_PEN_TABLET_DATA_EVENT +NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 +*/ +static void penTabletDataEvent( double x, double y, double z, double pressure, double pitch, double yaw, double roll ) { + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 8, 0 ); + lua_pushinteger( L, GLFW_PEN_TABLET_DATA_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushnumber( L, x ); + lua_setfield( L, -2, "x" ); + lua_pushnumber( L, y ); + lua_setfield( L, -2, "y" ); + lua_pushnumber( L, z ); + lua_setfield( L, -2, "z" ); + lua_pushnumber( L, pressure ); + lua_setfield( L, -2, "pressure" ); + lua_pushnumber( L, pitch ); + lua_setfield( L, -2, "pitch" ); + lua_pushnumber( L, yaw ); + lua_setfield( L, -2, "yaw" ); + lua_pushnumber( L, roll ); + lua_setfield( L, -2, "roll" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWpentabletcursorEvent = { int type, int identifier } + +Called when the pen tablet cursor has changed. Type GLFW_PEN_TABLET_CURSOR_EVENT +NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 +*/ +static void penTabletCursorEvent( unsigned int identifier ) { + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_PEN_TABLET_CURSOR_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, identifier ); + lua_setfield( L, -2, "identifier" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +/* +> GLFWpentabletproximityEvent = { int type, int proxState } + +Called when the pen tablet proximity has changed. Type GLFW_PEN_TABLET_PROXIMITY_EVENT +NOTE: Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 +*/ +static void penTabletProximityEvent( int proxState ) { + lua_State* L = state->luaState; + + lua_pushcfunction( L, luaTraceback ); + int tracebackidx = lua_gettop( L ); + + lua_getglobal( L, "RL" ); + lua_getfield( L, -1, "event" ); + + if ( lua_isfunction( L, -1 ) ) { + lua_createtable( L, 2, 0 ); + lua_pushinteger( L, GLFW_PEN_TABLET_PROXIMITY_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, proxState ); + lua_setfield( L, -2, "state" ); + + if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { + TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) ); + state->run = false; + } + } + lua_pop( L, -1 ); +} + +static void platformRegisterEvents() { + /* Window events. */ + state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), (GLFWwindowsizefun)windowSizeEvent ); +#if !defined( PLATFORM_WEB ) + state->raylibWindowMaximizeCallback = glfwSetWindowMaximizeCallback( GetWindowHandle(), windowMaximizeEvent ); +#endif + state->raylibWindowIconifyCallback = glfwSetWindowIconifyCallback( GetWindowHandle(), windowIconyfyEvent ); + state->raylibWindowFocusCallback = glfwSetWindowFocusCallback( GetWindowHandle(), windowFocusEvent ); + state->raylibWindowDropCallback = glfwSetDropCallback( GetWindowHandle(), windowDropEvent ); + + /* Input events. */ + state->raylibKeyCallback = glfwSetKeyCallback( GetWindowHandle(), keyInputEvent ); + state->raylibCharCallback = glfwSetCharCallback( GetWindowHandle(), charInputEvent ); + state->raylibMouseButtonCallback = glfwSetMouseButtonCallback( GetWindowHandle(), mouseButtonInputEvent ); + state->raylibMouseCursorPosCallback = glfwSetCursorPosCallback( GetWindowHandle(), mouseCursorPosInputEvent ); + state->raylibMouseScrollCallback = glfwSetScrollCallback( GetWindowHandle(), mouseScrollInputEvent ); + state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); + state->raylibJoystickCallback = glfwSetJoystickCallback( joystickEvent ); + /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ + // state->glfwTabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); + // state->glfwTabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); + // state->glfwTabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); +} + +void luaPlatformRegister() { + lua_State* L = state->luaState; + lua_getglobal( L, "RL" ); + + /* Input-related functions: keyboard. */ + assingGlobalFunction( "GetKeyName", lcoreGetKeyName ); + assingGlobalFunction( "GetKeyScancode", lcoreGetKeyScancode ); + + lua_pop( L, -1 ); + +#ifdef LUA_EVENTS + platformRegisterEvents(); +#endif +} diff --git a/src/shapes.c b/src/shapes.c index 2efa78a..07f40c3 100644 --- a/src/shapes.c +++ b/src/shapes.c @@ -4,17 +4,6 @@ #include "lua_core.h" #include "textures.h" -static inline void getVector2Array( lua_State* L, int index, Vector2 points[] ) { - int t = index, i = 0; - lua_pushnil( L ); - - while ( lua_next( L, t ) != 0 ) { - points[i] = uluaGetVector2( L, lua_gettop( L ) ); - i++; - lua_pop( L, 1 ); - } -} - /* ## Shapes - Basic shapes drawing functions */ @@ -30,11 +19,39 @@ int lshapesSetShapesTexture( lua_State* L ) { Texture* texture = uluaGetTexture( L, 1 ); Rectangle source = uluaGetRectangle( L, 2 ); + state->shapesTexture = *texture; + SetShapesTexture( *texture, source ); return 0; } +/* +> texture = RL.GetShapesTexture() + +Get texture that is used for shapes drawing. Return as lightuserdata + +- Success return Texture +*/ +int lshapesGetShapesTexture( lua_State* L ) { + lua_pushlightuserdata( L, &state->shapesTexture ); + + return 1; +} + +/* +> source = RL.GetShapesTextureRectangle() + +Get texture source rectangle that is used for shapes drawing + +- Success return Rectangle +*/ +int lshapesGetShapesTextureRectangle( lua_State* L ) { + uluaPushRectangle( L, GetShapesTextureRectangle() ); + + return 1; +} + /* > RL.DrawPixel( Vector2 pos, Color color ) @@ -874,6 +891,24 @@ int lshapesCheckCollisionCircleRec( lua_State* L ) { return 1; } +/* +> collision = RL.CheckCollisionCircleLine( Vector2 center, float radius, Vector2 p1, Vector2 p2 ) + +Check if circle collides with a line created betweeen two points [p1] and [p2] + +- Success return bool +*/ +int lshapesCheckCollisionCircleLine( lua_State* L ) { + Vector2 center = uluaGetVector2( L, 1 ); + float radius = luaL_checknumber( L, 2 ); + Vector2 p1 = uluaGetVector2( L, 3 ); + Vector2 p2 = uluaGetVector2( L, 4 ); + + lua_pushboolean( L, CheckCollisionCircleLine( center, radius, p1, p2 ) ); + + return 1; +} + /* > collision = RL.CheckCollisionPointRec( Vector2 point, Rectangle rec ) diff --git a/src/state.c b/src/state.c index 70045de..9f3fb5a 100644 --- a/src/state.c +++ b/src/state.c @@ -35,6 +35,7 @@ bool stateInit( int argn, const char** argc, const char* basePath ) { state->guiFont = GuiGetFont(); state->defaultMaterial = LoadMaterialDefault(); state->defaultTexture = (Texture){ 1, 1, 1, 1, 7 }; + state->shapesTexture = (Texture){ 1, 1, 1, 1, 7 }; state->RLGLcurrentShaderLocs = malloc( RL_MAX_SHADER_LOCATIONS * sizeof( int ) ); int* defaultShaderLocs = rlGetShaderLocsDefault(); diff --git a/src/text.c b/src/text.c index 9b6fe95..2740bed 100644 --- a/src/text.c +++ b/src/text.c @@ -1242,3 +1242,33 @@ int ltextTextToPascal( lua_State* L ) { return 1; } + +/* +> text = RL.TextToSnake( string text ) + +Get Snake case notation version of provided string + +- Success return string +*/ +int ltextTextToSnake( lua_State* L ) { + const char* text = luaL_checkstring( L, 1 ); + + lua_pushstring( L, TextToSnake( text ) ); + + return 1; +} + +/* +> text = RL.TextToCamel( string text ) + +Get Camel case notation version of provided string + +- Success return string +*/ +int ltextTextToCamel( lua_State* L ) { + const char* text = luaL_checkstring( L, 1 ); + + lua_pushstring( L, TextToCamel( text ) ); + + return 1; +} diff --git a/src/textures.c b/src/textures.c index d571a32..eaf2284 100644 --- a/src/textures.c +++ b/src/textures.c @@ -77,6 +77,24 @@ int ltexturesLoadImageAnim( lua_State* L ) { return 1; } +/* +> image, frameCount = RL.LoadImageAnimFromMemory( string fileType, Buffer fileData ) + +Load image sequence from memory buffer. All frames are returned in RGBA format + +- Success return Image, int +*/ +int ltexturesLoadImageAnimFromMemory( lua_State* L ) { + const char* fileType = luaL_checkstring( L, 1 ); + Buffer* buffer = uluaGetBuffer( L, 2 ); + + int frameCount = 0; + uluaPushImage( L, LoadImageAnimFromMemory( fileType, buffer->data, buffer->size, &frameCount ) ); + lua_pushinteger( L, frameCount ); + + return 2; +} + /* > image, frameCount = RL.LoadImageFromMemory( string fileType, Buffer data ) @@ -417,6 +435,22 @@ int ltexturesImageFromImage( lua_State* L ) { return 1; } +/* +> image = RL.ImageFromChannel( Image image, int selectedChannel ) + +Create an image from a selected channel of another image (GRAYSCALE) + +- Success return Image +*/ +int ltexturesImageFromChannel( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + int selectedChannel = luaL_checkinteger( L, 2 ); + + uluaPushImage( L, ImageFromChannel( *image, selectedChannel ) ); + + return 1; +} + /* > image = RL.ImageText( string text, int fontSize, Color tint ) @@ -565,6 +599,32 @@ int ltexturesImageBlurGaussian( lua_State* L ) { return 0; } +/* +> RL.ImageKernelConvolution( Image image, float{} kernel ) + +Apply custom square convolution kernel to image +*/ +int ltexturesImageKernelConvolution( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + + int kernelSize = uluaGetTableLen( L, 2 ); + float kernel[ kernelSize ]; + + int t = lua_gettop( L ); + int i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 ) { + kernel[i] = lua_tonumber( L, -1 ); + i++; + lua_pop( L, 1 ); + } + + ImageKernelConvolution( image, kernel, kernelSize ); + + return 0; +} + /* > RL.ImageResize( Image image, Vector2 size ) @@ -971,7 +1031,7 @@ int ltexturesImageDrawPixel( lua_State* L ) { } /* -> RL.ImageDrawLine( Image dst, Vector2 a, Vector2 b, Color color ) +> RL.ImageDrawLine( Image dst, Vector2 start, Vector2 end, Color color ) Draw line within an image */ @@ -986,6 +1046,23 @@ int ltexturesImageDrawLine( lua_State* L ) { return 0; } +/* +> RL.ImageDrawLineEx( Image dst, Vector2 start, Vector2 end, int thick, Color color ) + +Draw a line defining thickness within an image +*/ +int ltexturesImageDrawLineEx( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + Vector2 start = uluaGetVector2( L, 2 ); + Vector2 end = uluaGetVector2( L, 3 ); + int thick = luaL_checkinteger( L, 4 ); + Color color = uluaGetColor( L, 5 ); + + ImageDrawLineEx( image, start, end, thick, color ); + + return 0; +} + /* > RL.ImageDrawCircle( Image dst, Vector2 center, int radius, Color color ) @@ -1049,6 +1126,93 @@ int ltexturesImageDrawRectangleLines( lua_State* L ) { return 0; } +/* +> RL.ImageDrawTriangle( Image *dst, Vector2 v1, Vector2 v2, Vector2 v3, Color color ) + +Draw triangle within an image +*/ +int ltexturesImageDrawTriangle( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + Vector2 v1 = uluaGetVector2( L, 2 ); + Vector2 v2 = uluaGetVector2( L, 3 ); + Vector2 v3 = uluaGetVector2( L, 4 ); + Color color = uluaGetColor( L, 5 ); + + ImageDrawTriangle( image, v1, v2, v3, color ); + + return 0; +} + +/* +> RL.ImageDrawTriangleEx( Image *dst, Vector2 v1, Vector2 v2, Vector2 v3, Color c1, Color c2, Color c3 ) + +Draw triangle with interpolated colors within an image +*/ +int ltexturesImageDrawTriangleEx( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + Vector2 v1 = uluaGetVector2( L, 2 ); + Vector2 v2 = uluaGetVector2( L, 3 ); + Vector2 v3 = uluaGetVector2( L, 4 ); + Color c1 = uluaGetColor( L, 5 ); + Color c2 = uluaGetColor( L, 6 ); + Color c3 = uluaGetColor( L, 7 ); + + ImageDrawTriangleEx( image, v1, v2, v3, c1, c2, c3 ); + + return 0; +} + +/* +> RL.ImageDrawTriangleLines( Image *dst, Vector2 v1, Vector2 v2, Vector2 v3, Color color ) + +Draw triangle outline within an image +*/ +int ltexturesImageDrawTriangleLines( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + Vector2 v1 = uluaGetVector2( L, 2 ); + Vector2 v2 = uluaGetVector2( L, 3 ); + Vector2 v3 = uluaGetVector2( L, 4 ); + Color color = uluaGetColor( L, 5 ); + + ImageDrawTriangleLines( image, v1, v2, v3, color ); + + return 0; +} + +/* +> RL.ImageDrawTriangleFan( Image *dst, Vector2{} points, Color color ) + +Draw a triangle fan defined by points within an image (first vertex is the center) +*/ +int ltexturesImageDrawTriangleFan( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + int pointCount = uluaGetTableLen( L, 2 ); + Vector2 points[ pointCount ]; + getVector2Array( L, 2, points ); + Color color = uluaGetColor( L, 3 ); + + ImageDrawTriangleFan( image, points, pointCount, color ); + + return 0; +} + +/* +> RL.ImageDrawTriangleStrip( Image *dst, Vector2{} points, Color color ) + +Draw a triangle strip defined by points within an image +*/ +int ltexturesImageDrawTriangleStrip( lua_State* L ) { + Image* image = uluaGetImage( L, 1 ); + int pointCount = uluaGetTableLen( L, 2 ); + Vector2 points[ pointCount ]; + getVector2Array( L, 2, points ); + Color color = uluaGetColor( L, 3 ); + + ImageDrawTriangleStrip( image, points, pointCount, color ); + + return 0; +} + /* > RL.ImageDraw( Image dst, Image src, Rectangle srcRec, Rectangle dstRec, Color tint ) @@ -1855,6 +2019,22 @@ int ltexturesGetRenderTextureDepthTexture( lua_State* L ) { ## Textures - Color/pixel related functions */ +/* +> isEqual = RL.ColorIsEqual( Color col1, Color col2 ) + +Check if two colors are equal + +- Success return bool +*/ +int ltexturesColorIsEqual( lua_State* L ) { + Color col1 = uluaGetColor( L, 1 ); + Color col2 = uluaGetColor( L, 2 ); + + lua_pushboolean( L, ColorIsEqual( col1, col2 ) ); + + return 1; +} + /* > color = RL.Fade( Color color, float alpha ) @@ -2029,6 +2209,23 @@ int ltexturesColorAlphaBlend( lua_State* L ) { return 1; } +/* +> color = RL.ColorLerp( Color color1, Color color2, float factor ) + +Get color lerp interpolation between two colors, factor [0.0f..1.0f] + +- Success return Color +*/ +int ltexturesColorLerp( lua_State* L ) { + Color color1 = uluaGetColor( L, 1 ); + Color color2 = uluaGetColor( L, 2 ); + float factor = luaL_checknumber( L, 3 ); + + uluaPushColor( L, ColorLerp( color1, color2, factor ) ); + + return 1; +} + /* > color = RL.GetColor( int hexValue ) -- cgit v1.2.3