From 1ab9722875c543e8fd1b6600fd16e51412181641 Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 23 Nov 2023 00:00:49 +0200 Subject: Support for different platforms. Platform_desktop_sdl. --- API.md | 152 +---------- CMakeLists.txt | 30 ++- ReiLua_API.lua | 77 +----- changelog | 3 + cmake/EnumOption.cmake | 9 + devnotes | 6 +- examples/events/main.lua | 46 ++-- examples/resources/lib/vector2.lua | 8 + examples/snake/main.lua | 13 +- include/lua_core.h | 22 +- include/main.h | 9 +- include/platforms/core_desktop.h | 16 ++ include/platforms/core_desktop_sdl.h | 8 + include/state.h | 7 +- src/core.c | 50 ---- src/lua_core.c | 430 ++----------------------------- src/main.c | 2 +- src/platforms/core_desktop.c | 484 +++++++++++++++++++++++++++++++++++ src/platforms/core_desktop_sdl.c | 127 +++++++++ src/rmath.c | 9 +- 20 files changed, 775 insertions(+), 733 deletions(-) create mode 100644 cmake/EnumOption.cmake create mode 100644 include/platforms/core_desktop.h create mode 100644 include/platforms/core_desktop_sdl.h create mode 100644 src/platforms/core_desktop.c create mode 100644 src/platforms/core_desktop_sdl.c diff --git a/API.md b/API.md index f201cce..1f8a6f9 100644 --- a/API.md +++ b/API.md @@ -482,13 +482,6 @@ Disable logging ## Globals - KeyboardKey - -GLFW_KEY_UNKNOWN = nil - -Key: Unknown - ---- - > KEY_NULL = 0 Key: NULL, used for no key pressed @@ -3414,38 +3407,6 @@ Framebuffer texture attachment type: renderbuffer --- -## Globals - GLFW -> GLFW_RELEASE = 0 - -The key or mouse button was released - ---- - -> GLFW_PRESS = 1 - -The key or mouse button was pressed - ---- - -> GLFW_REPEAT = 2 - -The key was held down until it repeated - ---- - -> GLFW_CONNECTED = 262145 - -Joystick connected - ---- - -> GLFW_DISCONNECTED = 262146 - -Joystick disconnected - ---- - - ## Globals - CBuffer > BUFFER_UNSIGNED_CHAR = 0 @@ -3496,82 +3457,6 @@ C type double --- -## Globals - Window -> EVENT_WINDOW_SIZE = 0 - -GLFW event window size changed - ---- - -> EVENT_WINDOW_MAXIMIZE = 1 - -GLFW event window maximize - ---- - -> EVENT_WINDOW_ICONYFY = 2 - -GLFW event window iconify - ---- - -> EVENT_WINDOW_FOCUS = 3 - -GLFW event window focus - ---- - -> EVENT_WINDOW_DROP = 4 - -GLFW event window drop - ---- - - -## Globals - Input -> EVENT_KEY = 5 - -GLFW event keyboard key - ---- - -> EVENT_CHAR = 6 - -GLFW event Unicode character - ---- - -> EVENT_MOUSE_BUTTON = 7 - -GLFW event mouse button - ---- - -> EVENT_MOUSE_CURSOR_POS = 8 - -GLFW event cursor position - ---- - -> EVENT_MOUSE_SCROLL = 9 - -GLFW event mouse scroll - ---- - -> EVENT_CURSOR_ENTER = 10 - -GLFW event cursor enter/leave - ---- - -> EVENT_JOYSTICK = 11 - -GLFW event joystick - ---- - - ## Core - Window-related functions --- @@ -4524,34 +4409,6 @@ Set a custom key to exit program (default is ESC) --- -> keyName = RL.GetKeyName( int key, int scancode ) - -This function returns the name of the specified printable key, encoded as UTF-8. -This is typically the character that key would produce without any modifier keys, -intended for displaying key bindings to the user. For dead keys, it is typically -the diacritic it would add to a character. - -Do not use this function for text input. You will break text input for many -languages even if it happens to work for yours. - -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 - ---- - -> 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 - ---- - ## Core - Input-related functions: gamepads --- @@ -7546,7 +7403,8 @@ Calculate square distance between two vectors > result = RL.Vector2Angle( Vector2 v1, Vector2 v2 ) -Calculate angle from two vectors +Calculate angle between two vectors +NOTE: Angle is calculated from origin point (0, 0) - Success return float @@ -7554,9 +7412,9 @@ Calculate angle from two vectors > result = RL.Vector2LineAngle( Vector2 a, Vector2 b ) -Calculate angle defined by a two vectors line. -NOTE: Parameters need to be normalized. -Current implementation should be aligned with glm::angle. +Calculate angle defined by a two vectors line +NOTE: Parameters need to be normalized +Current implementation should be aligned with glm::angle - Success return float diff --git a/CMakeLists.txt b/CMakeLists.txt index f2b4c7d..381c4a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,7 @@ +set( CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ) +include( CMakeDependentOption ) +include( EnumOption ) + cmake_minimum_required( VERSION 3.9 ) project( ReiLua ) @@ -9,6 +13,8 @@ set( CMAKE_C_STANDARD 99 ) # Requires C99 standard option( SHARED "Build using dynamic libraries." off ) option( LUAJIT "Use LuaJIT." off ) +enum_option( PLATFORM "Desktop;Desktop_SDL" "Platform to build for." ) + if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES ) set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE ) set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" ) @@ -19,6 +25,12 @@ file( GLOB SOURCES src/*.c ) include_directories( include ) add_executable( ${PROJECT_NAME} ${SOURCES} ) +if( PLATFORM STREQUAL "Desktop" ) + set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_DESKTOP" ) +elseif( PLATFORM STREQUAL "Desktop_SDL" ) + set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPLATFORM_DESKTOP_SDL" ) +endif() + if( EMSCRIPTEN ) # Web target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/libraylib.a ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/liblua.a ) @@ -32,7 +44,7 @@ else() # Desktop if( SHARED ) message( Shared ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSHARED" ) - # find_package( raylib 4.5 REQUIRED ) # Requires at least version 4.5 + # find_package( raylib 5.0 REQUIRED ) # Requires at least version 5.0 target_link_libraries( ${PROJECT_NAME} raylib ) if( LUAJIT ) @@ -48,14 +60,20 @@ else() # Desktop set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLUAJIT" ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libluajit.a ) else() - # target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/liblua.a ) - target_link_libraries( ${PROJECT_NAME} lua ) + target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/liblua.a ) endif() endif() if( UNIX ) set( CMAKE_C_COMPILER "gcc" ) + if ( PLATFORM MATCHES "Desktop_SDL" ) + include( FindPkgConfig ) + pkg_search_module( SDL2 REQUIRED sdl2 ) + include_directories( ${SDL2_INCLUDE_DIRS} ) + target_link_libraries( ${PROJECT_NAME} ${SDL2_LIBRARIES} ) + endif() + if( DRM ) # Mainly for Raspberry Pi # target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm rt bcm_host m dl pthread ) # target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm pthread rt m dl ) @@ -70,6 +88,12 @@ else() # Desktop target_link_libraries( ${PROJECT_NAME} "-framework Cocoa" ) target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" ) elseif( WIN32 ) + if ( PLATFORM MATCHES "Desktop_SDL" ) + find_package( SDL2 REQUIRED ) + include_directories( ${SDL2_INCLUDE_DIRS} ) + target_link_libraries( ${PROJECT_NAME} ${SDL2MAIN_LIBRARIES} ) + target_link_libraries( ${PROJECT_NAME} ${SDL2_LIBRARIES} ) + endif() # Remove this to get console. set( CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-mwindows" ) target_link_libraries( ${PROJECT_NAME} mingw32 opengl32 gdi32 winmm ) diff --git a/ReiLua_API.lua b/ReiLua_API.lua index 3d59b78..b5e57e4 100644 --- a/ReiLua_API.lua +++ b/ReiLua_API.lua @@ -75,8 +75,6 @@ RL.LOG_NONE=7 -- Globals - KeyboardKey ----Key: Unknown -RL.GLFW_KEY_UNKNOWN=nil ---Key: NULL, used for no key pressed RL.KEY_NULL=0 ---Key: ' @@ -1166,19 +1164,6 @@ RL.GL_STENCIL_BUFFER_BIT=1024 RL.GL_NEAREST=9728 RL.GL_LINEAR=9729 --- Globals - GLFW - ----The key or mouse button was released -RL.GLFW_RELEASE=0 ----The key or mouse button was pressed -RL.GLFW_PRESS=1 ----The key was held down until it repeated -RL.GLFW_REPEAT=2 ----Joystick connected -RL.GLFW_CONNECTED=262145 ----Joystick disconnected -RL.GLFW_DISCONNECTED=262146 - -- Globals - CBuffer ---C type unsigned char @@ -1197,36 +1182,6 @@ RL.BUFFER_INT=5 RL.BUFFER_FLOAT=6 ---C type double RL.BUFFER_DOUBLE=7 - --- Globals - Window - ----GLFW event window size changed -RL.EVENT_WINDOW_SIZE=0 ----GLFW event window maximize -RL.EVENT_WINDOW_MAXIMIZE=1 ----GLFW event window iconify -RL.EVENT_WINDOW_ICONYFY=2 ----GLFW event window focus -RL.EVENT_WINDOW_FOCUS=3 ----GLFW event window drop -RL.EVENT_WINDOW_DROP=4 - --- Globals - Input - ----GLFW event keyboard key -RL.EVENT_KEY=5 ----GLFW event Unicode character -RL.EVENT_CHAR=6 ----GLFW event mouse button -RL.EVENT_MOUSE_BUTTON=7 ----GLFW event cursor position -RL.EVENT_MOUSE_CURSOR_POS=8 ----GLFW event mouse scroll -RL.EVENT_MOUSE_SCROLL=9 ----GLFW event cursor enter/leave -RL.EVENT_CURSOR_ENTER=10 ----GLFW event joystick -RL.EVENT_JOYSTICK=11 -- Core - Window-related functions ---Close window and unload OpenGL context and free all resources @@ -1943,29 +1898,6 @@ function RL.GetCharPressed() end ---@return any RL.SetExitKey function RL.SetExitKey( key ) end ----This function returns the name of the specified printable key, encoded as UTF-8. ----This is typically the character that key would produce without any modifier keys, ----intended for displaying key bindings to the user. For dead keys, it is typically ----the diacritic it would add to a character. ----Do not use this function for text input. You will break text input for many ----languages even if it happens to work for yours. ----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 ----@param key integer ----@param scancode integer ----@return any keyName -function RL.GetKeyName( key, scancode ) end - ----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 ----@param key integer ----@return any scancode -function RL.GetKeyScancode( key ) end - -- Core - Input-related functions: gamepads ---Detect if a gamepad is available @@ -4799,16 +4731,17 @@ function RL.Vector2Distance( v1, v2 ) end ---@return any result function RL.Vector2DistanceSqr( v1, v2 ) end ----Calculate angle from two vectors +---Calculate angle between two vectors +---NOTE: Angle is calculated from origin point (0, 0) ---- Success return float ---@param v1 table ---@param v2 table ---@return any result function RL.Vector2Angle( v1, v2 ) end ----Calculate angle defined by a two vectors line. ----NOTE: Parameters need to be normalized. ----Current implementation should be aligned with glm::angle. +---Calculate angle defined by a two vectors line +---NOTE: Parameters need to be normalized +---Current implementation should be aligned with glm::angle ---- Success return float ---@param a table ---@param b table diff --git a/changelog b/changelog index 980ece9..cef5430 100644 --- a/changelog +++ b/changelog @@ -9,6 +9,9 @@ KEY CHANGES: - ADDED: More Window-related functions. - ADDED: Spline functions. - CHANGE: Raygui to version 4.0. Gui libs also updated. + - ADDED: Support for platforms. + - ADDED: Platform desktop SDL. + - CHANGE: Renamed event enums. Events are now platform specific. DETAILED CHANGES: - REMOVED: DrawLineBezierQuad, DrawLineBezierCubic. diff --git a/cmake/EnumOption.cmake b/cmake/EnumOption.cmake new file mode 100644 index 0000000..d7d343f --- /dev/null +++ b/cmake/EnumOption.cmake @@ -0,0 +1,9 @@ +macro(enum_option var values description) + set(${var}_VALUES ${values}) + list(GET ${var}_VALUES 0 default) + set(${var} "${default}" CACHE STRING "${description}") + set_property(CACHE ${var} PROPERTY STRINGS ${${var}_VALUES}) + if (NOT ";${${var}_VALUES};" MATCHES ";${${var}};") + message(FATAL_ERROR "Unknown value ${${var}}. Only -D${var}=${${var}_VALUES} allowed.") + endif() +endmacro() diff --git a/devnotes b/devnotes index 2bd4fe7..3592cc0 100644 --- a/devnotes +++ b/devnotes @@ -2,8 +2,10 @@ Current { } Backlog { - * SDL_Desktop platform option. Might have to move events to platform specific file since events are not - the same in different platforms. + * Platform specific API documentation. + * Platform desktop SDL. + * Thread safe Lua RL.event calling. + * Haptic functions. * Text * Text codepoints management functions (unicode characters)? Could be usefull for luajit. * Some of the Text strings management functions could be easier to use than the Lua ones. diff --git a/examples/events/main.lua b/examples/events/main.lua index 5643668..64aa32d 100644 --- a/examples/events/main.lua +++ b/examples/events/main.lua @@ -9,27 +9,27 @@ function RL.init() end local function getEventType( event ) - if event.type == RL.EVENT_WINDOW_SIZE then + if event.type == RL.GLFW_WINDOW_SIZE_EVENT then return "Window Size" - elseif event.type == RL.EVENT_WINDOW_MAXIMIZE then + elseif event.type == RL.GLFW_WINDOW_MAXIMIZE_EVENT then return "Window Maximized" - elseif event.type == RL.EVENT_WINDOW_ICONYFY then + elseif event.type == RL.GLFW_WINDOW_ICONYFY_EVENT then return "Window Iconyfy" - elseif event.type == RL.EVENT_WINDOW_FOCUS then + elseif event.type == RL.GLFW_WINDOW_FOCUS_EVENT then return "Window Focus" - elseif event.type == RL.EVENT_WINDOW_DROP then + elseif event.type == RL.GLFW_WINDOW_DROP_EVENT then return "Window Drop" - elseif event.type == RL.EVENT_KEY then + elseif event.type == RL.GLFW_KEY_EVENT then return "Key" - elseif event.type == RL.EVENT_CHAR then + elseif event.type == RL.GLFW_CHAR_EVENT then return "Char" - elseif event.type == RL.EVENT_MOUSE_BUTTON then + elseif event.type == RL.GLFW_MOUSE_BUTTON_EVENT then return "Mouse Button" - elseif event.type == RL.EVENT_MOUSE_CURSOR_POS then + elseif event.type == RL.GLFW_MOUSE_CURSOR_POS_EVENT then return "Mouse Cursor Position" - elseif event.type == RL.EVENT_MOUSE_SCROLL then + elseif event.type == RL.GLFW_MOUSE_SCROLL_EVENT then return "Mouse Scroll" - elseif event.type == RL.EVENT_CURSOR_ENTER then + elseif event.type == RL.GLFW_CURSOR_ENTER_EVENT then return "Cursor Enter" elseif event.type == RL.EVENT_JOYSTICK then return "Joystick" @@ -63,33 +63,33 @@ end function RL.event( event ) text = "Event: "..getEventType( event ).."\n" - if event.type == RL.EVENT_WINDOW_SIZE then + if event.type == RL.GLFW_WINDOW_SIZE_EVENT then text = text.."width: "..event.width.." height: "..event.height - elseif event.type == RL.EVENT_WINDOW_MAXIMIZE then + elseif event.type == RL.GLFW_WINDOW_MAXIMIZE_EVENT then text = text.."maximized: "..event.maximized - elseif event.type == RL.EVENT_WINDOW_ICONYFY then + elseif event.type == RL.GLFW_WINDOW_ICONYFY_EVENT then text = text.."iconified: "..event.iconified - elseif event.type == RL.EVENT_WINDOW_FOCUS then + elseif event.type == RL.GLFW_WINDOW_FOCUS_EVENT then text = text.."focused: "..event.focused - elseif event.type == RL.EVENT_WINDOW_DROP then + elseif event.type == RL.GLFW_WINDOW_DROP_EVENT then text = text.."count: "..event.count.."\n" for _, path in ipairs( event.paths ) do text = text..path.."\n" end - elseif event.type == RL.EVENT_KEY then + elseif event.type == RL.GLFW_KEY_EVENT then text = text.."key: "..event.key.." scancode: "..event.scancode.." action: "..getAction( event.action ).." mods: "..event.mods text = text .."\nkeyName: "..keyName( event.key ) - elseif event.type == RL.EVENT_CHAR then + elseif event.type == RL.GLFW_CHAR_EVENT then text = text.."key: "..event.key -- text = text .."\nchar: "..string.char( event.key ) text = text .."\nchar: "..utf8.char( event.key ) - elseif event.type == RL.EVENT_MOUSE_BUTTON then + elseif event.type == RL.GLFW_MOUSE_BUTTON_EVENT then text = text.."button: "..event.button.." action: "..getAction( event.action ).." mods: "..event.mods - elseif event.type == RL.EVENT_MOUSE_CURSOR_POS then + elseif event.type == RL.GLFW_MOUSE_CURSOR_POS_EVENT then text = text.."x: "..event.x.." y: "..event.y - elseif event.type == RL.EVENT_MOUSE_SCROLL then + elseif event.type == RL.GLFW_MOUSE_SCROLL_EVENT then text = text.."xoffset: "..event.xoffset.." yoffset: "..event.yoffset - elseif event.type == RL.EVENT_CURSOR_ENTER then + elseif event.type == RL.GLFW_CURSOR_ENTER_EVENT then text = text.."enter: "..event.enter cursorIn = event.enter elseif event.type == RL.EVENT_JOYSTICK then @@ -99,6 +99,8 @@ function RL.event( event ) elseif event.event == RL.GLFW_DISCONNECTED then text = text.."\nDisconnected" end + elseif event.type == RL.SDL_KEYBOARD_EVENT then + text = text.."state: "..event.state end end diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua index c6efd5c..e3df887 100644 --- a/examples/resources/lib/vector2.lua +++ b/examples/resources/lib/vector2.lua @@ -126,6 +126,14 @@ function Vector2:angle( v2 ) return RL.Vector2Angle( self, v2 ) end +function Vector2:lineAngle( v2 ) + return RL.Vector2LineAngle( self, v2 ) +end + +function Vector2:atan() + return math.atan( self.x, self.y ) +end + function Vector2:scale( scale ) return Vector2:new( RL.Vector2Scale( self, scale ) ) end diff --git a/examples/snake/main.lua b/examples/snake/main.lua index 4273481..047cddf 100644 --- a/examples/snake/main.lua +++ b/examples/snake/main.lua @@ -1,5 +1,6 @@ package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" +Util = require( "utillib" ) Vec2 = require( "vector2" ) Rect = require( "rectangle" ) @@ -174,14 +175,14 @@ end local function drawSnake() for i, seg in ipairs( snake.segments ) do - local angle = math.deg( RL.Vector2Angle( { 0, 0 }, seg.heading ) ) + local angle = -seg.heading:atan() + RL.PI / 2 local source = Rect:new( 16, 0, 8, 8 ) if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment. source.x = 8 if 1 < #snake.segments then - angle = math.deg( RL.Vector2Angle( { 0, 0 }, snake.segments[ 2 ].heading ) ) + angle = -snake.segments[ 2 ].heading:atan() + RL.PI / 2 end elseif i < #snake.segments and seg.heading ~= snake.segments[ i+1 ].heading then -- Turned middle segments. source.x = 0 @@ -196,25 +197,27 @@ local function drawSnake() source.height = -8 end end + -- Notice that we set the origin to center { 4, 4 } that acts as pivot point. We also have to adjust our dest position by 4. RL.DrawTexturePro( snakeTexture, source, { seg.pos.x * TILE_SIZE + 4, seg.pos.y * TILE_SIZE + 4, 8, 8 }, { 4, 4 }, - angle, + angle * RL.RAD2DEG, RL.WHITE ) end -- Let's draw the head last to keep it on top. - local angle = math.deg( RL.Vector2Angle( { 0, 0 }, snake.heading ) ) + -- local angle = -math.atan2( snake.heading.x, snake.heading.y ) + RL.PI / 2 + local angle = -snake.heading:atan() + RL.PI / 2 RL.DrawTexturePro( snakeTexture, { 24, 0, 8, 8 }, { snake.headPos.x * TILE_SIZE + 4, snake.headPos.y * TILE_SIZE + 4, 8, 8 }, { 4, 4 }, - angle, + angle * RL.RAD2DEG, RL.WHITE ) end diff --git a/include/lua_core.h b/include/lua_core.h index 431fc28..6c81aaa 100644 --- a/include/lua_core.h +++ b/include/lua_core.h @@ -1,20 +1,5 @@ #pragma once -enum EventType { - EVENT_WINDOW_SIZE, - EVENT_WINDOW_MAXIMIZE, - EVENT_WINDOW_ICONYFY, - EVENT_WINDOW_FOCUS, - EVENT_WINDOW_DROP, - EVENT_KEY, - EVENT_CHAR, - EVENT_MOUSE_BUTTON, - EVENT_MOUSE_CURSOR_POS, - EVENT_MOUSE_SCROLL, - EVENT_CURSOR_ENTER, - EVENT_JOYSTICK -}; - enum BufferType { BUFFER_UNSIGNED_CHAR, BUFFER_UNSIGNED_SHORT, @@ -32,6 +17,13 @@ typedef struct { void *data; } Buffer; +/* Global assing functions. */ +void assignGlobalInt( int value, const char *name ); +void assignGlobalFloat( float value, const char *name ); +void assignGlobalDouble( double value, const char *name ); +void assignGlobalColor( Color color, const char *name ); +void assingGlobalFunction( const char *name, int ( *functionPtr )( lua_State* ) ); + bool luaInit( int argn, const char **argc ); int luaTraceback( lua_State *L ); bool luaCallMain(); diff --git a/include/main.h b/include/main.h index 4795121..fc711b8 100644 --- a/include/main.h +++ b/include/main.h @@ -13,8 +13,13 @@ #include #include "glad.h" -#include "glfw3.h" -#include "glfw3native.h" + +#ifdef PLATFORM_DESKTOP + #include "glfw3.h" + #include "glfw3native.h" +#elif PLATFORM_DESKTOP_SDL + #include +#endif #ifdef SHARED #include diff --git a/include/platforms/core_desktop.h b/include/platforms/core_desktop.h new file mode 100644 index 0000000..c3da090 --- /dev/null +++ b/include/platforms/core_desktop.h @@ -0,0 +1,16 @@ +#pragma once + +enum EventType { + GLFW_WINDOW_SIZE_EVENT, + GLFW_WINDOW_MAXIMIZE_EVENT, + GLFW_WINDOW_ICONYFY_EVENT, + GLFW_WINDOW_FOCUS_EVENT, + GLFW_WINDOW_DROP_EVENT, + GLFW_KEY_EVENT, + GLFW_CHAR_EVENT, + GLFW_MOUSE_BUTTON_EVENT, + GLFW_MOUSE_CURSOR_POS_EVENT, + GLFW_MOUSE_SCROLL_EVENT, + GLFW_CURSOR_ENTER_EVENT, + GLFW_JOYSTICK_EVENT +}; diff --git a/include/platforms/core_desktop_sdl.h b/include/platforms/core_desktop_sdl.h new file mode 100644 index 0000000..479b2a1 --- /dev/null +++ b/include/platforms/core_desktop_sdl.h @@ -0,0 +1,8 @@ +#pragma once + +// #include "SDL.h" + +enum EventType { + SDL_WINDOW_EVENT, + SDL_KEYBOARD_EVENT +}; diff --git a/include/state.h b/include/state.h index a777f40..4b2fe2c 100644 --- a/include/state.h +++ b/include/state.h @@ -13,7 +13,9 @@ typedef struct { Font defaultFont; Material defaultMaterial; int *RLGLcurrentShaderLocs; - /* Raylib GLFW input callback events. */ + /* Events. */ + /* GLFW events. */ +#ifdef PLATFORM_DESKTOP /* Window events. */ GLFWwindowsizefun raylibWindowSizeCallback; GLFWwindowmaximizefun raylibWindowMaximizeCallback; @@ -28,6 +30,9 @@ typedef struct { GLFWscrollfun raylibMouseScrollCallback; GLFWcursorenterfun raylibCursorEnterCallback; GLFWjoystickfun raylibJoystickCallback; +// #elif PLATFORM_DESKTOP_SDL + +#endif } State; extern State *state; diff --git a/src/core.c b/src/core.c index dc97f06..2f4b96b 100644 --- a/src/core.c +++ b/src/core.c @@ -1887,56 +1887,6 @@ int lcoreSetExitKey( lua_State *L ) { return 0; } -/* -> keyName = RL.GetKeyName( int key, int scancode ) - -This function returns the name of the specified printable key, encoded as UTF-8. -This is typically the character that key would produce without any modifier keys, -intended for displaying key bindings to the user. For dead keys, it is typically -the diacritic it would add to a character. - -Do not use this function for text input. You will break text input for many -languages even if it happens to work for yours. - -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; -} - /* ## Core - Input-related functions: gamepads */ diff --git a/src/lua_core.c b/src/lua_core.c index e289967..f0a81e1 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -14,6 +14,12 @@ #include "lgl.h" #include "reasings.h" +#ifdef PLATFORM_DESKTOP + #include "platforms/core_desktop.c" +#elif PLATFORM_DESKTOP_SDL + #include "platforms/core_desktop_sdl.c" +#endif + /* Define types. */ /* Buffer. */ @@ -282,31 +288,31 @@ static void defineModelAnimation() { /* Assing globals. */ -static void assignGlobalInt( int value, const char *name ) { +void assignGlobalInt( int value, const char *name ) { lua_State *L = state->luaState; lua_pushinteger( L, value ); lua_setfield( L, -2, name ); } -static void assignGlobalFloat( float value, const char *name ) { +void assignGlobalFloat( float value, const char *name ) { lua_State *L = state->luaState; lua_pushnumber( L, value ); lua_setfield( L, -2, name ); } -static void assignGlobalDouble( double value, const char *name ) { +void assignGlobalDouble( double value, const char *name ) { lua_State *L = state->luaState; lua_pushnumber( L, value ); lua_setfield( L, -2, name ); } -static void assignGlobalColor( Color color, const char *name ) { +void assignGlobalColor( Color color, const char *name ) { lua_State *L = state->luaState; uluaPushColor( L, color ); lua_setfield( L, -2, name ); } -static void assingGlobalFunction( const char *name, int ( *functionPtr )( lua_State* ) ) { +void assingGlobalFunction( const char *name, int ( *functionPtr )( lua_State* ) ) { lua_State *L = state->luaState; lua_pushcfunction( L, functionPtr ); lua_setfield( L, -2, name ); @@ -347,7 +353,6 @@ static void defineGlobals() { assignGlobalInt( LOG_FATAL, "LOG_FATAL" ); // Fatal logging, used to abort program: exit(EXIT_FAILURE) assignGlobalInt( LOG_NONE, "LOG_NONE" ); // Disable logging /* KeyboardKey */ - assignGlobalInt( GLFW_KEY_UNKNOWN, "KEY_UNKNOWN" ); // Key: Unknown assignGlobalInt( KEY_NULL, "KEY_NULL" ); // Key: NULL, used for no key pressed assignGlobalInt( KEY_APOSTROPHE, "KEY_APOSTROPHE" ); // Key: ' assignGlobalInt( KEY_COMMA, "KEY_COMMA" ); // Key: , @@ -890,12 +895,6 @@ static void defineGlobals() { assignGlobalInt( GL_STENCIL_BUFFER_BIT, "GL_STENCIL_BUFFER_BIT" ); assignGlobalInt( GL_NEAREST, "GL_NEAREST" ); assignGlobalInt( GL_LINEAR, "GL_LINEAR" ); - /* 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 /* CBuffer Data Types */ assignGlobalInt( BUFFER_UNSIGNED_CHAR, "BUFFER_UNSIGNED_CHAR" ); // C type unsigned char assignGlobalInt( BUFFER_UNSIGNED_SHORT, "BUFFER_UNSIGNED_SHORT" ); // C type unsigned short @@ -905,20 +904,6 @@ static void defineGlobals() { assignGlobalInt( BUFFER_INT, "BUFFER_INT" ); // C type int assignGlobalInt( BUFFER_FLOAT, "BUFFER_FLOAT" ); // C type float assignGlobalInt( BUFFER_DOUBLE, "BUFFER_DOUBLE" ); // C type double - /* Window Events. */ - assignGlobalInt( EVENT_WINDOW_SIZE, "EVENT_WINDOW_SIZE" ); // GLFW event window size changed - assignGlobalInt( EVENT_WINDOW_MAXIMIZE, "EVENT_WINDOW_MAXIMIZE" ); // GLFW event window maximize - assignGlobalInt( EVENT_WINDOW_ICONYFY, "EVENT_WINDOW_ICONYFY" ); // GLFW event window iconify - assignGlobalInt( EVENT_WINDOW_FOCUS, "EVENT_WINDOW_FOCUS" ); // GLFW event window focus - assignGlobalInt( EVENT_WINDOW_DROP, "EVENT_WINDOW_DROP" ); // GLFW event window drop - /* Input Events. */ - assignGlobalInt( EVENT_KEY, "EVENT_KEY" ); // GLFW event keyboard key - assignGlobalInt( EVENT_CHAR, "EVENT_CHAR" ); // GLFW event Unicode character - assignGlobalInt( EVENT_MOUSE_BUTTON, "EVENT_MOUSE_BUTTON" ); // GLFW event mouse button - assignGlobalInt( EVENT_MOUSE_CURSOR_POS, "EVENT_MOUSE_CURSOR_POS" ); // GLFW event cursor position - assignGlobalInt( EVENT_MOUSE_SCROLL, "EVENT_MOUSE_SCROLL" ); // GLFW event mouse scroll - assignGlobalInt( EVENT_CURSOR_ENTER, "EVENT_CURSOR_ENTER" ); // GLFW event cursor enter/leave - assignGlobalInt( EVENT_JOYSTICK, "EVENT_JOYSTICK" ); // GLFW event joystick /*DOC_END*/ lua_pop( L, -1 ); @@ -969,366 +954,6 @@ static void logCustom( int logLevel, const char *text, va_list args ) { } } -/* Window events. */ - -static void 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, EVENT_WINDOW_SIZE ); - 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 ) - -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, EVENT_WINDOW_MAXIMIZE ); - 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 - -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, EVENT_WINDOW_ICONYFY ); - 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 ); -} - -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, EVENT_WINDOW_FOCUS ); - 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 ); -} - -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, EVENT_WINDOW_DROP ); - 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. */ - -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, EVENT_KEY ); - 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 ); -} - -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, EVENT_CHAR ); - 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 ); -} - -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, EVENT_MOUSE_BUTTON ); - 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 ); -} - -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, EVENT_MOUSE_CURSOR_POS ); - 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 ); -} - -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, EVENT_MOUSE_SCROLL ); - 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 ); -} - -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, EVENT_CURSOR_ENTER ); - 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 ); -} - -static void joystickInputEvent( 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, EVENT_JOYSTICK ); - 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 ); -} - bool luaInit( int argn, const char **argc ) { state->luaState = luaL_newstate(); lua_State *L = state->luaState; @@ -1358,6 +983,11 @@ bool luaInit( int argn, const char **argc ) { defineModelAnimation(); /* Define globals. */ defineGlobals(); + definePlatformGlobals(); + + /* Register functions. */ + luaRegister(); + luaPlatformRegister(); /* Set arguments. */ lua_getglobal( L, "RL" ); @@ -1402,16 +1032,16 @@ bool luaCallMain() { /* If web, set path to resources folder. */ #ifdef EMSCRIPTEN - sprintf( path, "resources/main.lua" ); + snprintf( path, STRING_LEN, "resources/main.lua" ); /* Alternatively look for main. Could be precompiled binary file. */ if ( !FileExists( path ) ) { - sprintf( path, "resources/main" ); + snprintf( path, STRING_LEN, "resources/main" ); } #else - sprintf( path, "%smain.lua", state->exePath ); + snprintf( path, STRING_LEN, "%smain.lua", state->exePath ); /* Alternatively look for main. Could be precompiled binary file. */ if ( !FileExists( path ) ) { - sprintf( path, "%smain", state->exePath ); + snprintf( path, STRING_LEN, "%smain", state->exePath ); } #endif luaL_dofile( L, path ); @@ -1426,23 +1056,7 @@ bool luaCallMain() { /* Apply custom callback here. */ SetTraceLogCallback( logCustom ); - /* Window events. */ - state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), 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( joystickInputEvent ); + platformRegisterEvents(); lua_getglobal( L, "RL" ); lua_getfield( L, -1, "init" ); @@ -1667,8 +1281,6 @@ void luaRegister() { assingGlobalFunction( "GetKeyPressed", lcoreGetKeyPressed ); assingGlobalFunction( "GetCharPressed", lcoreGetCharPressed ); assingGlobalFunction( "SetExitKey", lcoreSetExitKey ); - assingGlobalFunction( "GetKeyName", lcoreGetKeyName ); - assingGlobalFunction( "GetKeyScancode", lcoreGetKeyScancode ); /* Input-related functions: gamepads. */ assingGlobalFunction( "IsGamepadAvailable", lcoreIsGamepadAvailable ); assingGlobalFunction( "GetGamepadName", lcoreGetGamepadName ); diff --git a/src/main.c b/src/main.c index fc64e86..2dd7618 100644 --- a/src/main.c +++ b/src/main.c @@ -64,7 +64,7 @@ int main( int argn, const char **argc ) { else { printVersion(); stateInit( argn, argc, exePath ); - luaRegister(); + // luaRegister(); state->run = luaCallMain(); while ( state->run ) { diff --git a/src/platforms/core_desktop.c b/src/platforms/core_desktop.c new file mode 100644 index 0000000..ca6b168 --- /dev/null +++ b/src/platforms/core_desktop.c @@ -0,0 +1,484 @@ +#include "main.h" +#include "lua_core.h" +#include "core.h" +#include "platforms/core_desktop.h" + +static void definePlatformGlobals() { + lua_State *L = state->luaState; + + lua_getglobal( L, "RL" ); + + /* KeyboardKey */ + 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 + /* 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 + /* 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 + + lua_pop( L, -1 ); +} + +/* Functions. */ + +/* +## 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. +This is typically the character that key would produce without any modifier keys, +intended for displaying key bindings to the user. For dead keys, it is typically +the diacritic it would add to a character. + +Do not use this function for text input. You will break text input for many +languages even if it happens to work for yours. + +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; +} + +static 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 ); +} + +/* Events. */ + +/* Window events. */ + +static void 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 ) + +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 + +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 ); +} + +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 ); +} + +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. */ + +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 ); +} + +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 ); +} + +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 ); +} + +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 ); +} + +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 ); +} + +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 ); +} + +static void joystickInputEvent( 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 ); +} + +void platformRegisterEvents() { + /* Window events. */ + state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), 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( joystickInputEvent ); +} diff --git a/src/platforms/core_desktop_sdl.c b/src/platforms/core_desktop_sdl.c new file mode 100644 index 0000000..2649eac --- /dev/null +++ b/src/platforms/core_desktop_sdl.c @@ -0,0 +1,127 @@ +#include "main.h" +#include "lua_core.h" +#include "core.h" +#include "platforms/core_desktop_sdl.h" + +static void definePlatformGlobals() { + lua_State *L = state->luaState; + + lua_getglobal( L, "RL" ); + + /* KeyboardKey */ + assignGlobalInt( SDL_KEYDOWN, "SDL_KEYDOWN" ); // key pressed + assignGlobalInt( SDL_KEYUP, "SDL_KEYUP" ); // key released + assignGlobalInt( SDL_PRESSED, "SDL_PRESSED" ); // key pressed + assignGlobalInt( SDL_RELEASED, "SDL_RELEASED" ); // key released + /* Keyboard Events. */ + assignGlobalInt( SDL_WINDOW_EVENT, "SDL_WINDOW_EVENT" ); // SDL Window Event + assignGlobalInt( SDL_KEYBOARD_EVENT, "SDL_KEYBOARD_EVENT" ); // SDL Keyboard Event + + lua_pop( L, -1 ); +} + +/* Functions. */ + +/* +## Core - Input-related functions: keyboard. +*/ + +/* +> keyName = RL.GetKeyName( int key ) + +Use this function to get a human-readable name for a key. If the key doesn't have a name, this function returns an empty string (""). + +- Success return string +*/ +int lcoreGetKeyName( lua_State *L ) { + int key = luaL_checkinteger( L, 1 ); + + lua_pushstring( L, SDL_GetKeyName( key ) ); + + return 1; +} + +/* +> keyName = RL.GetScancodeFromKey( int key ) + +Use this function to get the scancode corresponding to the given key code according to the current keyboard layout. + +- Success return int +*/ +int lcoreGetScancodeFromKey( lua_State *L ) { + int key = luaL_checkinteger( L, 1 ); + + lua_pushinteger( L, SDL_GetScancodeFromKey( key ) ); + + return 1; +} + +static void luaPlatformRegister() { + lua_State *L = state->luaState; + lua_getglobal( L, "RL" ); + + /* Input-related functions: keyboard. */ + assingGlobalFunction( "GetKeyName", lcoreGetKeyName ); + assingGlobalFunction( "GetScancodeFromKey", lcoreGetScancodeFromKey ); + + lua_pop( L, -1 ); +} + +/* Events. */ + +//TODO Thinking of different implementation since this could run on different thread than Lua. +static int SDLEventFilter( void *userdata, SDL_Event *event ) { + /* Don't handle events if exiting. Prevent segfault. */ + if ( event->type == SDL_QUIT || !state->run ) { + return 0; + } + 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 ) ) { + return 0; + } + + switch ( event->type ) { + case SDL_KEYDOWN: + case SDL_KEYUP: + { + lua_createtable( L, 7, 0 ); + lua_pushinteger( L, SDL_KEYBOARD_EVENT ); + lua_setfield( L, -2, "type" ); + lua_pushinteger( L, event->key.timestamp ); + lua_setfield( L, -2, "timestamp" ); + lua_pushinteger( L, event->key.state ); + lua_setfield( L, -2, "state" ); + lua_pushinteger( L, event->key.repeat ); + lua_setfield( L, -2, "repeat" ); + lua_pushinteger( L, event->key.keysym.scancode ); + lua_setfield( L, -2, "scancode" ); + lua_pushinteger( L, event->key.keysym.sym ); + lua_setfield( L, -2, "sym" ); + lua_pushinteger( L, event->key.keysym.mod ); + lua_setfield( L, -2, "mod" ); + + 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 ); + } + break; + default: + break; + } +} + +static void platformRegisterEvents() { + /* Probably sould not use this since. SDL Warning: + Be very careful of what you do in the event filter function, as it may run in a different thread! + There has already being some undefined behavior with Lua! :o */ + // SDL_AddEventWatch( SDLEventFilter, NULL ); +} diff --git a/src/rmath.c b/src/rmath.c index 8dd9905..23a93bf 100644 --- a/src/rmath.c +++ b/src/rmath.c @@ -293,7 +293,8 @@ int lmathVector2DistanceSqr( lua_State *L ) { /* > result = RL.Vector2Angle( Vector2 v1, Vector2 v2 ) -Calculate angle from two vectors +Calculate angle between two vectors +NOTE: Angle is calculated from origin point (0, 0) - Success return float */ @@ -309,9 +310,9 @@ int lmathVector2Angle( lua_State *L ) { /* > result = RL.Vector2LineAngle( Vector2 a, Vector2 b ) -Calculate angle defined by a two vectors line. -NOTE: Parameters need to be normalized. -Current implementation should be aligned with glm::angle. +Calculate angle defined by a two vectors line +NOTE: Parameters need to be normalized +Current implementation should be aligned with glm::angle - Success return float */ -- cgit v1.2.3