2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-04-03 11:09:40 -04:00

Bunnymark refactored and cleaned up. (#249)

This commit is contained in:
tiger tiger tiger 2024-06-16 20:08:14 +02:00 committed by GitHub
parent f7fd66b7eb
commit 5cfb0f3b5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -5,28 +5,39 @@
* 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)
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5), 2024 Moritz Voss (@thygrrr)
*
*
********************************************************************************************/
using System.Diagnostics;
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class Bunnymark
public static class Bunnymark
{
public const int MaxBunnies = 150000;
// limits
private const int MaxBunnies = 500_000;
private const int BunnyIncrement = 500;
private const int BunnyDecrement = 2_500;
private const int TARGET_FPS = 60;
// This is the maximum amount of elements (quads) per batch
// NOTE: This value is defined in [rlgl] module and can be changed there
public const int MAX_BATCH_ELEMENTS = 8192;
private const int MAX_BATCH_ELEMENTS = Rlgl.DEFAULT_BATCH_BUFFER_ELEMENTS;
struct Bunny
private record struct Bunny()
{
public Vector2 Position;
public Vector2 Speed;
public Color Color;
public Vector2 Position { get; set; } = GetMousePosition();
public Vector2 Speed { get; set; } = new(
GetRandomValue(-250, 250) / (float)TARGET_FPS,
GetRandomValue(-250, 250) / (float)TARGET_FPS);
public Color Color { get; } = new(
GetRandomValue(50, 240),
GetRandomValue(80, 240),
GetRandomValue(100, 240), 255);
}
public static int Main()
@ -40,12 +51,13 @@ public class Bunnymark
// Load bunny texture
Texture2D texBunny = LoadTexture("resources/wabbit_alpha.png");
Vector2 halfSize = new Vector2(texBunny.Width, texBunny.Height) / 2;
// 50K bunnies limit
Bunny[] bunnies = new Bunny[MaxBunnies];
// Initialize bunnies storage
Span<Bunny> bunnies = new Bunny[MaxBunnies];
int bunniesCount = 0;
SetTargetFPS(60);
SetTargetFPS(TARGET_FPS);
//--------------------------------------------------------------------------------------
// Main game loop
@ -53,45 +65,39 @@ public class Bunnymark
{
// Update
//----------------------------------------------------------------------------------
if (IsMouseButtonDown(MouseButton.Left))
if (IsMouseButtonDown(MouseButton.Left) && bunniesCount < MaxBunnies)
{
// Create more bunnies
for (int i = 0; i < 100; i++)
// Add a range of new bunnies
foreach (ref var bunny in bunnies[bunniesCount..(bunniesCount+BunnyIncrement)])
{
if (bunniesCount < MaxBunnies)
{
bunnies[bunniesCount].Position = GetMousePosition();
bunnies[bunniesCount].Speed.X = (float)GetRandomValue(-250, 250) / 60.0f;
bunnies[bunniesCount].Speed.Y = (float)GetRandomValue(-250, 250) / 60.0f;
bunnies[bunniesCount].Color = new Color(
GetRandomValue(50, 240),
GetRandomValue(80, 240),
GetRandomValue(100, 240), 255
);
bunniesCount++;
}
bunny = new();
}
bunniesCount += BunnyIncrement;
}
else if (IsMouseButtonDown(MouseButton.Right))
{
// Remove the oldest bunnies, shifting them back in the span
if (bunniesCount > BunnyDecrement)
{
bunnies[BunnyDecrement .. bunniesCount].CopyTo(bunnies);
}
bunniesCount = Math.Max(0, bunniesCount - BunnyDecrement);
}
// Update bunnies
Vector2 screen = new(GetScreenWidth(), GetScreenHeight());
Vector2 halfSize = new Vector2(texBunny.Width, texBunny.Height) / 2;
for (int i = 0; i < bunniesCount; i++)
foreach (ref var bunny in bunnies[..bunniesCount])
{
bunnies[i].Position += bunnies[i].Speed;
// Integrate position
bunny.Position += bunny.Speed;
if (((bunnies[i].Position.X + halfSize.X) > screen.X) ||
((bunnies[i].Position.X + halfSize.X) < 0))
// Bounce bunnies off the screen borders
bunny.Speed *= (bunny.Position + halfSize) switch
{
bunnies[i].Speed.X *= -1;
}
if (((bunnies[i].Position.Y + halfSize.Y) > screen.Y) ||
((bunnies[i].Position.Y + halfSize.Y - 40) < 0))
{
bunnies[i].Speed.Y *= -1;
}
{ X: < 0 or > screenWidth, Y: < 40 or > screenHeight} => new(-1, -1),
{ X: < 0 or > screenWidth} => new(-1, 1),
{ Y: < 40 or > screenHeight} => new(1,-1),
_ => Vector2.One,
};
}
//----------------------------------------------------------------------------------
@ -100,20 +106,22 @@ public class Bunnymark
BeginDrawing();
ClearBackground(Color.RayWhite);
for (int i = 0; i < bunniesCount; i++)
foreach (var bunny in bunnies[..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
// 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, (int)bunnies[i].Position.X, (int)bunnies[i].Position.Y, bunnies[i].Color);
// it could generate a stall and consequently a frame drop, limiting the number of drawn bunnies
DrawTexture(texBunny, (int)bunny.Position.X, (int)bunny.Position.Y, bunny.Color);
}
DrawRectangle(0, 0, screenWidth, 40, Color.Black);
DrawText($"bunnies: {bunniesCount}", 120, 10, 20, Color.Green);
DrawText($"batched draw calls: {1 + bunniesCount / MAX_BATCH_ELEMENTS}", 320, 10, 20, Color.Maroon);
DrawText("Left Mouse: Add Bunnies!!! :D", 10, 400, 20, Color.LightGray);
DrawText("Right Mouse: Remove Bunnies", 10, 420, 20, Color.LightGray);
DrawFPS(10, 10);
@ -130,4 +138,10 @@ public class Bunnymark
return 0;
}
static Bunnymark()
{
Debug.Assert(MaxBunnies % BunnyIncrement == 0 && MaxBunnies % BunnyDecrement == 0,
"MaxBunnies must be a common multiple of BunnyIncrement and BunnyDecrement.");
}
}