Reintroducing Unload functions. Is*Ready functions. GC_UNLOAD setting and check function.

This commit is contained in:
jussi
2023-10-30 22:40:20 +02:00
parent 6e0d577d63
commit c3295e014d
37 changed files with 853 additions and 646 deletions

View File

@@ -1,160 +0,0 @@
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
util = require( "utillib" )
Vec2 = require( "vector2" )
Rect = require( "rectangle" )
Color = require( "color" )
Gui = require( "gui" )
local container = {}
-- local circleTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/circle.png" )
local circleTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/plain-circle.png" )
local checkTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/check-mark.png" )
local borderTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/ui_border.png" )
local textInput
RL.GenTextureMipmaps( circleTexture )
RL.GenTextureMipmaps( checkTexture )
RL.SetTextureFilter( circleTexture, RL.TEXTURE_FILTER_TRILINEAR )
RL.SetTextureFilter( checkTexture, RL.TEXTURE_FILTER_TRILINEAR )
function initGui()
-- local label = Gui.label:new( { text = "Dog", bounds = { 32, 32, 96, 96 }, drawBounds = true, Haling = Gui.ALING.CENTER, Valing = Gui.ALING.TOP } )
local panel = Gui.element:new( { bounds = Rect:new( 60, 32, 128, 128 ), drawBounds = true } )
container = Gui.container:new( {
bounds = Rect:new( 256, 120, 128, 128 ),
-- spacing = 14,
-- drawScrollRect = true,
-- HAling = Gui.ALING.RIGHT,
-- HAling = Gui.ALING.CENTER,
-- type = Gui.CONTAINER.HORIZONTAL,
-- VAling = Gui.ALING.CENTER,
-- type = Gui.CONTAINER.GRID,
-- columns = 2,
-- rows = 2,
scrollable = true,
showScrollbar = true,
} )
-- local container = Gui.container:new( { bounds = Rect:new( 256, 120, 128, 128 ), drawBounds = true, type = Gui.CONTAINER.HORIZONTAL } )
-- local itemBounds = { 0, 0, container.bounds.width - container.spacing * 2, 36 }
local itemBounds = Rect:new( 0, 0, 64, 36 )
local dog = Gui.element:new( {
bounds = Rect:new( 0, 0, 128, 36 ),
onClicked = function() panel:setPosition( Vec2:new( 290, 120 ) ) end,
onMouseOver = function( self ) self.items[1].color = RL.RED end,
notMouseOver = function( self ) self.items[1].color = RL.BLACK end,
drawBounds = true,
} )
dog:add( Gui.text:new( { text = "Dog", HAling = Gui.ALING.LEFT } ) )
dog:add( Gui.texture:new( {
bounds = dog.bounds:clone(),
texture = borderTexture,
HAling = Gui.ALING.CENTER,
VAling = Gui.ALING.CENTER,
visible = true,
nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = RL.NPATCH_NINE_PATCH },
} ) )
dog:add( Gui.texture:new( { bounds = Rect:new( 0, 0, 24, 24 ), texture = circleTexture, HAling = Gui.ALING.RIGHT, color = Color:new( 150, 150, 255 ) } ) )
dog:add( Gui.texture:new( { bounds = Rect:new( 0, 0, 24, 24 ), texture = checkTexture, HAling = Gui.ALING.RIGHT, visible = true } ) )
-- dog:add( Gui.text:new( { text = "Cat", HAling = Gui.ALING.RIGHT } ) )
-- dog:add( Gui.shape:new( { bounds = Rect:new( 0, 0, 128, 36 ), shape = Gui.SHAPE.RECTANGLE_ROUNDED, color = Color:new( GREEN ) } ) )
container:add( dog )
-- container:add( Gui.element:new( {
-- text = "Cat",
-- bounds = Rect:new( 0, 0, 78, 24 ),
-- onClicked = function() panel:setPosition( Vec2:new( 290, 120 ) ) end,
-- onMouseOver = function( self ) self.color = RED end,
-- notMouseOver = function( self ) self.color = BLACK end,
-- drawBounds = true,
-- } ) )
for i = 1, 5 do
local element = Gui.element:new( {
bounds = Rect:new( 0, 0, 120, 30 ),
onClicked = function() panel:setPosition( Vec2:new( 340, 380 ) ) end,
onMouseOver = function( self ) self.color = Color:new( RL.DARKBLUE ) end,
notMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end,
drawBounds = true,
} )
element:add( Gui.text:new( { text = "Giraffe" } ) )
container:add( element )
end
local container2 = Gui.container:new( {
bounds = Rect:new( 0, 0, 154, 64 ),
type = Gui.CONTAINER.HORIZONTAL,
} )
local element = Gui.element:new( {
bounds = itemBounds:clone(),
onMouseOver = function( self ) self.color = Color:new( RL.DARKBLUE ) end,
notMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end,
drawBounds = true,
} )
element:add( Gui.text:new( { text = "Dog" } ) )
container2:add( element )
element = Gui.element:new( {
bounds = Rect:new( 0, 0, 78, 24 ),
-- bounds = Rect:new( 0, 0, 78, 64 ),
onMouseOver = function( self ) self.color = Color:new( RL.DARKBLUE ) end,
notMouseOver = function( self ) self.color = Color:new( RL.LIGHTGRAY ) end,
drawBounds = true,
} )
element:add( Gui.text:new( { text = "Cat" } ) )
container2:add( element )
container:add( container2 )
panel:set2Top()
-- Text input.
textInput = Gui.element:new( {
bounds = Rect:new( 64, 360, 300, 32 ),
drawBounds = true,
color = Color:new( RL.LIGHTGRAY ),
onClicked = function() Gui.setInputFocus( textInput ) end,
inputFocus = function() textInput.color = Color:new( RL.BLUE ) end,
-- inputFocus = function() container:delete() end,
-- inputFocus = function() panel:set2Back() end,
inputUnfocus = function() textInput.color = Color:new( RL.LIGHTGRAY ) end,
} )
textInput:add( Gui.text:new( { text = "", maxTextLen = 16, allowLineBreak = false } ) )
end
function RL.init()
local monitor = 0
local mPos = RL.GetMonitorPosition( monitor )
local mSize = RL.GetMonitorSize( monitor )
winSize = RL.GetScreenSize()
RL.SetWindowTitle( "ReiLuaGui Test" )
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetWindowSize( winSize )
RL.SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } )
initGui()
end
function RL.process( delta )
Gui.process( Vec2:new( RL.GetMousePosition() ) )
end
function RL.draw()
RL.ClearBackground( RL.RAYWHITE )
Gui.draw()
end

View File

@@ -136,5 +136,5 @@ function RL.draw()
camera:endMode3D()
RL.DrawText( RL.defaultFont, "Use keys [Y][R][G][B] to toggle lights", { 10, 10 }, 20, 4, RL.DARKGRAY )
RL.DrawText( RL.GetFontDefault(), "Use keys [Y][R][G][B] to toggle lights", { 10, 10 }, 20, 4, RL.DARKGRAY )
end

View File

@@ -83,7 +83,7 @@ function RL.draw()
end
RL.DrawRectangle( { 0, 0, screenWidth, 40 }, RL.BLACK)
RL.DrawText( RL.defaultFont, "bunnies: " .. #bunnies, { 120, 10 }, 20, 2, RL.GREEN )
RL.DrawText( RL.defaultFont, "batched draw calls: " .. math.ceil( 1 + #bunnies / MAX_BATCH_ELEMENTS ), { 320, 10 }, 20, 2, RL.RED )
RL.DrawText( RL.GetFontDefault(), "bunnies: " .. #bunnies, { 120, 10 }, 20, 2, RL.GREEN )
RL.DrawText( RL.GetFontDefault(), "batched draw calls: " .. math.ceil( 1 + #bunnies / MAX_BATCH_ELEMENTS ), { 320, 10 }, 20, 2, RL.RED )
RL.DrawFPS( { 10, 10 } )
end

View File

@@ -100,5 +100,5 @@ function RL.draw()
RL.ClearBackground( RL.RED )
end
RL.DrawText( RL.defaultFont, text, textPos, 20, 2, RL.BLACK )
RL.DrawText( RL.GetFontDefault(), text, textPos, 20, 2, RL.BLACK )
end

View File

@@ -68,5 +68,6 @@ function RL.draw()
text = text.."\nPress T to toggle target visible.\nVisible: "..tostring( targetVisible )
RL.DrawText( RL.defaultFont, text, { 16, 16 }, 30, 4, RL.WHITE )
-- RL.DrawText( RL.defaultFont, text, { 16, 16 }, 30, 4, RL.WHITE )
RL.DrawText( RL.GetFontDefault(), text, { 16, 16 }, 30, 4, RL.WHITE )
end

View File

@@ -26,7 +26,7 @@ function RL.init()
local mSize = RL.GetMonitorSize( monitor )
local winSize = { 1920, 1080 }
RL.GuiSetFont( RL.defaultFont )
RL.GuiSetFont( RL.GetFontDefault() )
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetWindowSize( winSize )

View File

@@ -1,9 +1,9 @@
local monitor = 0
local texture = -1
local image = -1
local catImage = -1
local catCopy = -1
local textImage = -1
local texture = nil
local image = nil
local catImage = nil
local catCopy = nil
local textImage = nil
function RL.init()
local mPos = RL.GetMonitorPosition( monitor )
@@ -24,14 +24,14 @@ function RL.init()
RL.ImageDrawRectangle( image, { 120, 64, 32, 64 }, RL.BLUE )
RL.ImageDrawRectangleLines( image, { 160, 64, 32, 64 }, 2.0, RL.BLUE )
RL.ImageDraw( image, catImage, { 143, 25, 230, 250 }, { 200, 200, 230, 250 }, RL.WHITE )
RL.ImageDrawTextEx( image, RL.defaultFont, "Hello", { 300, 32 }, 48.0, 1.0, RL.WHITE )
RL.ImageDrawTextEx( image, RL.GetFontDefault(), "Hello", { 300, 32 }, 48.0, 1.0, RL.WHITE )
local src = { 80, 70, 96, 96 }
catCopy = RL.ImageFromImage( catImage, src )
RL.ImageDraw( image, catCopy, src, { 600, 200, src[3], src[4] }, RL.WHITE )
textImage = RL.ImageText( RL.defaultFont, "Cat", 10, 4, RL.WHITE )
textImage = RL.ImageText( RL.GetFontDefault(), "Cat", 10, 4, RL.WHITE )
local imageSize = RL.GetImageSize( textImage )
RL.ImageDraw( image, textImage, { 0, 0, imageSize[1], imageSize[2] }, { 250, 40, imageSize[1], imageSize[2] }, RL.WHITE )

View File

@@ -80,7 +80,7 @@ function RL.draw()
RL.DrawModelEx( model, { 0, 0, 0 }, { 1.0, 0.0, 0.0 }, -90.0, { 1.0, 1.0, 1.0 }, RL.WHITE )
RL.EndMode3D()
RL.DrawText( RL.defaultFont,
RL.DrawText( RL.GetFontDefault(),
"Enter: Change animation\
Space: Play animation\
Up arrow: Inreace animation speed\

View File

@@ -5,12 +5,12 @@ Cam3D = require( "camera3d" )
local PLANE_SIZE = 8
local monitor = 0
local camera = -1
local tileTexture = -1
local mesh = -1
local material = -1
local lightmap = -1
local shader = -1
local camera = {}
local tileTexture = nil
local mesh = nil
local material = nil
local lightmap = nil
local shader = nil
local matrix = {}
@@ -80,8 +80,6 @@ function RL.init()
}
material = RL.CreateMaterial( materialData )
print( "material", material )
matrix = RL.MatrixMultiply( RL.MatrixIdentity(), RL.MatrixTranslate( { -4, 0, -4 } ) )
end
@@ -99,18 +97,8 @@ end
function RL.draw()
RL.ClearBackground( { 25, 50, 50 } )
-- RL.UpdateCamera3D( camera, RL.CAMERA_ORBITAL )
-- RL.UpdateCamera3D( camera, RL.CAMERA_FREE )
-- RL.UpdateCamera3D( camera, RL.CAMERA_FIRST_PERSON )
camera:beginMode3D()
RL.DrawMesh( mesh, material, matrix )
camera:endMode3D()
end
function RL.exit()
material = nil
collectgarbage( "collect" )
tileTexture = nil
collectgarbage( "collect" )
end

View File

@@ -1,54 +0,0 @@
local tex = -1
local pos = { 32, 32 }
local speed = 60.0
local monitor = 0
local mPos = RL.GetMonitorPosition( monitor )
local mSize = RL.GetMonitorSize( monitor )
local framebuffer = nil
local res = { 320, 180 }
local scale = 5
local winSize = { res[1] * scale, res[2] * scale }
function RL.init()
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } )
RL.SetWindowSize( winSize )
tex = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" )
-- Create framebuffer.
framebuffer = RL.LoadRenderTexture( res )
end
function RL.process( delta )
if RL.IsKeyDown( RL.KEY_RIGHT ) then
pos[1] = pos[1] + delta * speed
elseif RL.IsKeyDown( RL.KEY_LEFT ) then
pos[1] = pos[1] - delta * speed
end
if RL.IsKeyDown( RL.KEY_UP ) then
pos[2] = pos[2] - delta * speed
elseif RL.IsKeyDown( RL.KEY_DOWN ) then
pos[2] = pos[2] + delta * speed
end
if RL.IsWindowResized() then
winSize = RL.GetScreenSize()
end
end
function RL.draw()
RL.ClearBackground( { 0, 0, 0 } )
RL.BeginTextureMode( framebuffer )
RL.ClearBackground( { 100, 150, 100 } )
RL.DrawPixel( { 100, 100 }, { 255, 50, 100 } )
RL.DrawLine( { 120, 100 }, { 140, 150 }, 2.4, { 255, 150, 255 } )
RL.DrawRectangle( { 200, 120, 40, 50 }, { 100, 170, 255 } )
RL.DrawTexturePro( tex, { 166, 138, 128, 128 }, { pos[1], pos[2], 128, 128 }, { 16, 16 }, 0.0, RL.WHITE )
RL.DrawText( RL.defaultFont, "Cat MIAU!!", { 16, 32 }, 10, 1, { 255, 180, 155 } )
RL.DrawTriangle( { 0, 32 }, { 32, 16 }, { 0, 0 }, RL.RED )
RL.EndTextureMode()
RL.DrawTexturePro( RL.GetRenderTextureTexture( framebuffer ), { 0, 0, res[1], -res[2] }, { 0, 0, winSize[1], winSize[2] }, { 0, 0 }, 0.0, { 255, 255, 255 } )
end

View File

@@ -1,120 +0,0 @@
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
util = require "utillib"
Vec2 = require "vector2"
Vec3 = require "vector3"
local MOVE_SPEED = 0.5
local monitor = 0
local camera = -1
local tri = {
a = Vec3:new( 0, 0, 0 ),
-- a = Vec3:new( 0, 1, 0 ),
b = Vec3:new( 0, 0, 2 ),
c = Vec3:new( 1, 0, 0 ),
normal = Vec3:new( 0, 0, 0 ),
}
local point = {
pos = Vec3:new( 0.2, 0.3, 0.2 ),
radius = 0.05,
lineLen = 0.5,
color = RL.RED,
}
local debugText = ""
local function calcNormal( tri )
tri.normal = Vec3:new( RL.Vector3Normalize( RL.Vector3CrossProduct( tri.b - tri.a, tri.c - tri.a ) ) )
end
function RL.init()
local mPos = RL.GetMonitorPosition( monitor )
local mSize = RL.GetMonitorSize( monitor )
local winSize = { 1920, 1080 }
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } )
RL.SetWindowSize( winSize )
camera = RL.CreateCamera3D()
RL.SetCamera3DPosition( camera, { 0, 1, 2 } )
RL.SetCamera3DTarget( camera, { 0, 0, 0 } )
RL.SetCamera3DUp( camera, { 0, 1, 0 } )
calcNormal( tri )
end
local function checkCollisionPointTriangle( p, a, b, c, n )
local result = Vec3:new( 0, 0, 0 )
local v0 = Vec3:new( b.x - a.x, b.y - a.y, b.z - a.z ) -- Vector3Subtract( b, a )
local v1 = Vec3:new( c.x - a.x, c.y - a.y, c.z - a.z ) -- Vector3Subtract( c, a )
local v2 = Vec3:new( p.x - a.x, p.y - a.y, p.z - a.z ) -- Vector3Subtract( p, a )
local d00 = v0.x * v0.x + v0.y * v0.y + v0.z * v0.z -- Vector3DotProduct( v0, v0 )
local d01 = v0.x * v1.x + v0.y * v1.y + v0.z * v1.z -- Vector3DotProduct( v0, v1 )
local d11 = v1.x * v1.x + v1.y * v1.y + v1.z * v1.z -- Vector3DotProduct( v1, v1 )
local d20 = v2.x * v0.x + v2.y * v0.y + v2.z * v0.z -- Vector3DotProduct( v2, v0 )
local d21 = v2.x * v1.x + v2.y * v1.y + v2.z * v1.z -- Vector3DotProduct( v2, v1 )
local denom = d00 * d11 - d01 * d01
local distance = v2.x * n.x + v2.y * n.y + v2.z * n.z -- Vector3DotProduct( v2, n )
debugText = debugText.."distance "..distance.."\n"
debugText = debugText.."v0 "..v0.x..", "..v0.y..", "..v0.z.."\n"
debugText = debugText.."v1 "..v1.x..", "..v1.y..", "..v1.z.."\n"
debugText = debugText.."v2 "..v2.x..", "..v2.y..", "..v2.z.."\n"
result.y = ( d11 * d20 - d01 * d21) / denom
result.z = ( d00 * d21 - d01 * d20) / denom
result.x = 1.0 - ( result.z + result.y )
debugText = debugText.."result "..result.x..", "..result.y..", "..result.z.."\n"
return 0.0 < result.x and 0.0 < result.y and 0.0 < result.z and distance < 0.0, distance
end
function RL.process( delta )
debugText = ""
if RL.IsKeyDown( string.byte( "D" ) ) then
point.pos.x = point.pos.x + MOVE_SPEED * delta
elseif RL.IsKeyDown( string.byte( "A" ) ) then
point.pos.x = point.pos.x - MOVE_SPEED * delta
end
if RL.IsKeyDown( string.byte( "S" ) ) then
point.pos.z = point.pos.z + MOVE_SPEED * delta
elseif RL.IsKeyDown( string.byte( "W" ) ) then
point.pos.z = point.pos.z - MOVE_SPEED * delta
end
if RL.IsKeyDown( string.byte( "R" ) ) then
point.pos.y = point.pos.y + MOVE_SPEED * delta
elseif RL.IsKeyDown( string.byte( "F" ) ) then
point.pos.y = point.pos.y - MOVE_SPEED * delta
end
if checkCollisionPointTriangle( point.pos, tri.a, tri.b, tri.c, tri.normal ) then
point.color = RL.RED
else
point.color = RL.GREEN
end
end
function RL.draw()
RL.ClearBackground( { 100, 150, 100 } )
RL.BeginMode3D( camera )
RL.DrawGrid( 8, 1 )
RL.DrawTriangle3D( tri.a, tri.b, tri.c, { 200, 100, 100 } )
RL.DrawLine3D( { point.pos.x - point.lineLen, point.pos.y, point.pos.z },
{ point.pos.x + point.lineLen, point.pos.y, point.pos.z }, RL.BLUE )
RL.DrawLine3D( { point.pos.x, point.pos.y - point.lineLen, point.pos.z },
{ point.pos.x, point.pos.y + point.lineLen, point.pos.z }, RL.BLUE )
RL.DrawLine3D( { point.pos.x, point.pos.y, point.pos.z - point.lineLen },
{ point.pos.x, point.pos.y, point.pos.z + point.lineLen }, RL.BLUE )
RL.DrawSphereWires( point.pos, point.radius, 3, 8, point.color )
RL.EndMode3D()
RL.DrawText( RL.defaultFont, debugText, { 10, 10 }, 30, 4, RL.WHITE )
end

View File

@@ -1,5 +1,11 @@
-- Pong example using Vector2 library.
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Vec2 = require "vector2"
-- Settings.
local winSize = { 800, 600 }
local winSize = Vec2:new( 800, 600 )
local monitor = 0
-- Constants.
@@ -8,54 +14,54 @@ local BALL_SPEED = 330
-- Game objects.
local playerLeft = {
pos = { 0, 0 },
size = { 10, 70 },
pos = Vec2:new( 0, 0 ),
size = Vec2:new( 10, 70 ),
score = 0,
}
local playerRight = {
pos = { 0, 0 },
size = { 10, 70 },
pos = Vec2:new( 0, 0 ),
size = Vec2:new( 10, 70 ),
score = 0,
}
local ball = {
pos = { 0, 0 },
pos = Vec2:new( 0, 0 ),
radius = 8.0,
vel = { 0, 0 },
vel = Vec2:new( 0, 0 ),
}
local function reset()
-- Initialize player positions.
playerLeft.pos[1] = playerLeft.size[1]
playerLeft.pos[2] = winSize[2] / 2 - playerLeft.size[2] / 2
playerRight.pos[1] = winSize[1] - playerRight.size[1] * 2
playerRight.pos[2] = winSize[2] / 2 - playerRight.size[2] / 2
playerLeft.pos.x = playerLeft.size.x
playerLeft.pos.y = winSize.y / 2 - playerLeft.size.y / 2
playerRight.pos.x = winSize.x - playerRight.size.x * 2
playerRight.pos.y = winSize.y / 2 - playerRight.size.y / 2
-- Set ball to center.
ball.pos = { winSize[1] / 2, winSize[2] / 2 }
ball.pos:set( winSize.x / 2, winSize.y / 2 )
-- Short for if math random result 1, set BALL_SPEED otherwise set -BALL_SPEED.
-- Could be replaced by normal if statement for easier readability.
ball.vel[1] = math.random( 0, 1 ) == 1 and BALL_SPEED or -BALL_SPEED
ball.vel.x = math.random( 0, 1 ) == 1 and BALL_SPEED or -BALL_SPEED
-- Start slow.
ball.vel[2] = 0
ball.vel.y = 0
end
local function ballHit( padPos, padSize )
ball.vel[1] = -ball.vel[1]
ball.vel.x = -ball.vel.x
local padCenter = padPos[2] + padSize[2] / 2
local relHitPos = ball.pos[2] - padCenter
ball.vel[2] = BALL_SPEED * relHitPos / padSize[2] * 2
local padCenter = padPos.y + padSize.y / 2
local relHitPos = ball.pos.y - padCenter
ball.vel.y = BALL_SPEED * relHitPos / padSize.y * 2
end
function RL.init()
-- Set window to center of monitor.
local mPos = RL.GetMonitorPosition( monitor )
local mSize = RL.GetMonitorSize( monitor )
local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetConfigFlags( RL.FLAG_VSYNC_HINT )
RL.SetWindowSize( winSize )
RL.SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } )
RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )
RL.SetWindowTitle( "Pong" )
-- Initialize ball pos.
@@ -65,46 +71,45 @@ end
function RL.process( delta )
-- Left player controls.
if RL.IsKeyDown( string.byte( "W" ) ) and 0 < playerLeft.pos[2] then
playerLeft.pos[2] = playerLeft.pos[2] - PLAYER_SPEED * delta
elseif RL.IsKeyDown( string.byte( "S" ) ) and playerLeft.pos[2] + playerLeft.size[2] < winSize[2] then
playerLeft.pos[2] = playerLeft.pos[2] + PLAYER_SPEED * delta
if RL.IsKeyDown( RL.KEY_W ) and 0 < playerLeft.pos.y then
playerLeft.pos.y = playerLeft.pos.y - PLAYER_SPEED * delta
elseif RL.IsKeyDown( RL.KEY_S ) and playerLeft.pos.y + playerLeft.size.y < winSize.y then
playerLeft.pos.y = playerLeft.pos.y + PLAYER_SPEED * delta
end
-- Right player controls.
if RL.IsKeyDown( RL.KEY_UP ) and 0 < playerRight.pos[2] then
playerRight.pos[2] = playerRight.pos[2] - PLAYER_SPEED * delta
elseif RL.IsKeyDown( RL.KEY_DOWN ) and playerRight.pos[2] + playerRight.size[2] < winSize[2] then
playerRight.pos[2] = playerRight.pos[2] + PLAYER_SPEED * delta
if RL.IsKeyDown( RL.KEY_UP ) and 0 < playerRight.pos.y then
playerRight.pos.y = playerRight.pos.y - PLAYER_SPEED * delta
elseif RL.IsKeyDown( RL.KEY_DOWN ) and playerRight.pos.y + playerRight.size.y < winSize.y then
playerRight.pos.y = playerRight.pos.y + PLAYER_SPEED * delta
end
-- Move ball.
ball.pos = { ball.pos[1] + ball.vel[1] * delta,
ball.pos[2] + ball.vel[2] * delta }
ball.pos = ball.pos + ball.vel:scale( delta )
-- Bounce from window edge.
if ( ball.pos[2] < ball.radius and ball.vel[2] < 0 )
or ( winSize[2] < ball.pos[2] + ball.radius and 0 < ball.vel[2] ) then
ball.vel[2] = -ball.vel[2]
if ( ball.pos.y < ball.radius and ball.vel.y < 0 )
or ( winSize.y < ball.pos.y + ball.radius and 0 < ball.vel.y ) then
ball.vel.y = -ball.vel.y
end
-- Bounce from players.
local playerLeftRect = { playerLeft.pos[1], playerLeft.pos[2],
playerLeft.size[1], playerLeft.size[2] }
local playerRightRect = { playerRight.pos[1], playerRight.pos[2],
playerRight.size[1], playerRight.size[2] }
local playerLeftRect = { playerLeft.pos.x, playerLeft.pos.y,
playerLeft.size.x, playerLeft.size.y }
local playerRightRect = { playerRight.pos.x, playerRight.pos.y,
playerRight.size.x, playerRight.size.y }
if RL.CheckCollisionCircleRec( ball.pos, ball.radius, playerLeftRect ) and ball.vel[1] < 0 then
if RL.CheckCollisionCircleRec( ball.pos, ball.radius, playerLeftRect ) and ball.vel.x < 0 then
ballHit( playerLeft.pos, playerLeft.size )
elseif RL.CheckCollisionCircleRec( ball.pos, ball.radius, playerRightRect ) and 0 < ball.vel[1] then
elseif RL.CheckCollisionCircleRec( ball.pos, ball.radius, playerRightRect ) and 0 < ball.vel.x then
ballHit( playerRight.pos, playerRight.size )
end
-- Score.
if ball.pos[1] < 0 then
if ball.pos.x < 0 then
playerRight.score = playerRight.score + 1
reset()
elseif winSize[1] < ball.pos[1] then
elseif winSize.x < ball.pos.x then
playerLeft.score = playerLeft.score + 1
reset()
end
@@ -114,14 +119,14 @@ function RL.draw()
RL.ClearBackground( RL.BLACK )
-- Draw players.
RL.DrawRectangle( { playerLeft.pos[1], playerLeft.pos[2], playerLeft.size[1], playerLeft.size[2] }, RL.WHITE )
RL.DrawRectangle( { playerRight.pos[1], playerRight.pos[2], playerRight.size[1], playerRight.size[2] }, RL.WHITE )
RL.DrawRectangle( { playerLeft.pos.x, playerLeft.pos.y, playerLeft.size.x, playerLeft.size.y }, RL.WHITE )
RL.DrawRectangle( { playerRight.pos.x, playerRight.pos.y, playerRight.size.x, playerRight.size.y }, RL.WHITE )
-- Draw ball. Ball position will be the center in drawCircle.
RL.DrawCircle( ball.pos, ball.radius, RL.WHITE )
-- Draw scire
RL.DrawText( RL.defaultFont, playerLeft.score, { 50, 10 }, 40, 2, RL.WHITE )
local rightTextSize = RL.MeasureText( RL.defaultFont, playerRight.score, 40, 2 )
RL.DrawText( RL.defaultFont, playerRight.score, { winSize[1] - 50 - rightTextSize[1], 10 }, 40, 2, RL.WHITE )
-- Draw score.
RL.DrawText( RL.GetFontDefault(), tostring( playerLeft.score ), { 50, 10 }, 40, 2, RL.WHITE )
local rightTextSize = Vec2:new( RL.MeasureText( RL.GetFontDefault(), tostring( playerRight.score ), 40, 2 ) )
RL.DrawText( RL.GetFontDefault(), tostring( playerRight.score ), { winSize.x - 50 - rightTextSize.x, 10 }, 40, 2, RL.WHITE )
end

