Skip to content

Instantly share code, notes, and snippets.

@Its-Kenta
Created October 8, 2022 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Its-Kenta/5a42cb025281f252c87a987a184b69c7 to your computer and use it in GitHub Desktop.
Save Its-Kenta/5a42cb025281f252c87a987a184b69c7 to your computer and use it in GitHub Desktop.
import kCore.*
import kEnums.KeyboardKey
import kEnums.MouseButton
import kShapes.drawRectangle
import kShapes.drawRectangleRec
import kShapes.drawRectangleV
import kText.drawFPS
import kText.drawText
import kTextures.drawTexture
import kTextures.loadTexture
import kTextures.unloadTexture
import kTypes.*
import kaylib.*
import kotlinx.cinterop.*
import kotlin.random.Random
import kotlin.random.nextInt
const val SCREEN_WIDTH: Int = 1920
const val SCREEN_HEIGHT: Int = 1080
class Bunny(var position: Vector2, var speed: Vector2)
var MAX_BUNNIES = 500000
var MAX_BATCH_ELEMENTS = 8192
fun main() {
// Initialization
//--------------------------------------------------------------------------------------
initWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - bunnymark")
// Load bunny texture
val texBunny = loadTexture("resources/wabbit_alpha.png")
val bunnies = arrayOfNulls<Bunny>(MAX_BUNNIES)
for (i in 0 until MAX_BUNNIES) {
bunnies[i] = Bunny(kVector2(0F, 0F), kVector2(0F, 0F))
}
var bunniesCount = 0 // Bunnies counter
setTargetFPS(60)
//--------------------------------------------------------------------------------------
// Main game loop
while (!windowShouldClose) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (isMouseButtonDown(MouseButton.LEFT)) {
// Create more bunnies
for (i in 0..99) {
if (bunniesCount < MAX_BUNNIES) {
bunnies[bunniesCount]!!.position = getMousePosition()
bunnies[bunniesCount]!!.speed.x = (Random.nextInt(-250, 250) / 60.0f)
bunnies[bunniesCount]!!.speed.y = (Random.nextInt(-250, 250) / 60.0f)
bunniesCount++
}
}
}
// Update bunnies
for (i in 0 until bunniesCount) {
bunnies[i]!!.position.x += bunnies[i]!!.speed.x
bunnies[i]!!.position.y += bunnies[i]!!.speed.y
if (bunnies[i]!!.position.x + texBunny.width / 2.0 > SCREEN_WIDTH ||
bunnies[i]!!.position.x + texBunny.width / 2.0 < 0
) {
bunnies[i]!!.speed.x *= -1f
}
if (bunnies[i]!!.position.y + texBunny.height / 2.0 > SCREEN_HEIGHT ||
bunnies[i]!!.position.y + texBunny.height / 2.0 - 40 < 0
) {
bunnies[i]!!.speed.y *= -1f
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
beginDrawing()
clearBackground(rayWhite)
for (i in 0 until bunniesCount) {
// 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
drawTexture(
texBunny,
bunnies[i]!!.position.x.toInt(),
bunnies[i]!!.position.y.toInt(),
rayWhite
)
}
drawRectangle(0, 0, SCREEN_WIDTH, 40, black)
drawText("bunnies: $bunniesCount", 120, 10, 20, green)
drawText("batched draw calls: " + 1 + bunniesCount / MAX_BATCH_ELEMENTS, 320, 10, 20, maroon)
drawFPS(10, 10)
endDrawing()
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
unloadTexture(texBunny) // Unload bunny texture
closeWindow() // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment