Support for different platforms. Platform_desktop_sdl.

This commit is contained in:
jussi
2023-11-23 00:00:49 +02:00
parent 85a48cf093
commit 1ab9722875
20 changed files with 775 additions and 733 deletions

152
API.md
View File

@@ -482,13 +482,6 @@ Disable logging
## Globals - KeyboardKey ## Globals - KeyboardKey
GLFW_KEY_UNKNOWN = nil
Key: Unknown
---
> KEY_NULL = 0 > KEY_NULL = 0
Key: NULL, used for no key pressed 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 ## Globals - CBuffer
> BUFFER_UNSIGNED_CHAR = 0 > 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 ## 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 ## Core - Input-related functions: gamepads
--- ---
@@ -7546,7 +7403,8 @@ Calculate square distance between two vectors
> result = RL.Vector2Angle( Vector2 v1, Vector2 v2 ) > 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 - Success return float
@@ -7554,9 +7412,9 @@ Calculate angle from two vectors
> result = RL.Vector2LineAngle( Vector2 a, Vector2 b ) > result = RL.Vector2LineAngle( Vector2 a, Vector2 b )
Calculate angle defined by a two vectors line. Calculate angle defined by a two vectors line
NOTE: Parameters need to be normalized. NOTE: Parameters need to be normalized
Current implementation should be aligned with glm::angle. Current implementation should be aligned with glm::angle
- Success return float - Success return float

View File

