diff options
| author | jussi | 2022-03-06 15:01:59 +0200 |
|---|---|---|
| committer | jussi | 2022-03-06 15:01:59 +0200 |
| commit | debe4baa8c208458f847dd4c89c17f7cc39be559 (patch) | |
| tree | 9ecde213fee634ea075b2ef564a8226eda5545a8 /examples | |
| parent | 8035d31109964504d970ed302c7aa6ffa0a8f106 (diff) | |
| download | reilua-enhanced-debe4baa8c208458f847dd4c89c17f7cc39be559.tar.gz reilua-enhanced-debe4baa8c208458f847dd4c89c17f7cc39be559.tar.bz2 reilua-enhanced-debe4baa8c208458f847dd4c89c17f7cc39be559.zip | |
Bunnymark.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/bunnymark/main.lua | 91 | ||||
| -rw-r--r-- | examples/resources/images/wabbit_alpha.png | bin | 0 -> 561 bytes | |||
| -rw-r--r-- | examples/waving_cubes/main.lua | 10 |
3 files changed, 97 insertions, 4 deletions
diff --git a/examples/bunnymark/main.lua b/examples/bunnymark/main.lua new file mode 100644 index 0000000..50b8f11 --- /dev/null +++ b/examples/bunnymark/main.lua @@ -0,0 +1,91 @@ +local MAX_BUNNIES = 100000 + +-- 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 + +-- Initialization +local screenWidth = 800 +local screenHeight = 450 +local texSize = { 0, 0 } +local texBunny = -1 +local bunnies = {} + +function Bunny:update( texture ) + self.position[1] = self.position[1] + self.speed[1] + self.position[2] = self.position[2] + self.speed[2] + + if ( ( self.position[1] + texSize[1] / 2 ) > screenWidth ) + or ( ( self.position[2] + texSize[1] / 2 ) < 0 ) then + self.speed[1] = self.speed[1] * -1 + end + + if ( ( self.position[2] + texSize[2] / 2 ) > screenHeight ) + or ( ( self.position[2] + texSize[2] / 2 - 40 ) < 0 ) then + self.speed[2] = self.speed[2] * -1 + end +end + +-- Init + +function init() + RL_SetWindowState( FLAG_VSYNC_HINT ) + RL_SetWindowSize( { screenWidth, screenHeight } ) + RL_SetWindowTitle( "raylib [textures] example - bunnymark" ) + -- Load bunny texture + texBunny = RL_LoadTexture( RL_GetBasePath().."../resources/images/wabbit_alpha.png" ) + texSize = RL_GetTextureSize( texBunny ) +end + +-- Update + +function process( delta ) + if RL_IsMouseButtonDown( 0 ) then + -- Create more bunnies + for i = 1, 100 do + if #bunnies < MAX_BUNNIES then + local speed = { math.random( -250, 250 ) / 60, math.random( -250, 250 ) / 60 } + local color = { math.random( 50, 240 ), math.random( 80, 240 ), math.random( 100, 240 ), 255 } + table.insert( bunnies, Bunny:new( RL_GetMousePosition(), speed, color) ) + end + end + end + -- Update bunnies + for i = 1, #bunnies do + bunnies[i]:update( texBunny ) + end +end + + -- Draw + +function draw() + RL_ClearBackground( 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[1], bunnies[i].position[2] }, bunnies[i].color ); + end + + RL_DrawRectangle( { 0, 0, screenWidth, 40 }, BLACK) + RL_DrawText( 0, "bunnies: " .. #bunnies, { 120, 10 }, 20, 2, GREEN ) + RL_DrawText( 0, "batched draw calls: " .. math.ceil( 1 + #bunnies / MAX_BATCH_ELEMENTS ), { 320, 10 }, 20, 2, RED ) + RL_DrawFPS( { 10, 10 } ) +end diff --git a/examples/resources/images/wabbit_alpha.png b/examples/resources/images/wabbit_alpha.png Binary files differnew file mode 100644 index 0000000..1a5eb0b --- /dev/null +++ b/examples/resources/images/wabbit_alpha.png diff --git a/examples/waving_cubes/main.lua b/examples/waving_cubes/main.lua index 8023f67..969dc3c 100644 --- a/examples/waving_cubes/main.lua +++ b/examples/waving_cubes/main.lua @@ -29,13 +29,15 @@ end function draw() local t = RL_GetTime() + local cos = math.cos + local sin = math.sin - local scale = (2.0 + math.sin(t)) * 0.7 + local scale = (2.0 + sin(t)) * 0.7 local camera_time = t * 0.3 local camera_pos = RL_GetCamera3DPosition( camera ) - camera_pos[1] = math.cos(camera_time) * 40.0 - camera_pos[3] = math.sin(camera_time) * 40.0 + camera_pos[1] = cos(camera_time) * 40.0 + camera_pos[3] = sin(camera_time) * 40.0 RL_SetCamera3DPosition( camera, camera_pos ) RL_ClearBackground( RAYWHITE ) @@ -47,7 +49,7 @@ function draw() for y = 0,num_blocks - 1 do for z = 0,num_blocks - 1 do local block_scale = (x + y + z) / 30 - local scatter = math.sin(block_scale * 20.0 + t * 4.0) + local scatter = sin(block_scale * 20.0 + t * 4.0) local cube_pos = { (x - num_blocks / 2) * (scale * 3.0) + scatter, |
