local v=require("\114\097\121\108\105\098")local M=100000 local O=8192 local U={}U.__index=U function U.new(J,v,M,O)local W={}setmetatable(W,U)W.position=v W.speed=M W.color=O return W end function U.update(O,M)O.position.x=O.position.x+O.speed.x O.position.y=O.position.y+O.speed.y if O.position.x+M.width/2>v.GetScreenWidth()or O.position.x+M.width/2<0 then O.speed.x=O.speed.x*-1 end if O.position.y+M.height/2>v.GetScreenHeight()or(O.position.y+M.height/2)-40<0 then O.speed.y=O.speed.y*-1 end end local J=800 local W=450 v.SetConfigFlags(v.FLAG_VSYNC_HINT)v.InitWindow(J,W,"\114\097\121\108\105\098 \091\116\101\120\116\117\114\101\115\093 \101\120\097\109\112\108\101 - \098\117\110\110\121\109\097\114\107")local K=v.LoadTexture("\101\120\097\109\112\108\101\115\047\114\101\115\111\117\114\099\101\115\047\119\097\098\098\105\116\095\097\108\112\104\097\046\112\110\103")local F={}while not v.WindowShouldClose()do if v.IsMouseButtonDown(v.MOUSE_BUTTON_LEFT)then for O=1,100,1 do if#F<M then local M=v.Vector2(v.GetRandomValue(-250,250)/60,v.GetRandomValue(-250,250)/60)local O=v.Color(v.GetRandomValue(50,240),v.GetRandomValue(80,240),v.GetRandomValue(100,240),255)table.insert(F,U:new(v.GetMousePosition(),M,O))end end end for v=1,#F,1 do F[v]:update(K)end v.BeginDrawing()v.ClearBackground(v.RAYWHITE)for M=1,#F,1 do v.DrawTexture(K,F[M].position.x,F[M].position.y,F[M].color)end v.DrawRectangle(0,0,J,40,v.BLACK)v.DrawText("\098\117\110\110\105\101\115\058 "..#F,120,10,20,v.GREEN)v.DrawText("\098\097\116\099\104\101\100 \100\114\097\119 \099\097\108\108\115\058 "..math.ceil(1+#F/O),320,10,20,v.MAROON)v.DrawFPS(10,10)v.EndDrawing()end v.UnloadTexture(K)v.CloseWindow()
local rl = require("raylib")

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

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.SetConfigFlags(rl.FLAG_VSYNC_HINT)
rl.InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark")

-- Load bunny texture
local texBunny = rl.LoadTexture("examples/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_BUTTON_LEFT) then
    -- Create more bunnies
    for i = 1, 100 do
      if #bunnies < MAX_BUNNIES then
        local speed = rl.Vector2(rl.GetRandomValue(-250, 250) / 60, rl.GetRandomValue(-250, 250) / 60)
        local color = rl.Color(rl.GetRandomValue(50, 240), rl.GetRandomValue(80, 240), rl.GetRandomValue(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

  -- 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)
rl.CloseWindow()
