Fix some issues in examples and favor vsync instead of fps locking.

This commit is contained in:
TSnake41 2020-03-29 15:40:02 +02:00
parent 83e4094cf6
commit ada4f23d2b
4 changed files with 127 additions and 129 deletions

View File

@ -1,8 +1,6 @@
setmetatable(_G, { __index = rl }) setmetatable(_G, { __index = rl })
SetConfigFlags(FLAG_VSYNC_HINT) SetConfigFlags(FLAG_VSYNC_HINT)
SetTargetFPS(60)
InitWindow(800, 450, "raylib [lua] example - global api") InitWindow(800, 450, "raylib [lua] example - global api")
while not WindowShouldClose() do while not WindowShouldClose() do

View File

@ -5,7 +5,7 @@ local screenHeight = 450
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT) rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT)
rl.InitWindow(screenWidth, screenHeight, "Physac [raylib-lua] - Physics demo") rl.InitWindow(screenWidth, screenHeight, "Physac [raylua] - Physics demo")
local logoX = screenWidth - rl.MeasureText("Physac", 30) - 10 local logoX = screenWidth - rl.MeasureText("Physac", 30) - 10
local logoY = 15 local logoY = 15

View File

@ -1,11 +1,9 @@
local ffi = require "ffi" local ffi = require "ffi"
local lua_color = ffi.new("Color", 3, 3, 128, 255) local lua_color = ffi.new("Color", 3, 3, 128, 255)
rl.SetConfigFlags(rl.FLAG_VSYNC_HINT)
rl.SetTargetFPS(60)
local width, height = 800, 450 local width, height = 800, 450
rl.SetConfigFlags(rl.FLAG_VSYNC_HINT)
rl.InitWindow(800, 450, "raylib [shapes] example - basic shapes drawing") rl.InitWindow(800, 450, "raylib [shapes] example - basic shapes drawing")
while not rl.WindowShouldClose() do while not rl.WindowShouldClose() do

View File