View File

@@ -1,132 +0,0 @@
-- Pong example using Vector2 library.
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Vec2 = require "vector2"
-- Settings.
local winSize = Vec2:new( 800, 600 )
local monitor = 0
-- Constants.
local PLAYER_SPEED = 300
local BALL_SPEED = 330
-- Game objects.
local playerLeft = {
pos = Vec2:new( 0, 0 ),
size = Vec2:new( 10, 70 ),
score = 0,
}
local playerRight = {
pos = Vec2:new( 0, 0 ),
size = Vec2:new( 10, 70 ),
score = 0,
}
local ball = {
pos = Vec2:new( 0, 0 ),
radius = 8.0,
vel = Vec2:new( 0, 0 ),
}
local function reset()
-- Initialize player positions.
playerLeft.pos.x = playerLeft.size.x
playerLeft.pos.y = winSize.y / 2 - playerLeft.size.y / 2
playerRight.pos.x = winSize.x - playerRight.size.x * 2
playerRight.pos.y = winSize.y / 2 - playerRight.size.y / 2
-- Set ball to center.
ball.pos:set( winSize.x / 2, winSize.y / 2 )
-- Short for if math random result 1, set BALL_SPEED otherwise set -BALL_SPEED.
-- Could be replaced by normal if statement for easier readability.
ball.vel.x = math.random( 0, 1 ) == 1 and BALL_SPEED or -BALL_SPEED
-- Start slow.
ball.vel.y = 0
end
local function ballHit( padPos, padSize )
ball.vel.x = -ball.vel.x
local padCenter = padPos.y + padSize.y / 2
local relHitPos = ball.pos.y - padCenter
ball.vel.y = BALL_SPEED * relHitPos / padSize.y * 2
end
function RL.init()
-- Set window to center of monitor.
local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
RL.SetConfigFlags( RL.FLAG_VSYNC_HINT )
RL.SetWindowSize( winSize )
RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )
RL.SetWindowTitle( "Pong" )
-- Initialize ball pos.
math.randomseed( os.time() )
reset()
end
function RL.process( delta )
-- Left player controls.
if RL.IsKeyDown( RL.KEY_W ) and 0 < playerLeft.pos.y then
playerLeft.pos.y = playerLeft.pos.y - PLAYER_SPEED * delta
elseif RL.IsKeyDown( RL.KEY_S ) and playerLeft.pos.y + playerLeft.size.y < winSize.y then
playerLeft.pos.y = playerLeft.pos.y + PLAYER_SPEED * delta
end
-- Right player controls.
if RL.IsKeyDown( RL.KEY_UP ) and 0 < playerRight.pos.y then
playerRight.pos.y = playerRight.pos.y - PLAYER_SPEED * delta
elseif RL.IsKeyDown( RL.KEY_DOWN ) and playerRight.pos.y + playerRight.size.y < winSize.y then
playerRight.pos.y = playerRight.pos.y + PLAYER_SPEED * delta
end
-- Move ball.
ball.pos = ball.pos + ball.vel:scale( delta )
-- Bounce from window edge.
if ( ball.pos.y < ball.radius and ball.vel.y < 0 )
or ( winSize.y < ball.pos.y + ball.radius and 0 < ball.vel.y ) then
ball.vel.y = -ball.vel.y
end
-- Bounce from players.
local playerLeftRect = { playerLeft.pos.x, playerLeft.pos.y,
playerLeft.size.x, playerLeft.size.y }
local playerRightRect = { playerRight.pos.x, playerRight.pos.y,
playerRight.size.x, playerRight.size.y }
if RL.CheckCollisionCircleRec( ball.pos, ball.radius, playerLeftRect ) and ball.vel.x < 0 then
ballHit( playerLeft.pos, playerLeft.size )
elseif RL.CheckCollisionCircleRec( ball.pos, ball.radius, playerRightRect ) and 0 < ball.vel.x then
ballHit( playerRight.pos, playerRight.size )
end
-- Score.
if ball.pos.x < 0 then
playerRight.score = playerRight.score + 1
reset()
elseif winSize.x < ball.pos.x then
playerLeft.score = playerLeft.score + 1
reset()
end
end
function RL.draw()
RL.ClearBackground( RL.BLACK )
-- Draw players.
RL.DrawRectangle( { playerLeft.pos.x, playerLeft.pos.y, playerLeft.size.x, playerLeft.size.y }, RL.WHITE )
RL.DrawRectangle( { playerRight.pos.x, playerRight.pos.y, playerRight.size.x, playerRight.size.y }, RL.WHITE )
-- Draw ball. Ball position will be the center in drawCircle.
RL.DrawCircle( ball.pos, ball.radius, RL.WHITE )
-- Draw score.
RL.DrawText( RL.defaultFont, tostring( playerLeft.score ), { 50, 10 }, 40, 2, RL.WHITE )
local rightTextSize = Vec2:new( RL.MeasureText( RL.defaultFont, tostring( playerRight.score ), 40, 2 ) )
RL.DrawText( RL.defaultFont, tostring( playerRight.score ), { winSize.x - 50 - rightTextSize.x, 10 }, 40, 2, RL.WHITE )
end

