Watch
2
0
Fork
You've already forked raylib-cs
0

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:
tiger tiger tiger 2026-07-30 19:34:34 +02:00 committed by GitHub
commit 8c22e68c2a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
236 changed files with 40405 additions and 10896 deletions

View file

@ -1,63 +1,82 @@
/*******************************************************************************************
*
* raylib [textures] example - Mouse painting
* raylib [textures] example - mouse painting
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 3/4
*
* Example originally created with raylib 3.0, last time updated with raylib 3.0
*
* Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Chris Dill (@MysteriousSpace) and Ramon Santamaria (@raysan5)
* 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) 2019-2025 Chris Dill (@MysteriousSpace) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class MousePainting
public partial class MousePainting : IExample
{
public static int Main()
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Textures / Mouse Painting";
public string Title => "raylib [textures] example - mouse painting";
public int TargetFps => 120;
private Color[] colors;
private Rectangle[] colorsRecs;
private int colorSelected;
private int colorSelectedPrev;
private int colorMouseHover;
private float brushSize;
private bool mouseWasPressed;
private Rectangle btnSaveRec;
private bool btnSaveMouseHover;
private bool showSaveMessage;
private int saveMessageCounter;
private RenderTexture2D target;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting");
// Colours to choose from
Color[] colors = new Color[] {
Color.RayWhite,
Color.Yellow,
Color.Gold,
Color.Orange,
Color.Pink,
Color.Red,
Color.Maroon,
Color.Green,
Color.Lime,
Color.DarkGreen,
Color.SkyBlue,
Color.Blue,
Color.DarkBlue,
Color.Purple,
Color.Violet,
Color.DarkPurple,
Color.Beige,
Color.Brown,
Color.DarkBrown,
Color.LightGray,
Color.Gray,
Color.DarkGray,
Color.Black
};
// Colors to choose from
colors = new Color[] {
Color.RayWhite,
Color.Yellow,
Color.Gold,
Color.Orange,
Color.Pink,
Color.Red,
Color.Maroon,
Color.Green,
Color.Lime,
Color.DarkGreen,
Color.SkyBlue,
Color.Blue,
Color.DarkBlue,
Color.Purple,
Color.Violet,
Color.DarkPurple,
Color.Beige,
Color.Brown,
Color.DarkBrown,
Color.LightGray,
Color.Gray,
Color.DarkGray,
Color.Black
};
// Define colorsRecs data (for every rectangle)
Rectangle[] colorsRecs = new Rectangle[colors.Length];
colorsRecs = new Rectangle[colors.Length];
for (int i = 0; i < colorsRecs.Length; i++)
for (var i = 0; i < colorsRecs.Length; i++)
{
colorsRecs[i].X = 10 + 30 * i + 2 * i;
colorsRecs[i].Y = 10;
@ -65,226 +84,255 @@ public class MousePainting
colorsRecs[i].Height = 30;
}
int colorSelected = 0;
int colorSelectedPrev = colorSelected;
int colorMouseHover = 0;
int brushSize = 20;
colorSelected = 0;
colorSelectedPrev = colorSelected;
colorMouseHover = 0;
brushSize = 20.0f;
mouseWasPressed = false;
Rectangle btnSaveRec = new(750, 10, 40, 30);
bool btnSaveMouseHover = false;
bool showSaveMessage = false;
int saveMessageCounter = 0;
btnSaveRec = new(750, 10, 40, 30);
btnSaveMouseHover = false;
showSaveMessage = false;
saveMessageCounter = 0;
// Create a RenderTexture2D to use as a canvas
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
target = LoadRenderTexture(screenWidth, screenHeight);
// Clear render texture before entering the game loop
BeginTextureMode(target);
ClearBackground(colors[0]);
EndTextureMode();
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
var mousePos = GetMousePosition();
// Move between colors with keys
if (IsKeyPressed(KeyboardKey.Right))
{
colorSelected++;
}
else if (IsKeyPressed(KeyboardKey.Left))
{
colorSelected--;
}
if (colorSelected >= colors.Length)
{
colorSelected = colors.Length - 1;
}
else if (colorSelected < 0)
{
colorSelected = 0;
}
// Choose color with mouse
for (var i = 0; i < colors.Length; i++)
{
if (CheckCollisionPointRec(mousePos, colorsRecs[i]))
{
colorMouseHover = i;
break;
}
else
{
colorMouseHover = -1;
}
}
if ((colorMouseHover >= 0) && IsMouseButtonPressed(MouseButton.Left))
{
colorSelected = colorMouseHover;
colorSelectedPrev = colorSelected;
}
// Change brush size
brushSize += GetMouseWheelMove() * 5;
if (brushSize < 2)
{
brushSize = 2;
}
if (brushSize > 50)
{
brushSize = 50;
}
if (IsKeyPressed(KeyboardKey.C))
{
// Clear render texture to clear color
BeginTextureMode(target);
ClearBackground(colors[0]);
EndTextureMode();
}
if (IsMouseButtonDown(MouseButton.Left) || (GetGestureDetected() == Gesture.Drag))
{
// Paint circle into render texture
// NOTE: To avoid discontinuous circles, we could store
// previous-next mouse points and just draw a line using brush size
BeginTextureMode(target);
if (mousePos.Y > 50)
{
DrawCircle((int)mousePos.X, (int)mousePos.Y, brushSize, colors[colorSelected]);
}
EndTextureMode();
}
if (IsMouseButtonDown(MouseButton.Right))
{
if (!mouseWasPressed)
{
colorSelectedPrev = colorSelected;
colorSelected = 0;
}
mouseWasPressed = true;
// Erase circle from render texture
BeginTextureMode(target);
if (mousePos.Y > 50)
{
DrawCircle((int)mousePos.X, (int)mousePos.Y, brushSize, colors[0]);
}
EndTextureMode();
}
else if (IsMouseButtonReleased(MouseButton.Right) && mouseWasPressed)
{
colorSelected = colorSelectedPrev;
mouseWasPressed = false;
}
// Check mouse hover save button
if (CheckCollisionPointRec(mousePos, btnSaveRec))
{
btnSaveMouseHover = true;
}
else
{
btnSaveMouseHover = false;
}
// Image saving logic
// NOTE: Saving painted texture to a default named image
if ((btnSaveMouseHover && IsMouseButtonReleased(MouseButton.Left)) ||
IsKeyPressed(KeyboardKey.S))
{
var image = LoadImageFromTexture(target.Texture);
ImageFlipVertical(ref image);
ExportImage(image, "my_amazing_texture_painting.png");
UnloadImage(image);
showSaveMessage = true;
}
if (showSaveMessage)
{
// On saving, show a full screen message for 2 seconds
saveMessageCounter++;
if (saveMessageCounter > 240)
{
showSaveMessage = false;
saveMessageCounter = 0;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
Rectangle source = new(0, 0, target.Texture.Width, -target.Texture.Height);
DrawTextureRec(target.Texture, source, new Vector2(0, 0), Color.White);
// Draw drawing circle for reference
if (mousePos.Y > 50)
{
if (IsMouseButtonDown(MouseButton.Right))
{
DrawCircleLines((int)mousePos.X, (int)mousePos.Y, brushSize, Color.Gray);
}
else
{
DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected]);
}
}
// Draw top panel
DrawRectangle(0, 0, GetScreenWidth(), 50, Color.RayWhite);
DrawLine(0, 50, GetScreenWidth(), 50, Color.LightGray);
// Draw color selection rectangles
for (var i = 0; i < colors.Length; i++)
{
DrawRectangleRec(colorsRecs[i], colors[i]);
}
DrawRectangleLines(10, 10, 30, 30, Color.LightGray);
if (colorMouseHover >= 0)
{
DrawRectangleRec(colorsRecs[colorMouseHover], Fade(Color.White, 0.6f));
}
Rectangle rec = new(
colorsRecs[colorSelected].X - 2,
colorsRecs[colorSelected].Y - 2,
colorsRecs[colorSelected].Width + 4,
colorsRecs[colorSelected].Height + 4
);
DrawRectangleLinesEx(rec, 2, Color.Black);
// Draw save image button
DrawRectangleLinesEx(btnSaveRec, 2, btnSaveMouseHover ? Color.Red : Color.Black);
DrawText("SAVE!", 755, 20, 10, btnSaveMouseHover ? Color.Red : Color.Black);
// Draw save image message
if (showSaveMessage)
{
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(Color.RayWhite, 0.8f));
DrawRectangle(0, 150, GetScreenWidth(), 80, Color.Black);
DrawText("IMAGE SAVED!", 150, 180, 20, Color.RayWhite);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadRenderTexture(target); // Unload render texture
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [textures] example - mouse painting");
SetTargetFPS(120); // Set our game to run at 120 frames-per-second
//--------------------------------------------------------------------------------------
var game = new MousePainting();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
Vector2 mousePos = GetMousePosition();
// Move between colors with keys
if (IsKeyPressed(KeyboardKey.Right))
{
colorSelected++;
}
else if (IsKeyPressed(KeyboardKey.Left))
{
colorSelected--;
}
if (colorSelected >= colors.Length)
{
colorSelected = colors.Length - 1;
}
else if (colorSelected < 0)
{
colorSelected = 0;
}
// Choose color with mouse
for (int i = 0; i < colors.Length; i++)
{
if (CheckCollisionPointRec(mousePos, colorsRecs[i]))
{
colorMouseHover = i;
break;
}
else
{
colorMouseHover = -1;
}
}
if ((colorMouseHover >= 0) && IsMouseButtonPressed(MouseButton.Left))
{
colorSelected = colorMouseHover;
colorSelectedPrev = colorSelected;
}
// Change brush size
brushSize += (int)(GetMouseWheelMove() * 5);
if (brushSize < 2)
{
brushSize = 2;
}
if (brushSize > 50)
{
brushSize = 50;
}
if (IsKeyPressed(KeyboardKey.C))
{
// Clear render texture to clear color
BeginTextureMode(target);
ClearBackground(colors[0]);
EndTextureMode();
}
if (IsMouseButtonDown(MouseButton.Left))
{
// Paint circle into render texture
// NOTE: To avoid discontinuous circles, we could store
// previous-next mouse points and just draw a line using brush size
BeginTextureMode(target);
if (mousePos.Y > 50)
{
DrawCircle((int)mousePos.X, (int)mousePos.Y, brushSize, colors[colorSelected]);
}
EndTextureMode();
}
else if (IsMouseButtonDown(MouseButton.Right))
{
colorSelected = 0;
// Erase circle from render texture
BeginTextureMode(target);
if (mousePos.Y > 50)
{
DrawCircle((int)mousePos.X, (int)mousePos.Y, brushSize, colors[0]);
}
EndTextureMode();
}
else
{
colorSelected = colorSelectedPrev;
}
// Check mouse hover save button
if (CheckCollisionPointRec(mousePos, btnSaveRec))
{
btnSaveMouseHover = true;
}
else
{
btnSaveMouseHover = false;
}
// Image saving logic
// NOTE: Saving painted texture to a default named image
if ((btnSaveMouseHover && IsMouseButtonReleased(MouseButton.Left)) ||
IsKeyPressed(KeyboardKey.S))
{
Image image = LoadImageFromTexture(target.Texture);
ImageFlipVertical(ref image);
ExportImage(image, "my_amazing_texture_painting.png");
UnloadImage(image);
showSaveMessage = true;
}
if (showSaveMessage)
{
// On saving, show a full screen message for 2 seconds
saveMessageCounter++;
if (saveMessageCounter > 240)
{
showSaveMessage = false;
saveMessageCounter = 0;
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
Rectangle source = new(0, 0, target.Texture.Width, -target.Texture.Height);
DrawTextureRec(target.Texture, source, new Vector2(0, 0), Color.White);
// Draw drawing circle for reference
if (mousePos.Y > 50)
{
if (IsMouseButtonDown(MouseButton.Right))
{
DrawCircleLines((int)mousePos.X, (int)mousePos.Y, brushSize, colors[colorSelected]);
}
else
{
DrawCircle(GetMouseX(), GetMouseY(), brushSize, colors[colorSelected]);
}
}
// Draw top panel
DrawRectangle(0, 0, GetScreenWidth(), 50, Color.RayWhite);
DrawLine(0, 50, GetScreenWidth(), 50, Color.LightGray);
// Draw color selection rectangles
for (int i = 0; i < colors.Length; i++)
{
DrawRectangleRec(colorsRecs[i], colors[i]);
}
DrawRectangleLines(10, 10, 30, 30, Color.LightGray);
if (colorMouseHover >= 0)
{
DrawRectangleRec(colorsRecs[colorMouseHover], ColorAlpha(Color.White, 0.6f));
}
Rectangle rec = new(
colorsRecs[colorSelected].X - 2,
colorsRecs[colorSelected].Y - 2,
colorsRecs[colorSelected].Width + 4,
colorsRecs[colorSelected].Height + 4
);
DrawRectangleLinesEx(rec, 2, Color.Black);
// Draw save image button
DrawRectangleLinesEx(btnSaveRec, 2, btnSaveMouseHover ? Color.Red : Color.Black);
DrawText("SAVE!", 755, 20, 10, btnSaveMouseHover ? Color.Red : Color.Black);
// Draw save image message
if (showSaveMessage)
{
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), ColorAlpha(Color.RayWhite, 0.8f));
DrawRectangle(0, 150, GetScreenWidth(), 80, Color.Black);
DrawText("IMAGE SAVED: my_amazing_texture_painting.png", 150, 180, 20, Color.RayWhite);
}
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadRenderTexture(target);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;