summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/events/main.lua46
-rw-r--r--examples/resources/lib/vector2.lua8
-rw-r--r--examples/snake/main.lua13
3 files changed, 40 insertions, 27 deletions
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