* 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')
196 lines
6.1 KiB
C#
196 lines
6.1 KiB
C#
/*******************************************************************************************
|
|
*
|
|
* raylib [core] example - 2d camera
|
|
*
|
|
* Example complexity rating: [★★☆☆] 2/4
|
|
*
|
|
* Example originally created with raylib 1.5, last time updated with raylib 3.0
|
|
*
|
|
* 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) 2016-2025 Ramon Santamaria (@raysan5)
|
|
*
|
|
********************************************************************************************/
|
|
|
|
namespace Examples.Core;
|
|
|
|
public partial class Camera2dDemo : IExample
|
|
{
|
|
public const int MaxBuildings = 100;
|
|
|
|
private const int screenWidth = 800;
|
|
private const int screenHeight = 450;
|
|
|
|
public string Name => "Core / Camera 2D Demo";
|
|
|
|
public string Title => "raylib [core] example - 2d camera";
|
|
|
|
private Rectangle player;
|
|
private Rectangle[] buildings;
|
|
private Color[] buildColors;
|
|
private Camera2D camera;
|
|
|
|
public void Init()
|
|
{
|
|
player = new(400, 280, 40, 40);
|
|
buildings = new Rectangle[MaxBuildings];
|
|
buildColors = new Color[MaxBuildings];
|
|
|
|
var spacing = 0;
|
|
|
|
for (var i = 0; i < MaxBuildings; i++)
|
|
{
|
|
buildings[i].Width = GetRandomValue(50, 200);
|
|
buildings[i].Height = GetRandomValue(100, 800);
|
|
buildings[i].Y = screenHeight - 130 - buildings[i].Height;
|
|
buildings[i].X = -6000 + spacing;
|
|
|
|
spacing += (int)buildings[i].Width;
|
|
|
|
buildColors[i] = new Color(
|
|
GetRandomValue(200, 240),
|
|
GetRandomValue(200, 240),
|
|
GetRandomValue(200, 250),
|
|
255
|
|
);
|
|
}
|
|
|
|
camera = new();
|
|
camera.Target = new Vector2(player.X + 20, player.Y + 20);
|
|
camera.Offset = new Vector2(screenWidth / 2.0f, screenHeight / 2.0f);
|
|
camera.Rotation = 0.0f;
|
|
camera.Zoom = 1.0f;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
// Update
|
|
//----------------------------------------------------------------------------------
|
|
// Player movement
|
|
if (IsKeyDown(KeyboardKey.Right))
|
|
{
|
|
player.X += 2;
|
|
}
|
|
else if (IsKeyDown(KeyboardKey.Left))
|
|
{
|
|
player.X -= 2;
|
|
}
|
|
|
|
// Camera target follows player
|
|
camera.Target = new Vector2(player.X + 20, player.Y + 20);
|
|
|
|
// Camera rotation controls
|
|
if (IsKeyDown(KeyboardKey.A))
|
|
{
|
|
camera.Rotation--;
|
|
}
|
|
else if (IsKeyDown(KeyboardKey.S))
|
|
{
|
|
camera.Rotation++;
|
|
}
|
|
|
|
// Limit camera rotation to 80 degrees (-40 to 40)
|
|
if (camera.Rotation > 40)
|
|
{
|
|
camera.Rotation = 40;
|
|
}
|
|
else if (camera.Rotation < -40)
|
|
{
|
|
camera.Rotation = -40;
|
|
}
|
|
|
|
// Camera zoom controls
|
|
// Uses log scaling to provide consistent zoom speed
|
|
camera.Zoom = MathF.Exp(MathF.Log(camera.Zoom) + ((float)GetMouseWheelMove() * 0.1f));
|
|
|
|
if (camera.Zoom > 3.0f)
|
|
{
|
|
camera.Zoom = 3.0f;
|
|
}
|
|
else if (camera.Zoom < 0.1f)
|
|
{
|
|
camera.Zoom = 0.1f;
|
|
}
|
|
|
|
// Camera reset (zoom and rotation)
|
|
if (IsKeyPressed(KeyboardKey.R))
|
|
{
|
|
camera.Zoom = 1.0f;
|
|
camera.Rotation = 0.0f;
|
|
}
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Draw
|
|
//----------------------------------------------------------------------------------
|
|
BeginDrawing();
|
|
ClearBackground(Color.RayWhite);
|
|
|
|
BeginMode2D(camera);
|
|
|
|
DrawRectangle(-6000, 320, 13000, 8000, Color.DarkGray);
|
|
|
|
for (var i = 0; i < MaxBuildings; i++)
|
|
{
|
|
DrawRectangleRec(buildings[i], buildColors[i]);
|
|
}
|
|
|
|
DrawRectangleRec(player, Color.Red);
|
|
|
|
DrawLine((int)camera.Target.X, -screenHeight * 10, (int)camera.Target.X, screenHeight * 10, Color.Green);
|
|
DrawLine(-screenWidth * 10, (int)camera.Target.Y, screenWidth * 10, (int)camera.Target.Y, Color.Green);
|
|
|
|
EndMode2D();
|
|
|
|
DrawText("SCREEN AREA", 640, 10, 20, Color.Red);
|
|
|
|
DrawRectangle(0, 0, screenWidth, 5, Color.Red);
|
|
DrawRectangle(0, 5, 5, screenHeight - 10, Color.Red);
|
|
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, Color.Red);
|
|
DrawRectangle(0, screenHeight - 5, screenWidth, 5, Color.Red);
|
|
|
|
DrawRectangle(10, 10, 250, 113, Fade(Color.SkyBlue, 0.5f));
|
|
DrawRectangleLines(10, 10, 250, 113, Color.Blue);
|
|
|
|
DrawText("Free 2D camera controls:", 20, 20, 10, Color.Black);
|
|
DrawText("- Right/Left to move player", 40, 40, 10, Color.DarkGray);
|
|
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, Color.DarkGray);
|
|
DrawText("- A / S to Rotate", 40, 80, 10, Color.DarkGray);
|
|
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, Color.DarkGray);
|
|
|
|
EndDrawing();
|
|
//----------------------------------------------------------------------------------
|
|
}
|
|
|
|
public void Unload()
|
|
{
|
|
}
|
|
|
|
public static int Main()
|
|
{
|
|
// Initialization
|
|
//--------------------------------------------------------------------------------------
|
|
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
var game = new Camera2dDemo();
|
|
game.Init();
|
|
|
|
// Main game loop
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
{
|
|
game.Update();
|
|
}
|
|
|
|
game.Unload();
|
|
|
|
// De-Initialization
|
|
//--------------------------------------------------------------------------------------
|
|
CloseWindow(); // Close window and OpenGL context
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
return 0;
|
|
}
|
|
}
|