@@ -1,3 +1,7 @@
set( CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake )
include( CMakeDependentOption )
include( EnumOption )
cmake_minimum_required( VERSION 3.9 ) cmake_minimum_required( VERSION 3.9 )
project( ReiLua ) project( ReiLua )
@@ -9,6 +13,8 @@ set( CMAKE_C_STANDARD 99 ) # Requires C99 standard
option( SHARED "Build using dynamic libraries." off ) option( SHARED "Build using dynamic libraries." off )
option( LUAJIT "Use LuaJIT." 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 ) if( NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE ) 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" ) set_property( CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo" )
@@ -19,6 +25,12 @@ file( GLOB SOURCES src/*.c )
include_directories( include ) include_directories( include )
add_executable( ${PROJECT_NAME} ${SOURCES} ) 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 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/libraylib.a )
target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/liblua.a ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/web/liblua.a )
@@ -32,7 +44,7 @@ else() # Desktop
if( SHARED ) if( SHARED )
message( Shared ) message( Shared )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSHARED" ) 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 ) target_link_libraries( ${PROJECT_NAME} raylib )
if( LUAJIT ) if( LUAJIT )
@@ -48,14 +60,20 @@ else() # Desktop
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLUAJIT" ) set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLUAJIT" )
target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libluajit.a ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/libluajit.a )
else() else()
# target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/liblua.a ) target_link_libraries( ${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/liblua.a )
target_link_libraries( ${PROJECT_NAME} lua )
endif() endif()
endif() endif()
if( UNIX ) if( UNIX )
set( CMAKE_C_COMPILER "gcc" ) 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 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 rt bcm_host m dl pthread )
# target_link_libraries( ${PROJECT_NAME} GLESv2 EGL drm gbm pthread rt m dl ) # 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 Cocoa" )
target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" ) target_link_libraries( ${PROJECT_NAME} "-framework OpenGL" )
elseif( WIN32 ) 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. # Remove this to get console.
set( CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-mwindows" ) set( CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-mwindows" )
target_link_libraries( ${PROJECT_NAME} mingw32 opengl32 gdi32 winmm ) target_link_libraries( ${PROJECT_NAME} mingw32 opengl32 gdi32 winmm )

View File

@@ -75,8 +75,6 @@ RL.LOG_NONE=7
-- Globals - KeyboardKey -- Globals - KeyboardKey
---Key: Unknown
RL.GLFW_KEY_UNKNOWN=nil
---Key: NULL, used for no key pressed ---Key: NULL, used for no key pressed
RL.KEY_NULL=0 RL.KEY_NULL=0
---Key: ' ---Key: '
@@ -1166,19 +1164,6 @@ RL.GL_STENCIL_BUFFER_BIT=1024
RL.GL_NEAREST=9728 RL.GL_NEAREST=9728
RL.GL_LINEAR=9729 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 -- Globals - CBuffer
---C type unsigned char ---C type unsigned char
@@ -1197,36 +1182,6 @@ RL.BUFFER_INT=5
RL.BUFFER_FLOAT=6 RL.BUFFER_FLOAT=6
---C type double ---C type double
RL.BUFFER_DOUBLE=7 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 -- Core - Window-related functions
---Close window and unload OpenGL context and free all resources ---Close window and unload OpenGL context and free all resources
@@ -1943,29 +1898,6 @@ function RL.GetCharPressed() end
---@return any RL.SetExitKey ---@return any RL.SetExitKey
function RL.SetExitKey( key ) end 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 -- Core - Input-related functions: gamepads
---Detect if a gamepad is available ---Detect if a gamepad is available
@@ -4799,16 +4731,17 @@ function RL.Vector2Distance( v1, v2 ) end
---@return any result ---@return any result
function RL.Vector2DistanceSqr( v1, v2 ) end 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 ---- Success return float
---@param v1 table ---@param v1 table
---@param v2 table ---@param v2 table
---@return any result ---@return any result
function RL.Vector2Angle( v1, v2 ) end function RL.Vector2Angle( v1, v2 ) end
---Calculate angle defined by a two vectors line. ---Calculate angle defined by a two vectors line
---NOTE: Parameters need to be normalized. ---NOTE: Parameters need to be normalized
---Current implementation should be aligned with glm::angle. ---Current implementation should be aligned with glm::angle
---- Success return float ---- Success return float
---@param a table ---@param a table
---@param b table ---@param b table

View File

@@ -9,6 +9,9 @@ KEY CHANGES:
- ADDED: More Window-related functions. - ADDED: More Window-related functions.
- ADDED: Spline functions. - ADDED: Spline functions.
- CHANGE: Raygui to version 4.0. Gui libs also updated. - 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: DETAILED CHANGES:
- REMOVED: DrawLineBezierQuad, DrawLineBezierCubic. - REMOVED: DrawLineBezierQuad, DrawLineBezierCubic.

9
cmake/EnumOption.cmake Normal file
View File

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

View File

@@ -2,8 +2,10 @@ Current {
} }
Backlog { Backlog {
* SDL_Desktop platform option. Might have to move events to platform specific file since events are not * Platform specific API documentation.
the same in different platforms. * Platform desktop SDL.
* Thread safe Lua RL.event calling.
* Haptic functions.
* Text * Text
* Text codepoints management functions (unicode characters)? Could be usefull for luajit. * 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. * Some of the Text strings management functions could be easier to use than the Lua ones.

View File

@@ -9,27 +9,27 @@ function RL.init()
end end
local function getEventType( event ) local function getEventType( event )
if event.type == RL.EVENT_WINDOW_SIZE then if event.type == RL.GLFW_WINDOW_SIZE_EVENT then
return "Window Size" return "Window Size"
elseif event.type == RL.EVENT_WINDOW_MAXIMIZE then elseif event.type == RL.GLFW_WINDOW_MAXIMIZE_EVENT then
return "Window Maximized" return "Window Maximized"
elseif event.type == RL.EVENT_WINDOW_ICONYFY then elseif event.type == RL.GLFW_WINDOW_ICONYFY_EVENT then
return "Window Iconyfy" return "Window Iconyfy"
elseif event.type == RL.EVENT_WINDOW_FOCUS then elseif event.type == RL.GLFW_WINDOW_FOCUS_EVENT then
return "Window Focus" return "Window Focus"
elseif event.type == RL.EVENT_WINDOW_DROP then elseif event.type == RL.GLFW_WINDOW_DROP_EVENT then
return "Window Drop" return "Window Drop"
elseif event.type == RL.EVENT_KEY then elseif event.type == RL.GLFW_KEY_EVENT then
return "Key" return "Key"
elseif event.type == RL.EVENT_CHAR then elseif event.type == RL.GLFW_CHAR_EVENT then
return "Char" return "Char"
elseif event.type == RL.EVENT_MOUSE_BUTTON then elseif event.type == RL.GLFW_MOUSE_BUTTON_EVENT then
return "Mouse Button" 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" return "Mouse Cursor Position"
elseif event.type == RL.EVENT_MOUSE_SCROLL then elseif event.type == RL.GLFW_MOUSE_SCROLL_EVENT then
return "Mouse Scroll" return "Mouse Scroll"
elseif event.type == RL.EVENT_CURSOR_ENTER then elseif event.type == RL.GLFW_CURSOR_ENTER_EVENT then
return "Cursor Enter" return "Cursor Enter"
elseif event.type == RL.EVENT_JOYSTICK then elseif event.type == RL.EVENT_JOYSTICK then
return "Joystick" return "Joystick"
@@ -63,33 +63,33 @@ end
function RL.event( event ) function RL.event( event )
text = "Event: "..getEventType( event ).."\n" 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 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 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 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 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" text = text.."count: "..event.count.."\n"
for _, path in ipairs( event.paths ) do for _, path in ipairs( event.paths ) do
text = text..path.."\n" text = text..path.."\n"
end 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.."key: "..event.key.." scancode: "..event.scancode.." action: "..getAction( event.action ).." mods: "..event.mods
text = text .."\nkeyName: "..keyName( event.key ) 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.."key: "..event.key
-- text = text .."\nchar: "..string.char( event.key ) -- text = text .."\nchar: "..string.char( event.key )
text = text .."\nchar: "..utf8.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 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 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 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 text = text.."enter: "..event.enter
cursorIn = event.enter cursorIn = event.enter
elseif event.type == RL.EVENT_JOYSTICK then elseif event.type == RL.EVENT_JOYSTICK then
@@ -99,6 +99,8 @@ function RL.event( event )
elseif event.event == RL.GLFW_DISCONNECTED then elseif event.event == RL.GLFW_DISCONNECTED then
text = text.."\nDisconnected" text = text.."\nDisconnected"
end end
elseif event.type == RL.SDL_KEYBOARD_EVENT then
text = text.."state: "..event.state
end end
end end

View File

@@ -126,6 +126,14 @@ function Vector2:angle( v2 )
return RL.Vector2Angle( self, v2 ) return RL.Vector2Angle( self, v2 )
end 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 ) function Vector2:scale( scale )
return Vector2:new( RL.Vector2Scale( self, scale ) ) return Vector2:new( RL.Vector2Scale( self, scale ) )
end end

View File

@@ -1,5 +1,6 @@
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Util = require( "utillib" )
Vec2 = require( "vector2" ) Vec2 = require( "vector2" )
Rect = require( "rectangle" ) Rect = require( "rectangle" )
@@ -174,14 +175,14 @@ end
local function drawSnake() local function drawSnake()
for i, seg in ipairs( snake.segments ) do 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 ) local source = Rect:new( 16, 0, 8, 8 )
if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment. if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment.
source.x = 8 source.x = 8
if 1 < #snake.segments then 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 end
elseif i < #snake.segments and seg.heading ~= snake.segments[ i+1 ].heading then -- Turned middle segments. elseif i < #snake.segments and seg.heading ~= snake.segments[ i+1 ].heading then -- Turned middle segments.
source.x = 0 source.x = 0
@@ -196,25 +197,27 @@ local function drawSnake()
source.height = -8 source.height = -8
end end
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. -- 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( RL.DrawTexturePro(
snakeTexture, snakeTexture,
source, source,
{ seg.pos.x * TILE_SIZE + 4, seg.pos.y * TILE_SIZE + 4, 8, 8 }, { seg.pos.x * TILE_SIZE + 4, seg.pos.y * TILE_SIZE + 4, 8, 8 },
{ 4, 4 }, { 4, 4 },
angle, angle * RL.RAD2DEG,
RL.WHITE RL.WHITE
) )
end end
-- Let's draw the head last to keep it on top. -- 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( RL.DrawTexturePro(
snakeTexture, snakeTexture,
{ 24, 0, 8, 8 }, { 24, 0, 8, 8 },
{ snake.headPos.x * TILE_SIZE + 4, { snake.headPos.x * TILE_SIZE + 4,
snake.headPos.y * TILE_SIZE + 4, 8, 8 }, snake.headPos.y * TILE_SIZE + 4, 8, 8 },
{ 4, 4 }, { 4, 4 },
angle, angle * RL.RAD2DEG,
RL.WHITE RL.WHITE
) )
end end

View File

@@ -1,20 +1,5 @@
#pragma once #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 { enum BufferType {
BUFFER_UNSIGNED_CHAR, BUFFER_UNSIGNED_CHAR,
BUFFER_UNSIGNED_SHORT, BUFFER_UNSIGNED_SHORT,
@@ -32,6 +17,13 @@ typedef struct {
void *data; void *data;
} Buffer; } 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 ); bool luaInit( int argn, const char **argc );
int luaTraceback( lua_State *L ); int luaTraceback( lua_State *L );
bool luaCallMain(); bool luaCallMain();

View File

@@ -13,8 +13,13 @@
#include <stdint.h> #include <stdint.h>
#include "glad.h" #include "glad.h"
#include "glfw3.h"
#include "glfw3native.h" #ifdef PLATFORM_DESKTOP
#include "glfw3.h"
#include "glfw3native.h"
#elif PLATFORM_DESKTOP_SDL
#include <SDL.h>
#endif
#ifdef SHARED #ifdef SHARED
#include <raylib.h> #include <raylib.h>

View File

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

View File

@@ -0,0 +1,8 @@
#pragma once
// #include "SDL.h"
enum EventType {
SDL_WINDOW_EVENT,
SDL_KEYBOARD_EVENT
};

View File

@@ -13,7 +13,9 @@ typedef struct {
Font defaultFont; Font defaultFont;
Material defaultMaterial; Material defaultMaterial;
int *RLGLcurrentShaderLocs; int *RLGLcurrentShaderLocs;
/* Raylib GLFW input callback events. */ /* Events. */
/* GLFW events. */
#ifdef PLATFORM_DESKTOP
/* Window events. */ /* Window events. */
GLFWwindowsizefun raylibWindowSizeCallback; GLFWwindowsizefun raylibWindowSizeCallback;
GLFWwindowmaximizefun raylibWindowMaximizeCallback; GLFWwindowmaximizefun raylibWindowMaximizeCallback;
@@ -28,6 +30,9 @@ typedef struct {
GLFWscrollfun raylibMouseScrollCallback; GLFWscrollfun raylibMouseScrollCallback;
GLFWcursorenterfun raylibCursorEnterCallback; GLFWcursorenterfun raylibCursorEnterCallback;
GLFWjoystickfun raylibJoystickCallback; GLFWjoystickfun raylibJoystickCallback;
// #elif PLATFORM_DESKTOP_SDL
#endif
} State; } State;
extern State *state; extern State *state;

