* chg: New build system that uses the officially distributed binaries, bumped version to 8.0.0, simplified git workflow, removed deprecated OpenGL 1.1 functionality. * chg: Modernize CI workflow, enable SourceLink - Bump workflow actions to latest majors (Node 24); drop deprecated softprops/action-gh-release@v1 - Trigger push builds on main instead of master - Create local nuget feed dir before pack (fixes NU1301) - Enable Microsoft.SourceLink.GitHub for debugging symbols (ref PR #340) * fix: centralized version data in Directory.build.props, and fixed various interop details that had incorrect function signatures * chore: updated readme * fix: version the native extract marker and chain download via DependsOnTargets The .extracted marker now includes the raylib package name, so bumping TargetRaylibTag re-extracts the new archive instead of silently keeping (and packing/copying) the previous version's files. _PrepareNativeLibrary and _StageWasmNative now depend directly on _DownloadAndExtractInternal instead of CallTarget-ing it; dependency targets run in the same project instance, so the resolved properties (RaylibPackageName etc.) propagate naturally. * fix: let the binding build for browser-wasm on both net8.0 and net10.0 The net8-era wasm workload (Microsoft.NET.Runtime.WebAssembly.Sdk 8.0.x, auto-imported for RID browser-wasm) treats every browser-wasm project as a wasm app: it forces OutputType=Exe after project evaluation (CS5001 for a classlib) and hooks its app-bundle build after Build, which errors because a library has no assemblies to bundle. Opt Raylib-cs out via DisableAutoWasmBuildApp (props time, before the workload defaults its trigger) and pin OutputType back to Library in Directory.Build.targets (evaluated after the workload props, so the assignment wins). net10's wasm SDK needs neither workaround. * chore: readme updated * chg: simplifying build logic - a simple line in the documentation should save us the code here * fix: Wrong signature of FrameBufferComplete * chore: readme update * feat: samples default to local project reference, and can optionally use the nuget package * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * fix: Examples.csproj runs the download task when building locally * feat: backporting existing raylib-cs examples and new official raylib examples to WASM, adopting raylib's original code style * chore: readme, gitignore, and targets backport. * chore: clean up linter warnings * feat: html harness focuses the example and allows quick navigation with J/K instead. * chore: readme mentions the property to use nuget vs. the local project reference * feat: replaced the J/K navigation with good old HTML buttons * chore: run dotnet format scoped default (was previously scoped to just 'style')
185 lines
6.3 KiB
C#
185 lines
6.3 KiB
C#
/*******************************************************************************************
|
|
*
|
|
* raylib [textures] example - bunnymark
|
|
*
|
|
* Example complexity rating: [★★★☆] 3/4
|
|
*
|
|
* Example originally created with raylib 1.6, last time updated with raylib 2.5
|
|
*
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
|
* BSD-like license that allows static linking with closed source software
|
|
*
|
|
* Copyright (c) 2014-2025 Ramon Santamaria (@raysan5), 2024 Moritz Voss (@thygrrr)
|
|
*
|
|
********************************************************************************************/
|
|
|
|
using System.Diagnostics;
|
|
|
|
namespace Examples.Textures;
|
|
|
|
public partial class Bunnymark : IExample
|
|
{
|
|
private const int screenWidth = 800;
|
|
private const int screenHeight = 450;
|
|
|
|
// 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
|
|
private const int MAX_BATCH_ELEMENTS = Rlgl.DEFAULT_BATCH_BUFFER_ELEMENTS;
|
|
|
|
public string Name => "Textures / Bunnymark";
|
|
|
|
public string Title => "raylib [textures] example - bunnymark";
|
|
|
|
private record struct Bunny()
|
|
{
|
|
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);
|
|
}
|
|
|
|
private Texture2D texBunny;
|
|
private Vector2 halfSize;
|
|
private Bunny[] bunnies;
|
|
private int bunniesCount;
|
|
|
|
public void Init()
|
|
{
|
|
// Load bunny texture
|
|
texBunny = LoadTexture("resources/wabbit_alpha.png");
|
|
halfSize = new Vector2(texBunny.Width, texBunny.Height) / 2;
|
|
|
|
// Initialize bunnies storage
|
|
bunnies = new Bunny[MaxBunnies];
|
|
bunniesCount = 0;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
// Update
|
|
//----------------------------------------------------------------------------------
|
|
Span<Bunny> bunnies = this.bunnies;
|
|
|
|
if (IsMouseButtonDown(MouseButton.Left) && bunniesCount < MaxBunnies)
|
|
{
|
|
// Add a range of new bunnies
|
|
foreach (ref var bunny in bunnies[bunniesCount..(bunniesCount + BunnyIncrement)])
|
|
{
|
|
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
|
|
foreach (ref var bunny in bunnies[..bunniesCount])
|
|
{
|
|
// Integrate position
|
|
bunny.Position += bunny.Speed;
|
|
|
|
// Bounce bunnies off the screen borders
|
|
bunny.Speed *= (bunny.Position + halfSize) switch
|
|
{
|
|
{ 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,
|
|
};
|
|
}
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Draw
|
|
//----------------------------------------------------------------------------------
|
|
BeginDrawing();
|
|
ClearBackground(Color.RayWhite);
|
|
|
|
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
|
|
// 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)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);
|
|
|
|
EndDrawing();
|
|
//----------------------------------------------------------------------------------
|
|
}
|
|
|
|
public void Unload()
|
|
{
|
|
UnloadTexture(texBunny);
|
|
#if BROWSER
|
|
bunnies = null;
|
|
bunniesCount = 0;
|
|
#endif
|
|
}
|
|
|
|
public static int Main()
|
|
{
|
|
// Initialization
|
|
//--------------------------------------------------------------------------------------
|
|
InitWindow(screenWidth, screenHeight, "raylib [textures] example - bunnymark");
|
|
|
|
SetTargetFPS(TARGET_FPS);
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
var game = new Bunnymark();
|
|
game.Init();
|
|
|
|
// Main game loop
|
|
while (!WindowShouldClose())
|
|
{
|
|
game.Update();
|
|
}
|
|
|
|
game.Unload();
|
|
|
|
// De-Initialization
|
|
//--------------------------------------------------------------------------------------
|
|
CloseWindow();
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
return 0;
|
|
}
|
|
|
|
static Bunnymark()
|
|
{
|
|
Debug.Assert(MaxBunnies % BunnyIncrement == 0 && MaxBunnies % BunnyDecrement == 0,
|
|
"MaxBunnies must be a common multiple of BunnyIncrement and BunnyDecrement.");
|
|
}
|
|
}
|