From ada4f23d2b102e5a6ca9ed56302b30102cdd0668 Mon Sep 17 00:00:00 2001 From: TSnake41 Date: Sun, 29 Mar 2020 15:40:02 +0200 Subject: [PATCH] Fix some issues in examples and favor vsync instead of fps locking. --- examples/lua_global_api.lua | 32 +++-- examples/physics_demo.lua | 2 +- examples/shapes_logo_raylib.lua | 4 +- examples/textures_bunnymark.lua | 218 ++++++++++++++++---------------- 4 files changed, 127 insertions(+), 129 deletions(-) diff --git a/examples/lua_global_api.lua b/examples/lua_global_api.lua index 22c0cdf..23f2d37 100644 --- a/examples/lua_global_api.lua +++ b/examples/lua_global_api.lua @@ -1,17 +1,15 @@ -setmetatable(_G, { __index = rl }) - -SetConfigFlags(FLAG_VSYNC_HINT) -SetTargetFPS(60) - -InitWindow(800, 450, "raylib [lua] example - global api") - -while not WindowShouldClose() do - BeginDrawing() - - ClearBackground(RAYWHITE) - DrawText("Global API !", 350, 200, 20, LIGHTGRAY) - - EndDrawing() -end - -CloseWindow() +setmetatable(_G, { __index = rl }) + +SetConfigFlags(FLAG_VSYNC_HINT) +InitWindow(800, 450, "raylib [lua] example - global api") + +while not WindowShouldClose() do + BeginDrawing() + + ClearBackground(RAYWHITE) + DrawText("Global API !", 350, 200, 20, LIGHTGRAY) + + EndDrawing() +end + +CloseWindow() diff --git a/examples/physics_demo.lua b/examples/physics_demo.lua index 395535f..c316a56 100644 --- a/examples/physics_demo.lua +++ b/examples/physics_demo.lua @@ -5,7 +5,7 @@ local screenHeight = 450 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 logoY = 15 diff --git a/examples/shapes_logo_raylib.lua b/examples/shapes_logo_raylib.lua index 84f9e82..55a6ab4 100644 --- a/examples/shapes_logo_raylib.lua +++ b/examples/shapes_logo_raylib.lua @@ -1,11 +1,9 @@ local ffi = require "ffi" local lua_color = ffi.new("Color", 3, 3, 128, 255) -rl.SetConfigFlags(rl.FLAG_VSYNC_HINT) -rl.SetTargetFPS(60) - local width, height = 800, 450 +rl.SetConfigFlags(rl.FLAG_VSYNC_HINT) rl.InitWindow(800, 450, "raylib [shapes] example - basic shapes drawing") while not rl.WindowShouldClose() do diff --git a/examples/textures_bunnymark.lua b/examples/textures_bunnymark.lua index 3671f42..099d183 100644 --- a/examples/textures_bunnymark.lua +++ b/examples/textures_bunnymark.lua @@ -1,112 +1,114 @@ --------------------------------------------------------------------------------------------- --- --- raylib [textures] example - Bunnymark --- --- This example has been created using raylib 1.6 (www.raylib.com) --- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) --- --- Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) --- --------------------------------------------------------------------------------------------- -local ffi = require "ffi" - -local MAX_BUNNIES = 100000 -- 100K bunnies limit - --- This is the maximum amount of elements (quads) per batch --- NOTE: This value is defined in [rlgl] module and can be changed there -local MAX_BATCH_ELEMENTS = 8192 - --- Create the Bunny class. -Bunny = {} -Bunny.__index = Bunny -function Bunny:new(pos, spd, col) - local bunny = {} - setmetatable(bunny,Bunny) - bunny.position = pos - bunny.speed = spd - bunny.color = col - return bunny -end -function Bunny:update(texture) - self.position.x = self.position.x + self.speed.x - 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 - self.speed.x = self.speed.x * -1 - end - 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 - end -end - --- Initialization +-------------------------------------------------------------------------------------------- +-- +-- raylib [textures] example - Bunnymark +-- +-- This example has been created using raylib 1.6 (www.raylib.com) +-- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +-- +-- Copyright (c) 2014-2019 Ramon Santamaria (@raysan5) +-- +-------------------------------------------------------------------------------------------- +local ffi = require "ffi" +local bit = require "bit" + +local MAX_BUNNIES = 100000 -- 100K bunnies limit + +-- This is the maximum amount of elements (quads) per batch +-- NOTE: This value is defined in [rlgl] module and can be changed there +local MAX_BATCH_ELEMENTS = 8192 + +-- Create the Bunny class. +local Bunny = {} +Bunny.__index = Bunny + +function Bunny:new(pos, spd, col) + local bunny = {} + setmetatable(bunny,Bunny) + bunny.position = pos + bunny.speed = spd + bunny.color = col + return bunny +end + +function Bunny:update(texture) + self.position.x = self.position.x + self.speed.x + 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 + self.speed.x = self.speed.x * -1 + end + 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 + end +end + +-- Initialization ---------------------------------------------------------------------------------------- local screenWidth = 800 local screenHeight = 450 -rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark") - --- Load bunny texture -local texBunny = rl.LoadTexture("resources/wabbit_alpha.png") - -local bunnies = {} - -rl.SetTargetFPS(60) -- Set our game to run at 60 frames-per-second ----------------------------------------------------------------------------------------- - --- Main game loop -while not rl.WindowShouldClose() do -- Detect window close button or ESC key - -- Update - ------------------------------------------------------------------------------------ - if rl.IsMouseButtonDown(rl.MOUSE_LEFT_BUTTON) then - -- Create more bunnies - for i = 1, 100 do - if #bunnies < MAX_BUNNIES then - 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) - --bunnies[#bunnies] = Bunny:new(nil, GetMousePosition(), speed, color) - table.insert(bunnies, Bunny:new(rl.GetMousePosition(), speed, color)) - end - end - end - - -- Update bunnies - for i = 1, #bunnies do - bunnies[i]:update(texBunny) - end - ------------------------------------------------------------------------------------ - - -- Draw - ------------------------------------------------------------------------------------ - rl.BeginDrawing(); - - rl.ClearBackground(rl.RAYWHITE); - - for i = 1, #bunnies do - -- NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), - -- 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... - -- 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) - -- 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); - end - - rl.DrawRectangle(0, 0, screenWidth, 40, rl.BLACK) - 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) - -- 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.EndDrawing() - ------------------------------------------------------------------------------------ -end - --- De-Initialization ----------------------------------------------------------------------------------------- - -rl.UnloadTexture(texBunny) -- Unload bunny texture - -rl.CloseWindow() -- Close window and OpenGL context ----------------------------------------------------------------------------------------- +rl.SetConfigFlags(rl.FLAG_VSYNC_HINT) +rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark") + +-- Load bunny texture +local texBunny = rl.LoadTexture("resources/wabbit_alpha.png") + +local bunnies = {} +---------------------------------------------------------------------------------------- + +-- Main game loop +while not rl.WindowShouldClose() do -- Detect window close button or ESC key + -- Update + ------------------------------------------------------------------------------------ + if rl.IsMouseButtonDown(rl.MOUSE_LEFT_BUTTON) then + -- Create more bunnies + for i = 1, 100 do + if #bunnies < MAX_BUNNIES then + 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) + --bunnies[#bunnies] = Bunny:new(nil, GetMousePosition(), speed, color) + table.insert(bunnies, Bunny:new(rl.GetMousePosition(), speed, color)) + end + end + end + + -- Update bunnies + for i = 1, #bunnies do + bunnies[i]:update(texBunny) + end + ------------------------------------------------------------------------------------ + + -- Draw + ------------------------------------------------------------------------------------ + rl.BeginDrawing() + + rl.ClearBackground(rl.RAYWHITE) + + for i=1,#bunnies do + -- NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS), + -- 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... + -- 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) + -- 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); + end + + rl.DrawRectangle(0, 0, screenWidth, 40, rl.BLACK) + 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.DrawFPS(10, 10) + + rl.EndDrawing() + ------------------------------------------------------------------------------------ +end + +-- De-Initialization +---------------------------------------------------------------------------------------- + +rl.UnloadTexture(texBunny) -- Unload bunny texture + +rl.CloseWindow() -- Close window and OpenGL context +----------------------------------------------------------------------------------------