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,139 +1,169 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - window scale letterbox
|
||||
* raylib [core] example - window letterbox
|
||||
*
|
||||
* 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: [★★☆☆] 2/4
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 4.0
|
||||
*
|
||||
* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Copyright (c) 2019 Anata (@anatagawa) 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 Anata (@anatagawa) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Core;
|
||||
|
||||
public class WindowLetterbox
|
||||
public partial class WindowLetterbox : IExample
|
||||
{
|
||||
public static int Main()
|
||||
private const int windowWidth = 800;
|
||||
private const int windowHeight = 450;
|
||||
|
||||
private const int gamescreenWidth = 640;
|
||||
private const int gamescreenHeight = 480;
|
||||
|
||||
public string Name => "Core / Window Letterbox";
|
||||
|
||||
public string Title => "raylib [core] example - window letterbox";
|
||||
|
||||
public ConfigFlags ConfigFlags => ConfigFlags.ResizableWindow | ConfigFlags.VSyncHint;
|
||||
|
||||
private RenderTexture2D target;
|
||||
private Color[] colors;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
const int windowWidth = 800;
|
||||
const int windowHeight = 450;
|
||||
|
||||
// Enable config flags for resizable window and vertical synchro
|
||||
SetConfigFlags(ConfigFlags.ResizableWindow | ConfigFlags.VSyncHint);
|
||||
InitWindow(windowWidth, windowHeight, "raylib [core] example - window scale letterbox");
|
||||
SetWindowMinSize(320, 240);
|
||||
|
||||
int gameScreenWidth = 640;
|
||||
int gameScreenHeight = 480;
|
||||
|
||||
// Render texture initialization, used to hold the rendering result so we can easily resize it
|
||||
RenderTexture2D target = LoadRenderTexture(gameScreenWidth, gameScreenHeight);
|
||||
SetTextureFilter(target.Texture, TextureFilter.Bilinear);
|
||||
target = LoadRenderTexture(gamescreenWidth, gamescreenHeight);
|
||||
SetTextureFilter(target.Texture, TextureFilter.Bilinear); // Texture scale filter to use
|
||||
|
||||
Color[] colors = new Color[10];
|
||||
for (int i = 0; i < 10; i++)
|
||||
colors = new Color[10];
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
colors[i] = new Color(GetRandomValue(100, 250), GetRandomValue(50, 150), GetRandomValue(10, 100), 255);
|
||||
}
|
||||
}
|
||||
|
||||
SetTargetFPS(60);
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Compute required framebuffer scaling
|
||||
var scale = MathF.Min(
|
||||
(float)GetScreenWidth() / gamescreenWidth,
|
||||
(float)GetScreenHeight() / gamescreenHeight
|
||||
);
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.Space))
|
||||
{
|
||||
// Recalculate random colors for the bars
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
colors[i] = new Color(
|
||||
GetRandomValue(100, 250),
|
||||
GetRandomValue(50, 150),
|
||||
GetRandomValue(10, 100),
|
||||
255
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Update virtual mouse (clamped mouse value behind game screen)
|
||||
var mouse = GetMousePosition();
|
||||
var virtualMouse = Vector2.Zero;
|
||||
virtualMouse.X = (mouse.X - (GetScreenWidth() - (gamescreenWidth * scale)) * 0.5f) / scale;
|
||||
virtualMouse.Y = (mouse.Y - (GetScreenHeight() - (gamescreenHeight * scale)) * 0.5f) / scale;
|
||||
|
||||
Vector2 max = new((float)gamescreenWidth, (float)gamescreenHeight);
|
||||
virtualMouse = Vector2.Clamp(virtualMouse, Vector2.Zero, max);
|
||||
|
||||
// Apply the same transformation as the virtual mouse to the real mouse (i.e. to work with raygui)
|
||||
//SetMouseOffset(-(GetScreenWidth() - (gamescreenWidth*scale))*0.5f, -(GetScreenHeight() - (gamescreenHeight*scale))*0.5f);
|
||||
//SetMouseScale(1/scale, 1/scale);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
// Draw everything in the render texture, note this will not be rendered on screen, yet
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(Color.RayWhite); // Clear render texture background color
|
||||
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
DrawRectangle(0, (gamescreenHeight / 10) * i, gamescreenWidth, gamescreenHeight / 10, colors[i]);
|
||||
}
|
||||
|
||||
DrawText(
|
||||
"If executed inside a window,\nyou can resize the window,\nand see the screen scaling!",
|
||||
10,
|
||||
25,
|
||||
20,
|
||||
Color.White
|
||||
);
|
||||
|
||||
DrawText($"Default Mouse: [{(int)mouse.X} , {(int)mouse.Y}]", 350, 25, 20, Color.Green);
|
||||
DrawText($"Virtual Mouse: [{(int)virtualMouse.X} , {(int)virtualMouse.Y}]", 350, 55, 20, Color.Yellow);
|
||||
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.Black); // Clear screen background
|
||||
|
||||
// Draw render texture to screen, properly scaled
|
||||
Rectangle sourceRec = new(
|
||||
0.0f,
|
||||
0.0f,
|
||||
(float)target.Texture.Width,
|
||||
(float)-target.Texture.Height
|
||||
);
|
||||
Rectangle destRec = new(
|
||||
(GetScreenWidth() - ((float)gamescreenWidth * scale)) * 0.5f,
|
||||
(GetScreenHeight() - ((float)gamescreenHeight * scale)) * 0.5f,
|
||||
(float)gamescreenWidth * scale,
|
||||
(float)gamescreenHeight * scale
|
||||
);
|
||||
DrawTexturePro(target.Texture, sourceRec, destRec, new Vector2(0, 0), 0.0f, Color.White);
|
||||
|
||||
EndDrawing();
|
||||
//--------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadRenderTexture(target); // Unload render texture
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Enable config flags for resizable window and vertical synchro
|
||||
SetConfigFlags(ConfigFlags.ResizableWindow | ConfigFlags.VSyncHint);
|
||||
InitWindow(windowWidth, windowHeight, "raylib [core] example - window letterbox");
|
||||
SetWindowMinSize(320, 240);
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new WindowLetterbox();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Compute required framebuffer scaling
|
||||
float scale = MathF.Min(
|
||||
(float)GetScreenWidth() / gameScreenWidth,
|
||||
(float)GetScreenHeight() / gameScreenHeight
|
||||
);
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.Space))
|
||||
{
|
||||
// Recalculate random colors for the bars
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
colors[i] = new Color(
|
||||
GetRandomValue(100, 250),
|
||||
GetRandomValue(50, 150),
|
||||
GetRandomValue(10, 100),
|
||||
255
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Update virtual mouse (clamped mouse value behind game screen)
|
||||
Vector2 mouse = GetMousePosition();
|
||||
Vector2 virtualMouse = Vector2.Zero;
|
||||
virtualMouse.X = (mouse.X - (GetScreenWidth() - (gameScreenWidth * scale)) * 0.5f) / scale;
|
||||
virtualMouse.Y = (mouse.Y - (GetScreenHeight() - (gameScreenHeight * scale)) * 0.5f) / scale;
|
||||
|
||||
Vector2 max = new((float)gameScreenWidth, (float)gameScreenHeight);
|
||||
virtualMouse = Vector2.Clamp(virtualMouse, Vector2.Zero, max);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.Black);
|
||||
|
||||
// Draw everything in the render texture, note this will not be rendered on screen, yet
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
DrawRectangle(0, (gameScreenHeight / 10) * i, gameScreenWidth, gameScreenHeight / 10, colors[i]);
|
||||
}
|
||||
|
||||
DrawText(
|
||||
"If executed inside a window,\nyou can resize the window,\nand see the screen scaling!",
|
||||
10,
|
||||
25,
|
||||
20,
|
||||
Color.White
|
||||
);
|
||||
|
||||
DrawText($"Default Mouse: [{(int)mouse.X} {(int)mouse.Y}]", 350, 25, 20, Color.Green);
|
||||
DrawText($"Virtual Mouse: [{(int)virtualMouse.X}, {(int)virtualMouse.Y}]", 350, 55, 20, Color.Yellow);
|
||||
|
||||
EndTextureMode();
|
||||
|
||||
// Draw RenderTexture2D to window, properly scaled
|
||||
Rectangle sourceRec = new(
|
||||
0.0f,
|
||||
0.0f,
|
||||
(float)target.Texture.Width,
|
||||
(float)-target.Texture.Height
|
||||
);
|
||||
Rectangle destRec = new(
|
||||
(GetScreenWidth() - ((float)gameScreenWidth * scale)) * 0.5f,
|
||||
(GetScreenHeight() - ((float)gameScreenHeight * scale)) * 0.5f,
|
||||
(float)gameScreenWidth * scale,
|
||||
(float)gameScreenHeight * scale
|
||||
);
|
||||
DrawTexturePro(target.Texture, sourceRec, destRec, new Vector2(0, 0), 0.0f, Color.White);
|
||||
|
||||
EndDrawing();
|
||||
//--------------------------------------------------------------------------------------
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadRenderTexture(target);
|
||||
|
||||
CloseWindow();
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue