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,134 +1,210 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - smooth pixel-perfect camera
|
||||
* raylib [core] example - smooth pixelperfect
|
||||
*
|
||||
* This example has been created using raylib 3.7 (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.7, last time updated with raylib 4.0
|
||||
*
|
||||
* Example contributed by Giancamillo Alessandroni (@NotManyIdeasDev) and
|
||||
* reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Copyright (c) 2021 Giancamillo Alessandroni (@NotManyIdeasDev) 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) 2021-2025 Giancamillo Alessandroni (@NotManyIdeasDev) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using static Raylib_cs.Raylib;
|
||||
|
||||
namespace Examples.Core;
|
||||
|
||||
public static class SmoothPixelPerfect
|
||||
public partial class SmoothPixelPerfect : IExample
|
||||
{
|
||||
public static int Main()
|
||||
private const int screenWidth = 800;
|
||||
private const int screenHeight = 450;
|
||||
|
||||
private const int virtualScreenWidth = 160;
|
||||
private const int virtualScreenHeight = 90;
|
||||
|
||||
private const float virtualRatio = (float)screenWidth / (float)virtualScreenWidth;
|
||||
|
||||
public string Name => "Core / Smooth Pixelperfect";
|
||||
|
||||
public string Title => "raylib [core] example - smooth pixelperfect";
|
||||
|
||||
private Camera2D worldSpaceCamera; // Game world camera
|
||||
private Camera2D screenSpaceCamera; // Smoothing camera
|
||||
private RenderTexture2D target;
|
||||
|
||||
private Rectangle rec01;
|
||||
private Rectangle rec02;
|
||||
private Rectangle rec03;
|
||||
|
||||
private Rectangle sourceRec;
|
||||
private Rectangle destRec;
|
||||
|
||||
private Vector2 origin;
|
||||
private float rotation;
|
||||
private float cameraX;
|
||||
private float cameraY;
|
||||
private bool smoothOn;
|
||||
private bool overscan;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
const int virtualScreenWidth = 160;
|
||||
const int virtualScreenHeight = 90;
|
||||
|
||||
const float virtualRatio = (float)screenWidth / (float)virtualScreenWidth;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - smooth pixel-perfect camera");
|
||||
|
||||
// Game world camera
|
||||
Camera2D worldSpaceCamera = new();
|
||||
worldSpaceCamera = new(); // Game world camera
|
||||
worldSpaceCamera.Zoom = 1.0f;
|
||||
|
||||
// Smoothing camera
|
||||
Camera2D screenSpaceCamera = new();
|
||||
screenSpaceCamera = new(); // Smoothing camera
|
||||
screenSpaceCamera.Zoom = 1.0f;
|
||||
|
||||
// This is where we'll draw all our objects.
|
||||
RenderTexture2D target = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
|
||||
// Load render texture to draw all our objects
|
||||
target = LoadRenderTexture(virtualScreenWidth, virtualScreenHeight);
|
||||
|
||||
Rectangle rec01 = new(70.0f, 35.0f, 20.0f, 20.0f);
|
||||
Rectangle rec02 = new(90.0f, 55.0f, 30.0f, 10.0f);
|
||||
Rectangle rec03 = new(80.0f, 65.0f, 15.0f, 25.0f);
|
||||
rec01 = new(70.0f, 35.0f, 20.0f, 20.0f);
|
||||
rec02 = new(90.0f, 55.0f, 30.0f, 10.0f);
|
||||
rec03 = new(80.0f, 65.0f, 15.0f, 25.0f);
|
||||
|
||||
// The target's height is flipped (in the source Rectangle), due to OpenGL reasons
|
||||
Rectangle sourceRec = new(
|
||||
sourceRec = new(
|
||||
0.0f,
|
||||
0.0f,
|
||||
(float)target.Texture.Width,
|
||||
-(float)target.Texture.Height
|
||||
);
|
||||
Rectangle destRec = new(
|
||||
-virtualRatio,
|
||||
-virtualRatio,
|
||||
screenWidth + (virtualRatio * 2),
|
||||
screenHeight + (virtualRatio * 2)
|
||||
destRec = new(
|
||||
(screenWidth - screenWidth / 1.25f) / 2.0f,
|
||||
(screenHeight - screenHeight / 1.25f) / 2.0f,
|
||||
screenWidth / 1.25f,
|
||||
screenHeight / 1.25f
|
||||
);
|
||||
|
||||
Vector2 origin = new(0.0f, 0.0f);
|
||||
origin = new(0.0f, 0.0f);
|
||||
|
||||
float rotation = 0.0f;
|
||||
rotation = 0.0f;
|
||||
|
||||
float cameraX = 0.0f;
|
||||
float cameraY = 0.0f;
|
||||
cameraX = 0.0f;
|
||||
cameraY = 0.0f;
|
||||
|
||||
smoothOn = true;
|
||||
overscan = false;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
rotation += 60.0f * GetFrameTime(); // Rotate the rectangles, 60 degrees per second
|
||||
|
||||
// Make the camera move to demonstrate the effect
|
||||
cameraX = (MathF.Sin((float)GetTime()) * 50.0f) - 10.0f;
|
||||
cameraY = MathF.Cos((float)GetTime()) * 30.0f;
|
||||
|
||||
// Set the camera's target to the values computed above
|
||||
screenSpaceCamera.Target = new Vector2(cameraX, cameraY);
|
||||
|
||||
// Round worldSpace coordinates, keep decimals into screenSpace coordinates
|
||||
worldSpaceCamera.Target.X = MathF.Truncate(screenSpaceCamera.Target.X);
|
||||
screenSpaceCamera.Target.X -= worldSpaceCamera.Target.X;
|
||||
screenSpaceCamera.Target.X *= virtualRatio;
|
||||
|
||||
worldSpaceCamera.Target.Y = MathF.Truncate(screenSpaceCamera.Target.Y);
|
||||
screenSpaceCamera.Target.Y -= worldSpaceCamera.Target.Y;
|
||||
screenSpaceCamera.Target.Y *= virtualRatio;
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.S))
|
||||
{
|
||||
smoothOn = !smoothOn;
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KeyboardKey.O))
|
||||
{
|
||||
overscan = !overscan;
|
||||
}
|
||||
|
||||
if (overscan)
|
||||
{
|
||||
destRec = new Rectangle(
|
||||
-virtualRatio,
|
||||
-virtualRatio,
|
||||
screenWidth + (virtualRatio * 2),
|
||||
screenHeight + (virtualRatio * 2)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
destRec = new Rectangle(
|
||||
(screenWidth - screenWidth / 1.25f) / 2.0f,
|
||||
(screenHeight - screenHeight / 1.25f) / 2.0f,
|
||||
screenWidth / 1.25f,
|
||||
screenHeight / 1.25f
|
||||
);
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode2D(worldSpaceCamera);
|
||||
DrawRectanglePro(rec01, origin, rotation, Color.Black);
|
||||
DrawRectanglePro(rec02, origin, -rotation, Color.Red);
|
||||
DrawRectanglePro(rec03, origin, rotation + 45.0f, Color.Blue);
|
||||
EndMode2D();
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.LightGray);
|
||||
|
||||
if (smoothOn)
|
||||
{
|
||||
BeginMode2D(screenSpaceCamera);
|
||||
DrawTexturePro(target.Texture, sourceRec, destRec, origin, 0.0f, Color.White);
|
||||
EndMode2D();
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawTexturePro(target.Texture, sourceRec, destRec, origin, 0.0f, Color.White);
|
||||
}
|
||||
|
||||
DrawText($"Screen resolution: {screenWidth}x{screenHeight}", 10, 10, 20, Color.DarkBlue);
|
||||
DrawText($"World resolution: {virtualScreenWidth}x{virtualScreenHeight}", 10, 40, 20, Color.DarkGreen);
|
||||
DrawText($"Smooth: {(smoothOn ? "ON" : "OFF")}", 10, screenHeight - 60, 20, Color.Red);
|
||||
DrawText($"Overscan: {(overscan ? "ON" : "OFF")}", 10, screenHeight - 30, 20, Color.Red);
|
||||
DrawFPS(GetScreenWidth() - 95, 10);
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
public void Unload()
|
||||
{
|
||||
UnloadRenderTexture(target); // Unload render texture
|
||||
}
|
||||
|
||||
public static int Main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - smooth pixelperfect");
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
var game = new SmoothPixelPerfect();
|
||||
game.Init();
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose())
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
rotation += 60.0f * GetFrameTime(); // Rotate the rectangles, 60 degrees per second
|
||||
|
||||
// Make the camera move to demonstrate the effect
|
||||
cameraX = (MathF.Sin((float)GetTime()) * 50.0f) - 10.0f;
|
||||
cameraY = MathF.Cos((float)GetTime()) * 30.0f;
|
||||
|
||||
// Set the camera's target to the values computed above
|
||||
screenSpaceCamera.Target = new Vector2(cameraX, cameraY);
|
||||
|
||||
// Round worldSpace coordinates, keep decimals into screenSpace coordinates
|
||||
worldSpaceCamera.Target.X = (int)screenSpaceCamera.Target.X;
|
||||
screenSpaceCamera.Target.X -= worldSpaceCamera.Target.X;
|
||||
screenSpaceCamera.Target.X *= virtualRatio;
|
||||
|
||||
worldSpaceCamera.Target.Y = (int)screenSpaceCamera.Target.Y;
|
||||
screenSpaceCamera.Target.Y -= worldSpaceCamera.Target.Y;
|
||||
screenSpaceCamera.Target.Y *= virtualRatio;
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(Color.RayWhite);
|
||||
|
||||
BeginMode2D(worldSpaceCamera);
|
||||
DrawRectanglePro(rec01, origin, rotation, Color.Black);
|
||||
DrawRectanglePro(rec02, origin, -rotation, Color.Red);
|
||||
DrawRectanglePro(rec03, origin, rotation + 45.0f, Color.Blue);
|
||||
EndMode2D();
|
||||
|
||||
EndTextureMode();
|
||||
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.Red);
|
||||
|
||||
BeginMode2D(screenSpaceCamera);
|
||||
DrawTexturePro(target.Texture, sourceRec, destRec, origin, 0.0f, Color.White);
|
||||
EndMode2D();
|
||||
|
||||
DrawText($"Screen resolution: {screenWidth}x{screenHeight}", 10, 10, 20, Color.DarkBlue);
|
||||
DrawText($"World resolution: {virtualScreenWidth}x{virtualScreenHeight}", 10, 40, 20, Color.DarkGreen);
|
||||
DrawFPS(GetScreenWidth() - 95, 10);
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
game.Update();
|
||||
}
|
||||
|
||||
game.Unload();
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadRenderTexture(target);
|
||||
|
||||
CloseWindow();
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue