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,66 +1,85 @@
/*******************************************************************************************
*
* raylib [textures] example - Draw part of the texture tiled
* raylib [textures] example - tiled drawing
*
* This example has been created using raylib 3.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* Example complexity rating: [] 3/4
*
* Copyright (c) 2020 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
* Example originally created with raylib 3.0, last time updated with raylib 4.2
*
* Example contributed by Vlad Adrian (@demizdor) and reviewed by 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) 2020-2025 Vlad Adrian (@demizdor) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Textures;
public class DrawTiled
public partial class DrawTiled : IExample
{
const int OptWidth = 220;
const int MarginSize = 8;
const int ColorSize = 16;
private const int OptWidth = 220;
private const int MarginSize = 8;
private const int ColorSize = 16;
public static int Main()
public string Name => "Textures / Draw Tiled";
public string Title => "raylib [textures] example - tiled drawing";
public ConfigFlags ConfigFlags => ConfigFlags.ResizableWindow;
private int screenWidth;
private int screenHeight;
private Texture2D texPattern;
// Coordinates for all patterns inside the texture
private Rectangle[] recPattern;
// Setup colors
private Color[] colors;
private Rectangle[] colorRec;
private int activePattern;
private int activeCol;
private float scale;
private float rotation;
public void Init()
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
SetConfigFlags(ConfigFlags.ResizableWindow);
InitWindow(screenWidth, screenHeight, "raylib [textures] example - Draw part of a texture tiled");
screenWidth = 800;
screenHeight = 450;
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
Texture2D texPattern = LoadTexture("resources/patterns.png");
// Makes the texture smoother when upscaled
SetTextureFilter(texPattern, TextureFilter.Trilinear);
texPattern = LoadTexture("resources/patterns.png");
SetTextureFilter(texPattern, TextureFilter.Bilinear); // Makes the texture smoother when upscaled
// Coordinates for all patterns inside the texture
Rectangle[] recPattern = new[] {
new Rectangle(3, 3, 66, 66),
new Rectangle(75, 3, 100, 100),
new Rectangle(3, 75, 66, 66),
new Rectangle(7, 156, 50, 50),
new Rectangle(85, 106, 90, 45),
new Rectangle(75, 154, 100, 60)
};
recPattern = new[] {
new Rectangle(3, 3, 66, 66),
new Rectangle(75, 3, 100, 100),
new Rectangle(3, 75, 66, 66),
new Rectangle(7, 156, 50, 50),
new Rectangle(85, 106, 90, 45),
new Rectangle(75, 154, 100, 60)
};
// Setup colors
Color[] colors = new[]
colors = new[]
{
Color.Black,
Color.Maroon,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Beige,
Color.Lime,
Color.Red,
Color.DarkGray,
Color.SkyBlue
};
Rectangle[] colorRec = new Rectangle[colors.Length];
Color.Black,
Color.Maroon,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Beige,
Color.Lime,
Color.Red,
Color.DarkGray,
Color.SkyBlue
};
colorRec = new Rectangle[colors.Length];
// Calculate rectangle for each color
for (int i = 0, x = 0, y = 0; i < colors.Length; i++)
@ -81,155 +100,176 @@ public class DrawTiled
}
}
int activePattern = 0, activeCol = 0;
float scale = 1.0f, rotation = 0.0f;
activePattern = 0;
activeCol = 0;
scale = 1.0f;
rotation = 0.0f;
}
public void Update()
{
// Update
//----------------------------------------------------------------------------------
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
// Handle mouse
if (IsMouseButtonPressed(MouseButton.Left))
{
var mouse = GetMousePosition();
// Check which pattern was clicked and set it as the active pattern
for (var i = 0; i < recPattern.Length; i++)
{
Rectangle rec = new(
2 + MarginSize + recPattern[i].X,
40 + MarginSize + recPattern[i].Y,
recPattern[i].Width,
recPattern[i].Height
);
if (CheckCollisionPointRec(mouse, rec))
{
activePattern = i;
break;
}
}
// Check to see which color was clicked and set it as the active color
for (var i = 0; i < colors.Length; ++i)
{
if (CheckCollisionPointRec(mouse, colorRec[i]))
{
activeCol = i;
break;
}
}
}
// Handle keys: change scale
if (IsKeyPressed(KeyboardKey.Up))
{
scale += 0.25f;
}
if (IsKeyPressed(KeyboardKey.Down))
{
scale -= 0.25f;
}
if (scale > 10.0f)
{
scale = 10.0f;
}
else if (scale <= 0.0f)
{
scale = 0.25f;
}
// Handle keys: change rotation
if (IsKeyPressed(KeyboardKey.Left))
{
rotation -= 25.0f;
}
if (IsKeyPressed(KeyboardKey.Right))
{
rotation += 25.0f;
}
// Handle keys: reset
if (IsKeyPressed(KeyboardKey.Space))
{
rotation = 0.0f;
scale = 1.0f;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw the tiled area
var source = recPattern[activePattern];
Rectangle dest = new(
OptWidth + MarginSize,
MarginSize,
screenWidth - OptWidth - 2 * MarginSize,
screenHeight - 2 * MarginSize
);
DrawTextureTiled(texPattern, source, dest, Vector2.Zero, rotation, scale, colors[activeCol]);
// Draw options
var color = ColorAlpha(Color.LightGray, 0.5f);
DrawRectangle(MarginSize, MarginSize, OptWidth - MarginSize, screenHeight - 2 * MarginSize, color);
DrawText("Select Pattern", 2 + MarginSize, 30 + MarginSize, 10, Color.Black);
DrawTexture(texPattern, 2 + MarginSize, 40 + MarginSize, Color.Black);
DrawRectangle(
2 + MarginSize + (int)recPattern[activePattern].X,
40 + MarginSize + (int)recPattern[activePattern].Y,
(int)recPattern[activePattern].Width,
(int)recPattern[activePattern].Height,
ColorAlpha(Color.DarkBlue, 0.3f)
);
DrawText("Select Color", 2 + MarginSize, 10 + 256 + MarginSize, 10, Color.Black);
for (var i = 0; i < colors.Length; i++)
{
DrawRectangleRec(colorRec[i], colors[i]);
if (activeCol == i)
{
DrawRectangleLinesEx(colorRec[i], 3, ColorAlpha(Color.White, 0.5f));
}
}
DrawText("Scale (UP/DOWN to change)", 2 + MarginSize, 80 + 256 + MarginSize, 10, Color.Black);
DrawText($"{scale:F2}x", 2 + MarginSize, 92 + 256 + MarginSize, 20, Color.Black);
DrawText("Rotation (LEFT/RIGHT to change)", 2 + MarginSize, 122 + 256 + MarginSize, 10, Color.Black);
DrawText($"{rotation:F0} degrees", 2 + MarginSize, 134 + 256 + MarginSize, 20, Color.Black);
DrawText("Press [SPACE] to reset", 2 + MarginSize, 164 + 256 + MarginSize, 10, Color.DarkBlue);
// Draw FPS
DrawText($"{GetFPS()} FPS", 2 + MarginSize, 2 + MarginSize, 20, Color.Black);
EndDrawing();
//----------------------------------------------------------------------------------
}
public void Unload()
{
UnloadTexture(texPattern); // Unload texture
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
SetConfigFlags(ConfigFlags.ResizableWindow); // Make the window resizable
InitWindow(800, 450, "raylib [textures] example - tiled drawing");
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
var game = new DrawTiled();
game.Init();
// Main game loop
while (!WindowShouldClose())
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
// Handle mouse
if (IsMouseButtonPressed(MouseButton.Left))
{
Vector2 mouse = GetMousePosition();
// Check which pattern was clicked and set it as the active pattern
for (int i = 0; i < recPattern.Length; i++)
{
Rectangle rec = new(
2 + MarginSize + recPattern[i].X,
40 + MarginSize + recPattern[i].Y,
recPattern[i].Width,
recPattern[i].Height
);
if (CheckCollisionPointRec(mouse, rec))
{
activePattern = i;
break;
}
}
// Check to see which color was clicked and set it as the active color
for (int i = 0; i < colors.Length; ++i)
{
if (CheckCollisionPointRec(mouse, colorRec[i]))
{
activeCol = i;
break;
}
}
}
// Handle keys
// Change scale
if (IsKeyPressed(KeyboardKey.Up))
{
scale += 0.25f;
}
if (IsKeyPressed(KeyboardKey.Down))
{
scale -= 0.25f;
}
if (scale > 10.0f)
{
scale = 10.0f;
}
else if (scale <= 0.0f)
{
scale = 0.25f;
}
// Change rotation
if (IsKeyPressed(KeyboardKey.Left))
{
rotation -= 25.0f;
}
if (IsKeyPressed(KeyboardKey.Right))
{
rotation += 25.0f;
}
// Reset
if (IsKeyPressed(KeyboardKey.Space))
{
rotation = 0.0f;
scale = 1.0f;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(Color.RayWhite);
// Draw the tiled area
Rectangle source = recPattern[activePattern];
Rectangle dest = new(
OptWidth + MarginSize,
MarginSize,
screenWidth - OptWidth - 2 * MarginSize,
screenHeight - 2 * MarginSize
);
DrawTextureTiled(texPattern, source, dest, Vector2.Zero, rotation, scale, colors[activeCol]);
// Draw options
Color color = ColorAlpha(Color.LightGray, 0.5f);
DrawRectangle(MarginSize, MarginSize, OptWidth - MarginSize, screenHeight - 2 * MarginSize, color);
DrawText("Select Pattern", 2 + MarginSize, 30 + MarginSize, 10, Color.Black);
DrawTexture(texPattern, 2 + MarginSize, 40 + MarginSize, Color.Black);
DrawRectangle(
2 + MarginSize + (int)recPattern[activePattern].X,
40 + MarginSize + (int)recPattern[activePattern].Y,
(int)recPattern[activePattern].Width,
(int)recPattern[activePattern].Height,
ColorAlpha(Color.DarkBlue, 0.3f)
);
DrawText("Select Color", 2 + MarginSize, 10 + 256 + MarginSize, 10, Color.Black);
for (int i = 0; i < colors.Length; i++)
{
DrawRectangleRec(colorRec[i], colors[i]);
if (activeCol == i)
{
DrawRectangleLinesEx(colorRec[i], 3, ColorAlpha(Color.White, 0.5f));
}
}
DrawText("Scale (UP/DOWN to change)", 2 + MarginSize, 80 + 256 + MarginSize, 10, Color.Black);
DrawText($"{scale}x", 2 + MarginSize, 92 + 256 + MarginSize, 20, Color.Black);
DrawText("Rotation (LEFT/RIGHT to change)", 2 + MarginSize, 122 + 256 + MarginSize, 10, Color.Black);
DrawText($"{rotation} degrees", 2 + MarginSize, 134 + 256 + MarginSize, 20, Color.Black);
DrawText("Press [SPACE] to reset", 2 + MarginSize, 164 + 256 + MarginSize, 10, Color.DarkBlue);
// Draw FPS
DrawText($"{GetFPS()}", 2 + MarginSize, 2 + MarginSize, 20, Color.Black);
EndDrawing();
//----------------------------------------------------------------------------------
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texPattern);
CloseWindow();
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// Draw part of a texture (defined by a rectangle) with rotation and scale tiled into dest.
static void DrawTextureTiled(
private static void DrawTextureTiled(
Texture2D texture,
Rectangle source,
Rectangle dest,
@ -268,7 +308,7 @@ public class DrawTiled
else if (dest.Width <= tileWidth)
{
// Tiled vertically (one column)
int dy = 0;
var dy = 0;
for (; dy + tileHeight < dest.Height; dy += tileHeight)
{
DrawTexturePro(
@ -307,7 +347,7 @@ public class DrawTiled
else if (dest.Height <= tileHeight)
{
// Tiled horizontally (one row)
int dx = 0;
var dx = 0;
for (; dx + tileWidth < dest.Width; dx += tileWidth)
{
DrawTexturePro(
@ -351,10 +391,10 @@ public class DrawTiled
else
{
// Tiled both horizontally and vertically (rows and columns)
int dx = 0;
var dx = 0;
for (; dx + tileWidth < dest.Width; dx += tileWidth)
{
int dy = 0;
var dy = 0;
for (; dy + tileHeight < dest.Height; dy += tileHeight)
{
DrawTexturePro(
@ -397,7 +437,7 @@ public class DrawTiled
// Fit last column of tiles
if (dx < dest.Width)
{
int dy = 0;
var dy = 0;
for (; dy + tileHeight < dest.Height; dy += tileHeight)
{
DrawTexturePro(
@ -446,4 +486,3 @@ public class DrawTiled
}
}
}