View File

@@ -1887,56 +1887,6 @@ int lcoreSetExitKey( lua_State *L ) {
return 0; 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 ## Core - Input-related functions: gamepads
*/ */

View File

@@ -14,6 +14,12 @@
#include "lgl.h" #include "lgl.h"
#include "reasings.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. */ /* Define types. */
/* Buffer. */ /* Buffer. */
@@ -282,31 +288,31 @@ static void defineModelAnimation() {
/* Assing globals. */ /* Assing globals. */
static void assignGlobalInt( int value, const char *name ) { void assignGlobalInt( int value, const char *name ) {
lua_State *L = state->luaState; lua_State *L = state->luaState;
lua_pushinteger( L, value ); lua_pushinteger( L, value );
lua_setfield( L, -2, name ); 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_State *L = state->luaState;
lua_pushnumber( L, value ); lua_pushnumber( L, value );
lua_setfield( L, -2, name ); 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_State *L = state->luaState;
lua_pushnumber( L, value ); lua_pushnumber( L, value );
lua_setfield( L, -2, name ); 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; lua_State *L = state->luaState;
uluaPushColor( L, color ); uluaPushColor( L, color );
lua_setfield( L, -2, name ); 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_State *L = state->luaState;
lua_pushcfunction( L, functionPtr ); lua_pushcfunction( L, functionPtr );
lua_setfield( L, -2, name ); 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_FATAL, "LOG_FATAL" ); // Fatal logging, used to abort program: exit(EXIT_FAILURE)
assignGlobalInt( LOG_NONE, "LOG_NONE" ); // Disable logging assignGlobalInt( LOG_NONE, "LOG_NONE" ); // Disable logging
/* KeyboardKey */ /* KeyboardKey */
assignGlobalInt( GLFW_KEY_UNKNOWN, "KEY_UNKNOWN" ); // Key: Unknown
assignGlobalInt( KEY_NULL, "KEY_NULL" ); // Key: NULL, used for no key pressed assignGlobalInt( KEY_NULL, "KEY_NULL" ); // Key: NULL, used for no key pressed
assignGlobalInt( KEY_APOSTROPHE, "KEY_APOSTROPHE" ); // Key: ' assignGlobalInt( KEY_APOSTROPHE, "KEY_APOSTROPHE" ); // Key: '
assignGlobalInt( KEY_COMMA, "KEY_COMMA" ); // 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_STENCIL_BUFFER_BIT, "GL_STENCIL_BUFFER_BIT" );
assignGlobalInt( GL_NEAREST, "GL_NEAREST" ); assignGlobalInt( GL_NEAREST, "GL_NEAREST" );
assignGlobalInt( GL_LINEAR, "GL_LINEAR" ); 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 */ /* CBuffer Data Types */
assignGlobalInt( BUFFER_UNSIGNED_CHAR, "BUFFER_UNSIGNED_CHAR" ); // C type unsigned char assignGlobalInt( BUFFER_UNSIGNED_CHAR, "BUFFER_UNSIGNED_CHAR" ); // C type unsigned char
assignGlobalInt( BUFFER_UNSIGNED_SHORT, "BUFFER_UNSIGNED_SHORT" ); // C type unsigned short 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_INT, "BUFFER_INT" ); // C type int
assignGlobalInt( BUFFER_FLOAT, "BUFFER_FLOAT" ); // C type float assignGlobalInt( BUFFER_FLOAT, "BUFFER_FLOAT" ); // C type float
assignGlobalInt( BUFFER_DOUBLE, "BUFFER_DOUBLE" ); // C type double 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*/ /*DOC_END*/
lua_pop( L, -1 ); 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 ) { bool luaInit( int argn, const char **argc ) {
state->luaState = luaL_newstate(); state->luaState = luaL_newstate();
lua_State *L = state->luaState; lua_State *L = state->luaState;
@@ -1358,6 +983,11 @@ bool luaInit( int argn, const char **argc ) {
defineModelAnimation(); defineModelAnimation();
/* Define globals. */ /* Define globals. */
defineGlobals(); defineGlobals();
definePlatformGlobals();
/* Register functions. */
luaRegister();
luaPlatformRegister();
/* Set arguments. */ /* Set arguments. */
lua_getglobal( L, "RL" ); lua_getglobal( L, "RL" );
@@ -1402,16 +1032,16 @@ bool luaCallMain() {
/* If web, set path to resources folder. */ /* If web, set path to resources folder. */
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
sprintf( path, "resources/main.lua" ); snprintf( path, STRING_LEN, "resources/main.lua" );
/* Alternatively look for main. Could be precompiled binary file. */ /* Alternatively look for main. Could be precompiled binary file. */
if ( !FileExists( path ) ) { if ( !FileExists( path ) ) {
sprintf( path, "resources/main" ); snprintf( path, STRING_LEN, "resources/main" );
} }
#else #else
sprintf( path, "%smain.lua", state->exePath ); snprintf( path, STRING_LEN, "%smain.lua", state->exePath );
/* Alternatively look for main. Could be precompiled binary file. */ /* Alternatively look for main. Could be precompiled binary file. */
if ( !FileExists( path ) ) { if ( !FileExists( path ) ) {
sprintf( path, "%smain", state->exePath ); snprintf( path, STRING_LEN, "%smain", state->exePath );
} }
#endif #endif
luaL_dofile( L, path ); luaL_dofile( L, path );
@@ -1426,23 +1056,7 @@ bool luaCallMain() {
/* Apply custom callback here. */ /* Apply custom callback here. */
SetTraceLogCallback( logCustom ); SetTraceLogCallback( logCustom );
/* Window events. */ platformRegisterEvents();
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 );
lua_getglobal( L, "RL" ); lua_getglobal( L, "RL" );
lua_getfield( L, -1, "init" ); lua_getfield( L, -1, "init" );
@@ -1667,8 +1281,6 @@ void luaRegister() {
assingGlobalFunction( "GetKeyPressed", lcoreGetKeyPressed ); assingGlobalFunction( "GetKeyPressed", lcoreGetKeyPressed );
assingGlobalFunction( "GetCharPressed", lcoreGetCharPressed ); assingGlobalFunction( "GetCharPressed", lcoreGetCharPressed );
assingGlobalFunction( "SetExitKey", lcoreSetExitKey ); assingGlobalFunction( "SetExitKey", lcoreSetExitKey );
assingGlobalFunction( "GetKeyName", lcoreGetKeyName );
assingGlobalFunction( "GetKeyScancode", lcoreGetKeyScancode );
/* Input-related functions: gamepads. */ /* Input-related functions: gamepads. */
assingGlobalFunction( "IsGamepadAvailable", lcoreIsGamepadAvailable ); assingGlobalFunction( "IsGamepadAvailable", lcoreIsGamepadAvailable );
assingGlobalFunction( "GetGamepadName", lcoreGetGamepadName ); assingGlobalFunction( "GetGamepadName", lcoreGetGamepadName );

View File

@@ -64,7 +64,7 @@ int main( int argn, const char **argc ) {
else { else {
printVersion(); printVersion();
stateInit( argn, argc, exePath ); stateInit( argn, argc, exePath );
luaRegister(); // luaRegister();
state->run = luaCallMain(); state->run = luaCallMain();
while ( state->run ) { while ( state->run ) {

View File

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

View File

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

View File

@@ -293,7 +293,8 @@ int lmathVector2DistanceSqr( lua_State *L ) {
/* /*
> result = RL.Vector2Angle( Vector2 v1, Vector2 v2 ) > 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 - Success return float
*/ */
@@ -309,9 +310,9 @@ int lmathVector2Angle( lua_State *L ) {
/* /*
> result = RL.Vector2LineAngle( Vector2 a, Vector2 b ) > result = RL.Vector2LineAngle( Vector2 a, Vector2 b )
Calculate angle defined by a two vectors line. Calculate angle defined by a two vectors line
NOTE: Parameters need to be normalized. NOTE: Parameters need to be normalized
Current implementation should be aligned with glm::angle. Current implementation should be aligned with glm::angle
- Success return float - Success return float
*/ */