@ -9,6 +9,7 @@
-- --
-------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------
local ffi = require "ffi" local ffi = require "ffi"
local bit = require "bit"
local MAX_BUNNIES = 100000 -- 100K bunnies limit local MAX_BUNNIES = 100000 -- 100K bunnies limit
@ -17,8 +18,9 @@ local MAX_BUNNIES = 100000 -- 100K bunnies limit
local MAX_BATCH_ELEMENTS = 8192 local MAX_BATCH_ELEMENTS = 8192
-- Create the Bunny class. -- Create the Bunny class.
Bunny = {} local Bunny = {}
Bunny.__index = Bunny Bunny.__index = Bunny
function Bunny:new(pos, spd, col) function Bunny:new(pos, spd, col)
local bunny = {} local bunny = {}
setmetatable(bunny,Bunny) setmetatable(bunny,Bunny)
@ -27,13 +29,16 @@ function Bunny:new(pos, spd, col)
bunny.color = col bunny.color = col
return bunny return bunny
end end
function Bunny:update(texture) function Bunny:update(texture)
self.position.x = self.position.x + self.speed.x self.position.x = self.position.x + self.speed.x
self.position.y = self.position.y + self.speed.y self.position.y = self.position.y + self.speed.y
if ((self.position.x + texture.width/2) > rl.GetScreenWidth()) or ((self.position.x + texture.width/2) < 0) then if ((self.position.x + texture.width/2) > rl.GetScreenWidth())
or ((self.position.x + texture.width/2) < 0) then
self.speed.x = self.speed.x * -1 self.speed.x = self.speed.x * -1
end end
if ((self.position.y + texture.height/2) > rl.GetScreenHeight()) or ((self.position.y + texture.height/2 - 40) < 0) then if ((self.position.y + texture.height/2) > rl.GetScreenHeight())
or ((self.position.y + texture.height/2 - 40) < 0) then
self.speed.y = self.speed.y * -1 self.speed.y = self.speed.y * -1
end end
end end
@ -43,63 +48,60 @@ end
local screenWidth = 800 local screenWidth = 800
local screenHeight = 450 local screenHeight = 450
rl.SetConfigFlags(rl.FLAG_VSYNC_HINT)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark") rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark")
-- Load bunny texture -- Load bunny texture
local texBunny = rl.LoadTexture("resources/wabbit_alpha.png") local texBunny = rl.LoadTexture("resources/wabbit_alpha.png")
local bunnies = {} local bunnies = {}
rl.SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
---------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------
-- Main game loop -- Main game loop
while not rl.WindowShouldClose() do -- Detect window close button or ESC key while not rl.WindowShouldClose() do -- Detect window close button or ESC key
-- Update -- Update
------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------
if rl.IsMouseButtonDown(rl.MOUSE_LEFT_BUTTON) then if rl.IsMouseButtonDown(rl.MOUSE_LEFT_BUTTON) then
-- Create more bunnies -- Create more bunnies
for i = 1, 100 do for i = 1, 100 do
if #bunnies < MAX_BUNNIES then if #bunnies < MAX_BUNNIES then
local speed = ffi.new("Vector2", rl.GetRandomValue(-250, 250) / 60, rl.GetRandomValue(-250, 250) / 60) local speed = ffi.new("Vector2", rl.GetRandomValue(-250, 250) / 60, rl.GetRandomValue(-250, 250) / 60)
local color = ffi.new("Color", rl.GetRandomValue(50, 240), rl.GetRandomValue(80, 240), rl.GetRandomValue(100, 240), 255) local color = ffi.new("Color", rl.GetRandomValue(50, 240), rl.GetRandomValue(80, 240), rl.GetRandomValue(100, 240), 255)
--bunnies[#bunnies] = Bunny:new(nil, GetMousePosition(), speed, color) --bunnies[#bunnies] = Bunny:new(nil, GetMousePosition(), speed, color)
table.insert(bunnies, Bunny:new(rl.GetMousePosition(), speed, color)) table.insert(bunnies, Bunny:new(rl.GetMousePosition(), speed, color))
end end
end
end end
end
-- Update bunnies -- Update bunnies
for i = 1, #bunnies do for i = 1, #bunnies do
bunnies[i]:update(texBunny) bunnies[i]:update(texBunny)
end end
------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------
-- Draw -- Draw
------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------
rl.BeginDrawing(); rl.BeginDrawing()
rl.ClearBackground(rl.RAYWHITE); rl.ClearBackground(rl.RAYWHITE)
for i = 1, #bunnies do for i=1,#bunnies do
-- NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), -- NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
-- a draw call is launched and buffer starts being filled again; -- a draw call is launched and buffer starts being filled again;
-- before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU... -- before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
-- Process of sending data is costly and it could happen that GPU data has not been completely -- Process of sending data is costly and it could happen that GPU data has not been completely
-- processed for drawing while new data is tried to be sent (updating current in-use buffers) -- processed for drawing while new data is tried to be sent (updating current in-use buffers)
-- it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies -- it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
rl.DrawTexture(texBunny, bunnies[i].position.x, bunnies[i].position.y, bunnies[i].color); rl.DrawTexture(texBunny, bunnies[i].position.x, bunnies[i].position.y, bunnies[i].color);
end end
rl.DrawRectangle(0, 0, screenWidth, 40, rl.BLACK) rl.DrawRectangle(0, 0, screenWidth, 40, rl.BLACK)
rl.DrawText("bunnies: " .. #bunnies, 120, 10, 20, rl.GREEN) rl.DrawText("bunnies: " .. #bunnies, 120, 10, 20, rl.GREEN)
rl.DrawText("batched draw calls: " .. math.ceil(1 + #bunnies / MAX_BATCH_ELEMENTS), 320, 10, 20, rl.MAROON) rl.DrawText("batched draw calls: " .. math.ceil(1 + #bunnies / MAX_BATCH_ELEMENTS), 320, 10, 20, rl.MAROON)
-- DrawText(FormatText("bunnies: %i", #bunnies), 120, 10, 20, GREEN)
-- DrawText(FormatText("batched draw calls: %i", 1 + #bunnies/MAX_BATCH_ELEMENTS), 320, 10, 20, MAROON)
rl.DrawFPS(10, 10) rl.DrawFPS(10, 10)
rl.EndDrawing() rl.EndDrawing()
------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------
end end