View File

@@ -53,7 +53,7 @@ function RL.draw()
RL.DrawGrid( 8, 1 )
RL.DrawRay( ray, { 255, 100, 100 } )
RL.DrawMesh( sphereMesh, RL.defaultMaterial, RL.MatrixIdentity() )
RL.DrawMesh( sphereMesh, RL.GetMaterialDefault(), RL.MatrixIdentity() )
RL.DrawSphereWires( rayCol.point, 0.05, 4, 8, RL.BLUE )
RL.DrawLine3D( rayCol.point, RL.Vector3Add( rayCol.point, rayCol.normal ), RL.GREEN )
RL.EndMode3D()

View File

@@ -40,7 +40,7 @@ Gui = {
},
mouseButton = RL.MOUSE_BUTTON_LEFT,
font = RL.defaultFont,
font = RL.GetFontDefault(),
fontSize = 20,
padding = 2,
spacing = 4,

View File

@@ -213,7 +213,7 @@ function RL.draw()
drawApple()
if gameState == STATE.OVER then
RL.DrawText( RL.defaultFont, "Press Enter to\nrestart", { 10, 10 }, 10, 2, RL.WHITE )
RL.DrawText( RL.GetFontDefault(), "Press Enter to\nrestart", { 10, 10 }, 10, 2, RL.WHITE )
end
RL.EndTextureMode()

View File

@@ -24,5 +24,5 @@ end
function RL.draw()
RL.ClearBackground( RL.RAYWHITE )
RL.DrawText( RL.defaultFont, text, textPos, 20, 2, textColor )
RL.DrawText( RL.GetFontDefault(), text, textPos, 20, 2, textColor )
end