WASM examples (+ backports of new official examples) (#344)
* 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')
This commit is contained in:
parent
b207e633ae
commit
8c22e68c2a
236 changed files with 40405 additions and 10896 deletions
|
|
@ -1,153 +1,183 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib example - loading thread
|
||||
* raylib [core] example - loading thread
|
||||
*
|
||||
* This example has been created using raylib 2.5 (www.raylib.com)
|
||||
* NOTE: raylib is NOT thread-safe: the loading thread only updates plain data
|
||||
* (progress counter and loaded flag); all raylib calls happen on the main thread.
|
||||
*
|
||||
* Example originally created with raylib 2.5 (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-2025 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System.Threading;
|
||||
using static Raylib_cs.Raylib;
|
||||
using static Raylib_cs.Color;
|
||||
using static Raylib_cs.KeyboardKey;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Examples.Core;
|
||||
|
||||
enum State
|
||||
[ExcludeFromBrowser("System.Threading.Thread is unsupported on single-threaded wasm")]
|
||||
public partial class LoadingThread : IExample
|
||||
{
|
||||
STATE_WAITING,
|
||||
STATE_LOADING,
|
||||
STATE_FINISHED
|
||||
}
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
public class LoadingThread
|
||||
{
|
||||
// C# bool is atomic. Used for synchronization
|
||||
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/variables#atomicity-of-variable-references
|
||||
// Data Loaded completion indicator
|
||||
static bool dataLoaded = false;
|
||||
public string Name => "Core / Loading Thread";
|
||||
|
||||
// Data progress accumulator
|
||||
static int dataProgress = 0;
|
||||
public string Title => "raylib [core] example - loading thread";
|
||||
|
||||
enum State
|
||||
{
|
||||
Waiting,
|
||||
Loading,
|
||||
Finished
|
||||
}
|
||||
|
||||
// Loading data thread; a Thread can only be started once, so a fresh one
|
||||
// is created for every load
|
||||
Thread loadingThread;
|
||||
|
||||
// Data loaded completion indicator; volatile so the main thread sees the
|
||||
// background thread's writes
|
||||
volatile bool dataLoaded;
|
||||
|
||||
// Data progress accumulator (0..500, the progress bar width in pixels)
|
||||
volatile int dataProgress;
|
||||
|
||||
State state;
|
||||
int framesCounter;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
loadingThread = null;
|
||||
dataLoaded = false;
|
||||
dataProgress = 0;
|
||||
|
||||
state = State.Waiting;
|
||||
framesCounter = 0;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
switch (state)
|
||||
{
|
||||
case State.Waiting:
|
||||
if (IsKeyPressed(KeyboardKey.Enter))
|
||||
{
|
||||
loadingThread = new Thread(LoadDataThread) { IsBackground = true };
|
||||
loadingThread.Start();
|
||||
TraceLog(TraceLogLevel.Info, "Loading thread initialized successfully");
|
||||
|
||||
state = State.Loading;
|
||||
}
|
||||
break;
|
||||
|
||||
case State.Loading:
|
||||
framesCounter++;
|
||||
if (dataLoaded)
|
||||
{
|
||||
framesCounter = 0;
|
||||
loadingThread.Join();
|
||||
TraceLog(TraceLogLevel.Info, "Loading thread terminated");
|
||||
|
||||
state = State.Finished;
|
||||
}
|
||||
break;
|
||||
|
||||
case State.Finished:
|
||||
if (IsKeyPressed(KeyboardKey.Enter))
|
||||
{
|
||||
// Reset everything to launch again
|
||||
dataLoaded = false;
|
||||
dataProgress = 0;
|
||||
|
||||
state = State.Waiting;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case State.Waiting:
|
||||
DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, Color.DarkGray);
|
||||
break;
|
||||
|
||||
case State.Loading:
|
||||
DrawRectangle(150, 200, dataProgress, 60, Color.SkyBlue);
|
||||
if ((framesCounter / 15) % 2 == 0)
|
||||
{
|
||||
DrawText("LOADING DATA...", 240, 210, 40, Color.DarkBlue);
|
||||
}
|
||||
break;
|
||||
|
||||
case State.Finished:
|
||||
DrawRectangle(150, 200, 500, 60, Color.Lime);
|
||||
DrawText("DATA LOADED!", 250, 210, 40, Color.Green);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
DrawRectangleLines(150, 200, 500, 60, Color.DarkGray);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - loading thread");
|
||||
|
||||
// Loading data thread id
|
||||
Thread thread = new(new ThreadStart(LoadDataThread));
|
||||
|
||||
State state = State.STATE_WAITING;
|
||||
int framesCounter = 0;
|
||||
|
||||
SetTargetFPS(60);
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new LoadingThread();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
switch (state)
|
||||
{
|
||||
case State.STATE_WAITING:
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER))
|
||||
{
|
||||
thread.Start();
|
||||
//int error = pthread_create(ref, NULL, ref, NULL);
|
||||
//if (error != 0) TraceLog(TraceLogLevel.LOG_ERROR, "Error creating loading thread");
|
||||
//else TraceLog(TraceLogLevel.LOG_INFO, "Loading thread initialized successfully");
|
||||
|
||||
state = State.STATE_LOADING;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case State.STATE_LOADING:
|
||||
{
|
||||
framesCounter++;
|
||||
if (dataLoaded)
|
||||
{
|
||||
framesCounter = 0;
|
||||
state = State.STATE_FINISHED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case State.STATE_FINISHED:
|
||||
{
|
||||
if (IsKeyPressed(KEY_ENTER))
|
||||
{
|
||||
// Reset everything to launch again
|
||||
// atomic_store(ref, false);
|
||||
dataProgress = 0;
|
||||
state = State.STATE_WAITING;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case State.STATE_WAITING:
|
||||
DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY);
|
||||
break;
|
||||
case State.STATE_LOADING:
|
||||
{
|
||||
DrawRectangle(150, 200, dataProgress, 60, SKYBLUE);
|
||||
if ((framesCounter / 15) % 2 == 0) DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE);
|
||||
}
|
||||
break;
|
||||
case State.STATE_FINISHED:
|
||||
{
|
||||
DrawRectangle(150, 200, 500, 60, LIME);
|
||||
DrawText("DATA LOADED!", 250, 210, 40, GREEN);
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
DrawRectangleLines(150, 200, 500, 60, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Loading data thread function definition
|
||||
static void LoadDataThread()
|
||||
void LoadDataThread()
|
||||
{
|
||||
int timeCounter = 0; // Time counted in ms
|
||||
// clock_t prevTime = clock(); // Previous time
|
||||
int timeCounter = 0; // Time counted in ms
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
|
||||
// We simulate data loading with a time counter for 5 seconds
|
||||
while (timeCounter < 5000)
|
||||
{
|
||||
//clock_t currentTime = clock() - prevTime;
|
||||
//timeCounter = currentTime*1000/CLOCKS_PER_SEC;
|
||||
timeCounter += 1;
|
||||
timeCounter = (int)stopwatch.ElapsedMilliseconds;
|
||||
|
||||
// We accumulate time over a global variable to be used in
|
||||
// main thread as a progress bar
|
||||
|
|
@ -158,4 +188,3 @@ public class LoadingThread
|
|||
dataLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue