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

chore: rebase to main with translation ports

This commit is contained in:
tiger tiger tiger 2026-07-17 07:49:07 +02:00
commit 8024c6ac40
134 changed files with 15456 additions and 10650 deletions

View file

@ -7,4 +7,20 @@
<PropertyGroup Condition="'$(MSBuildProjectName)' == 'Raylib-cs' And '$(RuntimeIdentifier)' == 'browser-wasm'"> <PropertyGroup Condition="'$(MSBuildProjectName)' == 'Raylib-cs' And '$(RuntimeIdentifier)' == 'browser-wasm'">
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
</PropertyGroup> </PropertyGroup>
<!-- Raylib-cs 8.1.0 is not on nuget.org yet; pack locally so Examples and tests resolve the package with native runtimes. -->
<Target Name="_EnsureLocalRaylibCsPackage"
BeforeTargets="Restore"
Condition="'$(MSBuildProjectName)' != 'Raylib-cs' And Exists('$(MSBuildThisFileDirectory)Raylib-cs/Raylib-cs.csproj')">
<PropertyGroup>
<_LocalNuGetDir>$(MSBuildThisFileDirectory)nuget</_LocalNuGetDir>
<_LocalRaylibCsNupkg>$(_LocalNuGetDir)/Raylib-cs.$(RaylibCsVersion).nupkg</_LocalRaylibCsNupkg>
</PropertyGroup>
<MakeDir Directories="$(_LocalNuGetDir)" Condition="!Exists('$(_LocalNuGetDir)')"/>
<MSBuild Projects="$(MSBuildThisFileDirectory)Raylib-cs/Raylib-cs.csproj"
Targets="Restore;Pack"
Properties="Configuration=Release;PackageOutputPath=$(_LocalNuGetDir)"
RemoveProperties="RuntimeIdentifier;RuntimeIdentifiers;SelfContained;_IsPublishing"
Condition="!Exists('$(_LocalRaylibCsNupkg)') Or '$(ForceRaylibCsPack)' == 'true'"/>
</Target>
</Project> </Project>

View file

@ -1,13 +1,15 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [audio] example - Module playing (streaming) * raylib [audio] example - module playing
* *
* NOTE: This example requires OpenAL Soft library installed * Example complexity rating: [★☆☆☆] 1/4
* *
* This example has been created using raylib 1.5 (www.raylib.com) * Example originally created with raylib 1.5, last time updated with raylib 3.5
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2016 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) 2016-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -16,11 +18,20 @@ using static Raylib_cs.Raylib;
namespace Examples.Audio; namespace Examples.Audio;
public class ModulePlaying public partial class ModulePlaying : IExample
{ {
const int MaxCircles = 64; private const int screenWidth = 800;
private const int screenHeight = 450;
struct CircleWave private const int MaxCircles = 64;
public string Name => "Audio / Module Playing";
public string Title => "raylib [audio] example - module playing";
public ConfigFlags ConfigFlags => ConfigFlags.Msaa4xHint;
private struct CircleWave
{ {
public Vector2 Position; public Vector2 Position;
public float Radius; public float Radius;
@ -29,20 +40,18 @@ public class ModulePlaying
public Color Color; public Color Color;
} }
public static int Main() private Color[] colors;
private CircleWave[] circles;
private Music music;
private float pitch;
private float timePlayed;
private bool pause;
public void Init()
{ {
// Initialization InitAudioDevice(); // Initialize audio device
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(ConfigFlags.Msaa4xHint); // NOTE: Try to enable MSAA 4X colors = new Color[14] {
InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing (streaming)");
InitAudioDevice();
Color[] colors = new Color[14] {
Color.Orange, Color.Orange,
Color.Red, Color.Red,
Color.Gold, Color.Gold,
@ -59,33 +68,30 @@ public class ModulePlaying
Color.Beige Color.Beige
}; };
// Creates ome circles for visual effect // Creates some circles for visual effect
CircleWave[] circles = new CircleWave[MaxCircles]; circles = new CircleWave[MaxCircles];
for (int i = MaxCircles - 1; i >= 0; i--) for (var i = MaxCircles - 1; i >= 0; i--)
{ {
circles[i].Alpha = 0.0f; circles[i].Alpha = 0.0f;
circles[i].Radius = GetRandomValue(10, 40); circles[i].Radius = GetRandomValue(10, 40);
circles[i].Position.X = GetRandomValue((int)circles[i].Radius, screenWidth - (int)circles[i].Radius); circles[i].Position.X = GetRandomValue((int)circles[i].Radius, screenWidth - (int)circles[i].Radius);
circles[i].Position.Y = GetRandomValue((int)circles[i].Radius, screenHeight - (int)circles[i].Radius); circles[i].Position.Y = GetRandomValue((int)circles[i].Radius, screenHeight - (int)circles[i].Radius);
circles[i].Speed = (float)GetRandomValue(1, 100) / 20000.0f; circles[i].Speed = (float)GetRandomValue(1, 100) / 2000.0f;
circles[i].Color = colors[GetRandomValue(0, 13)]; circles[i].Color = colors[GetRandomValue(0, 13)];
} }
Music music = LoadMusicStream("resources/audio/mini1111.xm"); music = LoadMusicStream("resources/audio/mini1111.xm");
music.Looping = false; music.Looping = false;
float pitch = 1.0f; pitch = 1.0f;
PlayMusicStream(music); PlayMusicStream(music);
float timePlayed = 0.0f; timePlayed = 0.0f;
bool pause = false; pause = false;
}
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -128,7 +134,7 @@ public class ModulePlaying
timePlayed = GetMusicTimePlayed(music) / GetMusicTimeLength(music) * (screenWidth - 40); timePlayed = GetMusicTimePlayed(music) / GetMusicTimeLength(music) * (screenWidth - 40);
// Color circles animation // Color circles animation
for (int i = MaxCircles - 1; (i >= 0) && !pause; i--) for (var i = MaxCircles - 1; (i >= 0) && !pause; i--)
{ {
circles[i].Alpha += circles[i].Speed; circles[i].Alpha += circles[i].Speed;
circles[i].Radius += circles[i].Speed * 10.0f; circles[i].Radius += circles[i].Speed * 10.0f;
@ -161,12 +167,12 @@ public class ModulePlaying
BeginDrawing(); BeginDrawing();
ClearBackground(Color.RayWhite); ClearBackground(Color.RayWhite);
for (int i = MaxCircles - 1; i >= 0; i--) for (var i = MaxCircles - 1; i >= 0; i--)
{ {
DrawCircleV( DrawCircleV(
circles[i].Position, circles[i].Position,
circles[i].Radius, circles[i].Radius,
ColorAlpha(circles[i].Color, circles[i].Alpha) Fade(circles[i].Color, circles[i].Alpha)
); );
} }
@ -175,17 +181,50 @@ public class ModulePlaying
DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, Color.Maroon); DrawRectangle(20, screenHeight - 20 - 12, (int)timePlayed, 12, Color.Maroon);
DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, Color.Gray); DrawRectangleLines(20, screenHeight - 20 - 12, screenWidth - 40, 12, Color.Gray);
// Draw help instructions
DrawRectangle(20, 20, 425, 145, Color.White);
DrawRectangleLines(20, 20, 425, 145, Color.Gray);
DrawText("PRESS SPACE TO RESTART MUSIC", 40, 40, 20, Color.Black);
DrawText("PRESS P TO PAUSE/RESUME", 40, 70, 20, Color.Black);
DrawText("PRESS UP/DOWN TO CHANGE SPEED", 40, 100, 20, Color.Black);
DrawText($"SPEED: {pitch:F6}", 40, 130, 20, Color.Maroon);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
UnloadMusicStream(music); // Unload music stream buffers from RAM
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
SetConfigFlags(ConfigFlags.Msaa4xHint); // NOTE: Try to enable MSAA 4X
InitWindow(screenWidth, screenHeight, "raylib [audio] example - module playing");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new ModulePlaying();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadMusicStream(music); CloseWindow(); // Close window and OpenGL context
CloseAudioDevice();
CloseWindow();
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,13 +1,15 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [audio] example - IntPtr playing (streaming) * raylib [audio] example - music stream
* *
* NOTE: This example requires OpenAL Soft library installed * Example complexity rating: [★☆☆☆] 1/4
* *
* This example has been created using raylib 1.3 (www.raylib.com) * Example originally created with raylib 1.3, last time updated with raylib 4.2
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2015 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) 2015-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -15,29 +17,42 @@ using static Raylib_cs.Raylib;
namespace Examples.Audio; namespace Examples.Audio;
public class MusicStreamDemo public partial class MusicStreamDemo : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Audio / Music Stream Demo";
public string Title => "raylib [audio] example - music stream";
public int TargetFps => 30;
private Music music;
private float timePlayed;
private bool pause;
private float pan;
private float volume;
public void Init()
{ {
// Initialization InitAudioDevice(); // Initialize audio device
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [audio] example - music playing (streaming)"); music = LoadMusicStream("resources/audio/country.mp3");
InitAudioDevice();
Music music = LoadMusicStream("resources/audio/country.mp3");
PlayMusicStream(music); PlayMusicStream(music);
float timePlayed = 0.0f; timePlayed = 0.0f; // Time played normalized [0.0f..1.0f]
bool pause = false; pause = false; // Music playing paused
SetTargetFPS(60); pan = 0.0f; // Default audio pan center [-1.0f..1.0f]
//-------------------------------------------------------------------------------------- SetMusicPan(music, pan);
// Main game loop volume = 0.8f; // Default audio volume [0.0f..1.0f]
while (!WindowShouldClose()) SetMusicVolume(music, volume);
}
public void Update()
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -65,12 +80,52 @@ public class MusicStreamDemo
} }
} }
// Get timePlayed scaled to bar dimensions (400 pixels) // Set audio pan
timePlayed = GetMusicTimePlayed(music) / GetMusicTimeLength(music) * 400; if (IsKeyDown(KeyboardKey.Left))
if (timePlayed > 400)
{ {
StopMusicStream(music); pan -= 0.05f;
if (pan < -1.0f)
{
pan = -1.0f;
}
SetMusicPan(music, pan);
}
else if (IsKeyDown(KeyboardKey.Right))
{
pan += 0.05f;
if (pan > 1.0f)
{
pan = 1.0f;
}
SetMusicPan(music, pan);
}
// Set audio volume
if (IsKeyDown(KeyboardKey.Down))
{
volume -= 0.05f;
if (volume < 0.0f)
{
volume = 0.0f;
}
SetMusicVolume(music, volume);
}
else if (IsKeyDown(KeyboardKey.Up))
{
volume += 0.05f;
if (volume > 1.0f)
{
volume = 1.0f;
}
SetMusicVolume(music, volume);
}
// Get normalized time played for current music stream
timePlayed = GetMusicTimePlayed(music) / GetMusicTimeLength(music);
if (timePlayed > 1.0f)
{
timePlayed = 1.0f; // Make sure time played is no longer than music
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -81,24 +136,57 @@ public class MusicStreamDemo
DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, Color.LightGray); DrawText("MUSIC SHOULD BE PLAYING!", 255, 150, 20, Color.LightGray);
DrawText("LEFT-RIGHT for PAN CONTROL", 320, 74, 10, Color.DarkBlue);
DrawRectangle(300, 100, 200, 12, Color.LightGray);
DrawRectangleLines(300, 100, 200, 12, Color.Gray);
DrawRectangle((int)(300 + (pan + 1.0f) / 2.0f * 200 - 5), 92, 10, 28, Color.DarkGray);
DrawRectangle(200, 200, 400, 12, Color.LightGray); DrawRectangle(200, 200, 400, 12, Color.LightGray);
DrawRectangle(200, 200, (int)timePlayed, 12, Color.Maroon); DrawRectangle(200, 200, (int)(timePlayed * 400.0f), 12, Color.Maroon);
DrawRectangleLines(200, 200, 400, 12, Color.Gray); DrawRectangleLines(200, 200, 400, 12, Color.Gray);
DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, Color.LightGray); DrawText("PRESS SPACE TO RESTART MUSIC", 215, 250, 20, Color.LightGray);
DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, Color.LightGray); DrawText("PRESS P TO PAUSE/RESUME MUSIC", 208, 280, 20, Color.LightGray);
DrawText("UP-DOWN for VOLUME CONTROL", 320, 334, 10, Color.DarkGreen);
DrawRectangle(300, 360, 200, 12, Color.LightGray);
DrawRectangleLines(300, 360, 200, 12, Color.Gray);
DrawRectangle((int)(300 + volume * 200 - 5), 352, 10, 28, Color.DarkGray);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
UnloadMusicStream(music); // Unload music stream buffers from RAM
CloseAudioDevice(); // Close audio device (music streaming is automatically stopped)
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [audio] example - music stream");
SetTargetFPS(30); // Set our game to run at 30 frames-per-second
//--------------------------------------------------------------------------------------
var game = new MusicStreamDemo();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadMusicStream(music); CloseWindow(); // Close window and OpenGL context
CloseAudioDevice();
CloseWindow();
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,13 +1,15 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [audio] example - Sound loading and playing * raylib [audio] example - sound loading
* *
* NOTE: This example requires OpenAL Soft library installed * Example complexity rating: [★☆☆☆] 1/4
* *
* This example has been created using raylib 1.0 (www.raylib.com) * Example originally created with raylib 1.1, last time updated with raylib 3.5
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2014 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) 2014-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -15,37 +17,38 @@ using static Raylib_cs.Raylib;
namespace Examples.Audio; namespace Examples.Audio;
public class SoundLoading public partial class SoundLoading : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Audio / Sound Loading";
public string Title => "raylib [audio] example - sound loading";
private Sound fxWav;
private Sound fxOgg;
public void Init()
{ {
// Initialization InitAudioDevice(); // Initialize audio device
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [audio] example - sound loading and playing"); fxWav = LoadSound("resources/audio/sound.wav"); // Load WAV audio file
InitAudioDevice(); fxOgg = LoadSound("resources/audio/target.ogg"); // Load OGG audio file
}
Sound fxWav = LoadSound("resources/audio/sound.wav"); public void Update()
Sound fxOgg = LoadSound("resources/audio/target.ogg");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KeyboardKey.Space)) if (IsKeyPressed(KeyboardKey.Space))
{ {
PlaySound(fxWav); PlaySound(fxWav); // Play WAV sound
} }
if (IsKeyPressed(KeyboardKey.Enter)) if (IsKeyPressed(KeyboardKey.Enter))
{ {
PlaySound(fxOgg); PlaySound(fxOgg); // Play OGG sound
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -61,14 +64,37 @@ public class SoundLoading
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
UnloadSound(fxWav); // Unload sound data
UnloadSound(fxOgg); // Unload sound data
CloseAudioDevice(); // Close audio device
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [audio] example - sound loading");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new SoundLoading();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadSound(fxWav); CloseWindow(); // Close window and OpenGL context
UnloadSound(fxOgg);
CloseAudioDevice();
CloseWindow();
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,21 +1,17 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [core] example - Basic window * raylib [core] example - basic screen manager
* *
* Welcome to raylib! * Example complexity rating: [★☆☆☆] 1/4
* *
* To test examples, just press F6 and execute raylib_compile_execute script * NOTE: This example illustrates a very simple screen manager based on a states machines
* Note that compiled executable is placed in the same folder as .c file
* *
* You can find all basic examples on C:\raylib\raylib\examples folder or * Example originally created with raylib 4.0, last time updated with raylib 4.0
* raylib official webpage: www.raylib.com
* *
* Enjoy using raylib. :) * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
* *
* This example has been created using raylib 1.0 (www.raylib.com) * Copyright (c) 2021-2025 Ramon Santamaria (@raysan5)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -23,7 +19,7 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
enum GameScreen internal enum GameScreen
{ {
Logo = 0, Logo = 0,
Title, Title,
@ -31,29 +27,28 @@ enum GameScreen
Ending Ending
} }
public class BasicScreenManager public partial class BasicScreenManager : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Basic Screen Manager";
public string Title => "raylib [core] example - basic screen manager";
private GameScreen currentScreen;
private int framesCounter;
public void Init()
{ {
// Initialization currentScreen = GameScreen.Logo;
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic screen manager");
GameScreen currentScreen = GameScreen.Logo;
// TODO: Initialize all required variables and load all required data here! // TODO: Initialize all required variables and load all required data here!
// Useful to count frames framesCounter = 0; // Useful to count frames
int framesCounter = 0; }
SetTargetFPS(60); // Set desired framerate (frames-per-second) public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -63,8 +58,7 @@ public class BasicScreenManager
{ {
// TODO: Update LOGO screen variables here! // TODO: Update LOGO screen variables here!
// Count frames framesCounter++; // Count frames
framesCounter++;
// Wait for 2 seconds (120 frames) before jumping to TITLE screen // Wait for 2 seconds (120 frames) before jumping to TITLE screen
if (framesCounter > 120) if (framesCounter > 120)
@ -124,7 +118,6 @@ public class BasicScreenManager
// TODO: Draw LOGO screen here! // TODO: Draw LOGO screen here!
DrawText("LOGO SCREEN", 20, 20, 40, Color.LightGray); DrawText("LOGO SCREEN", 20, 20, 40, Color.LightGray);
DrawText("WAIT for 2 SECONDS...", 290, 220, 20, Color.Gray); DrawText("WAIT for 2 SECONDS...", 290, 220, 20, Color.Gray);
} }
break; break;
case GameScreen.Title: case GameScreen.Title:
@ -133,7 +126,6 @@ public class BasicScreenManager
DrawRectangle(0, 0, screenWidth, screenHeight, Color.Green); DrawRectangle(0, 0, screenWidth, screenHeight, Color.Green);
DrawText("TITLE SCREEN", 20, 20, 40, Color.DarkGreen); DrawText("TITLE SCREEN", 20, 20, 40, Color.DarkGreen);
DrawText("PRESS ENTER or TAP to JUMP to GAMEPLAY SCREEN", 120, 220, 20, Color.DarkGreen); DrawText("PRESS ENTER or TAP to JUMP to GAMEPLAY SCREEN", 120, 220, 20, Color.DarkGreen);
} }
break; break;
case GameScreen.Gameplay: case GameScreen.Gameplay:
@ -151,7 +143,6 @@ public class BasicScreenManager
DrawRectangle(0, 0, screenWidth, screenHeight, Color.Blue); DrawRectangle(0, 0, screenWidth, screenHeight, Color.Blue);
DrawText("ENDING SCREEN", 20, 20, 40, Color.DarkBlue); DrawText("ENDING SCREEN", 20, 20, 40, Color.DarkBlue);
DrawText("PRESS ENTER or TAP to RETURN to TITLE SCREEN", 120, 220, 20, Color.DarkBlue); DrawText("PRESS ENTER or TAP to RETURN to TITLE SCREEN", 120, 220, 20, Color.DarkBlue);
} }
break; break;
default: default:
@ -162,12 +153,34 @@ public class BasicScreenManager
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization public void Unload()
{
// TODO: Unload all loaded data (textures, fonts, audio) here!
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic screen manager");
SetTargetFPS(60); // Set desired framerate (frames-per-second)
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// TODO: Unload all loaded data (textures, fonts, audio) here! var game = new BasicScreenManager();
game.Init();
CloseWindow(); // 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; return 0;

View file

@ -33,43 +33,56 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class BasicWindow public partial class BasicWindow : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Basic Window";
public string Title => "raylib [core] example - basic window";
public void Init()
{ {
// Initialization }
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window"); public void Update()
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(Color.RayWhite); ClearBackground(Color.RayWhite);
DrawText("Congrats! You created your first window!", 190, 200, 20, Color.LightGray); DrawText("Congrats! You created your first window!", 190, 200, 20, Color.LightGray);
EndDrawing(); EndDrawing();
//----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
var game = new BasicWindow();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0; return 0;
} }

View file

@ -13,31 +13,37 @@
* *
********************************************************************************************/ ********************************************************************************************/
using System;
using System.Numerics; using System.Numerics;
using static Raylib_cs.Raylib; using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class Camera2dDemo public partial class Camera2dDemo : IExample
{ {
public const int MaxBuildings = 100; public const int MaxBuildings = 100;
public static int Main() 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()
{ {
// Initialization player = new(400, 280, 40, 40);
//-------------------------------------------------------------------------------------- buildings = new Rectangle[MaxBuildings];
const int screenWidth = 800; buildColors = new Color[MaxBuildings];
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera"); var spacing = 0;
Rectangle player = new(400, 280, 40, 40); for (var i = 0; i < MaxBuildings; i++)
Rectangle[] buildings = new Rectangle[MaxBuildings];
Color[] buildColors = new Color[MaxBuildings];
int spacing = 0;
for (int i = 0; i < MaxBuildings; i++)
{ {
buildings[i].Width = GetRandomValue(50, 200); buildings[i].Width = GetRandomValue(50, 200);
buildings[i].Height = GetRandomValue(100, 800); buildings[i].Height = GetRandomValue(100, 800);
@ -54,21 +60,17 @@ public class Camera2dDemo
); );
} }
Camera2D camera = new(); camera = new();
camera.Target = new Vector2(player.X + 20, player.Y + 20); camera.Target = new Vector2(player.X + 20, player.Y + 20);
camera.Offset = new Vector2(screenWidth / 2, screenHeight / 2); camera.Offset = new Vector2(screenWidth / 2.0f, screenHeight / 2.0f);
camera.Rotation = 0.0f; camera.Rotation = 0.0f;
camera.Zoom = 1.0f; camera.Zoom = 1.0f;
}
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Player movement // Player movement
if (IsKeyDown(KeyboardKey.Right)) if (IsKeyDown(KeyboardKey.Right))
{ {
@ -79,10 +81,10 @@ public class Camera2dDemo
player.X -= 2; player.X -= 2;
} }
// Camera3D target follows player // Camera target follows player
camera.Target = new Vector2(player.X + 20, player.Y + 20); camera.Target = new Vector2(player.X + 20, player.Y + 20);
// Camera3D rotation controls // Camera rotation controls
if (IsKeyDown(KeyboardKey.A)) if (IsKeyDown(KeyboardKey.A))
{ {
camera.Rotation--; camera.Rotation--;
@ -102,8 +104,9 @@ public class Camera2dDemo
camera.Rotation = -40; camera.Rotation = -40;
} }
// Camera3D zoom controls // Camera zoom controls
camera.Zoom += ((float)GetMouseWheelMove() * 0.05f); // 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) if (camera.Zoom > 3.0f)
{ {
@ -114,7 +117,7 @@ public class Camera2dDemo
camera.Zoom = 0.1f; camera.Zoom = 0.1f;
} }
// Camera3D reset (zoom and rotation) // Camera reset (zoom and rotation)
if (IsKeyPressed(KeyboardKey.R)) if (IsKeyPressed(KeyboardKey.R))
{ {
camera.Zoom = 1.0f; camera.Zoom = 1.0f;
@ -131,36 +134,30 @@ public class Camera2dDemo
DrawRectangle(-6000, 320, 13000, 8000, Color.DarkGray); DrawRectangle(-6000, 320, 13000, 8000, Color.DarkGray);
for (int i = 0; i < MaxBuildings; i++) for (var i = 0; i < MaxBuildings; i++)
{ {
DrawRectangleRec(buildings[i], buildColors[i]); DrawRectangleRec(buildings[i], buildColors[i]);
} }
DrawRectangleRec(player, Color.Red); DrawRectangleRec(player, Color.Red);
DrawRectangle((int)camera.Target.X, -500, 1, (int)(screenHeight * 4), Color.Green); DrawLine((int)camera.Target.X, -screenHeight * 10, (int)camera.Target.X, screenHeight * 10, Color.Green);
DrawLine( DrawLine(-screenWidth * 10, (int)camera.Target.Y, screenWidth * 10, (int)camera.Target.Y, Color.Green);
(int)(-screenWidth * 10),
(int)camera.Target.Y,
(int)(screenWidth * 10),
(int)camera.Target.Y,
Color.Green
);
EndMode2D(); EndMode2D();
DrawText("SCREEN AREA", 640, 10, 20, Color.Red); DrawText("SCREEN AREA", 640, 10, 20, Color.Red);
DrawRectangle(0, 0, (int)screenWidth, 5, Color.Red); DrawRectangle(0, 0, screenWidth, 5, Color.Red);
DrawRectangle(0, 5, 5, (int)screenHeight - 10, Color.Red); DrawRectangle(0, 5, 5, screenHeight - 10, Color.Red);
DrawRectangle((int)screenWidth - 5, 5, 5, (int)screenHeight - 10, Color.Red); DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, Color.Red);
DrawRectangle(0, (int)screenHeight - 5, (int)screenWidth, 5, Color.Red); DrawRectangle(0, screenHeight - 5, screenWidth, 5, Color.Red);
DrawRectangle(10, 10, 250, 113, ColorAlpha(Color.SkyBlue, 0.5f)); DrawRectangle(10, 10, 250, 113, Fade(Color.SkyBlue, 0.5f));
DrawRectangleLines(10, 10, 250, 113, Color.Blue); DrawRectangleLines(10, 10, 250, 113, Color.Blue);
DrawText("Free 2d camera controls:", 20, 20, 10, Color.Black); DrawText("Free 2D camera controls:", 20, 20, 10, Color.Black);
DrawText("- Right/Left to move Offset", 40, 40, 10, Color.DarkGray); DrawText("- Right/Left to move player", 40, 40, 10, Color.DarkGray);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 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("- A / S to Rotate", 40, 80, 10, Color.DarkGray);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, Color.DarkGray); DrawText("- R to reset Zoom and Rotation", 40, 100, 10, Color.DarkGray);
@ -169,9 +166,33 @@ public class Camera2dDemo
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
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 // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -2,12 +2,16 @@
* *
* raylib [core] example - 2d camera platformer * raylib [core] example - 2d camera platformer
* *
* This example has been created using raylib 2.5 (www.raylib.com) * Example complexity rating: [★★★☆] 3/4
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) *
* Example originally created with raylib 2.5, last time updated with raylib 3.0
* *
* Example contributed by arvyy (@arvyy) and reviewed by Ramon Santamaria (@raysan5) * Example contributed by arvyy (@arvyy) and reviewed by Ramon Santamaria (@raysan5)
* *
* Copyright (c) 2019 arvyy (@arvyy) * 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 arvyy (@arvyy)
* *
********************************************************************************************/ ********************************************************************************************/
@ -18,20 +22,27 @@ using static Raylib_cs.Raymath;
namespace Examples.Core; namespace Examples.Core;
public class Camera2dPlatformer public partial class Camera2dPlatformer : IExample
{ {
const int G = 400; private const int screenWidth = 800;
const float PlayerJumpSpeed = 350.0f; private const int screenHeight = 450;
const float PlayerHorSpeed = 200.0f;
struct Player private const int G = 400;
private const float PlayerJumpSpeed = 350.0f;
private const float PlayerHorSpeed = 200.0f;
public string Name => "Core / 2D Camera Platformer";
public string Title => "raylib [core] example - 2d camera platformer";
private struct Player
{ {
public Vector2 Position; public Vector2 Position;
public float Speed; public float Speed;
public bool CanJump; public bool CanJump;
} }
struct EnvItem private struct EnvItem
{ {
public Rectangle Rect; public Rectangle Rect;
public int Blocking; public int Blocking;
@ -45,7 +56,7 @@ public class Camera2dPlatformer
} }
} }
delegate void CameraUpdaterCallback( private delegate void CameraUpdaterCallback(
ref Camera2D camera, ref Camera2D camera,
ref Player player, ref Player player,
EnvItem[] envItems, EnvItem[] envItems,
@ -54,21 +65,22 @@ public class Camera2dPlatformer
int height int height
); );
public static int Main() private Player player;
private EnvItem[] envItems;
private Camera2D camera;
private CameraUpdaterCallback[] cameraUpdaters;
private int cameraOption;
private int cameraUpdatersLength;
private string[] cameraDescriptions;
public void Init()
{ {
// Initialization player = new();
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
Player player = new();
player.Position = new Vector2(400, 280); player.Position = new Vector2(400, 280);
player.Speed = 0; player.Speed = 0;
player.CanJump = false; player.CanJump = false;
EnvItem[] envItems = new EnvItem[] envItems = new EnvItem[]
{ {
new EnvItem(new Rectangle(0, 0, 1000, 400), 0, Color.LightGray), new EnvItem(new Rectangle(0, 0, 1000, 400), 0, Color.LightGray),
new EnvItem(new Rectangle(0, 400, 1000, 200), 1, Color.Gray), new EnvItem(new Rectangle(0, 400, 1000, 200), 1, Color.Gray),
@ -77,14 +89,14 @@ public class Camera2dPlatformer
new EnvItem(new Rectangle(650, 300, 100, 10), 1, Color.Gray) new EnvItem(new Rectangle(650, 300, 100, 10), 1, Color.Gray)
}; };
Camera2D camera = new(); camera = new();
camera.Target = player.Position; camera.Target = player.Position;
camera.Offset = new Vector2(screenWidth / 2, screenHeight / 2); camera.Offset = new Vector2(screenWidth / 2.0f, screenHeight / 2.0f);
camera.Rotation = 0.0f; camera.Rotation = 0.0f;
camera.Zoom = 1.0f; camera.Zoom = 1.0f;
// Store callbacks to the multiple update camera functions // Store pointers to the multiple update camera functions
CameraUpdaterCallback[] cameraUpdaters = new CameraUpdaterCallback[] cameraUpdaters = new CameraUpdaterCallback[]
{ {
UpdateCameraCenter, UpdateCameraCenter,
UpdateCameraCenterInsideMap, UpdateCameraCenterInsideMap,
@ -93,26 +105,23 @@ public class Camera2dPlatformer
UpdateCameraPlayerBoundsPush UpdateCameraPlayerBoundsPush
}; };
int cameraOption = 0; cameraOption = 0;
int cameraUpdatersLength = cameraUpdaters.Length; cameraUpdatersLength = cameraUpdaters.Length;
string[] cameraDescriptions = new string[]{ cameraDescriptions = new string[]{
"Follow player center", "Follow player center",
"Follow player center, but clamp to map edges", "Follow player center, but clamp to map edges",
"Follow player center; smoothed", "Follow player center; smoothed",
"Follow player center horizontally; update player center vertically after landing", "Follow player center horizontally; update player center vertically after landing",
"Player push camera on getting too close to screen edge" "Player push camera on getting too close to screen edge"
}; };
}
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
float deltaTime = GetFrameTime(); var deltaTime = GetFrameTime();
UpdatePlayer(ref player, envItems, deltaTime); UpdatePlayer(ref player, envItems, deltaTime);
@ -149,37 +158,36 @@ public class Camera2dPlatformer
BeginMode2D(camera); BeginMode2D(camera);
for (int i = 0; i < envItems.Length; i++) for (var i = 0; i < envItems.Length; i++)
{ {
DrawRectangleRec(envItems[i].Rect, envItems[i].Color); DrawRectangleRec(envItems[i].Rect, envItems[i].Color);
} }
Rectangle playerRect = new(player.Position.X - 20, player.Position.Y - 40, 40, 40); Rectangle playerRect = new(player.Position.X - 20, player.Position.Y - 40, 40.0f, 40.0f);
DrawRectangleRec(playerRect, Color.Red); DrawRectangleRec(playerRect, Color.Red);
DrawCircleV(player.Position, 5.0f, Color.Gold);
EndMode2D(); EndMode2D();
DrawText("Controls:", 20, 20, 10, Color.Black); DrawText("Controls:", 20, 20, 10, Color.Black);
DrawText("- Right/Left to move", 40, 40, 10, Color.DarkGray); DrawText("- Right/Left to move", 40, 40, 10, Color.DarkGray);
DrawText("- Space to jump", 40, 60, 10, Color.DarkGray); DrawText("- Space to jump", 40, 60, 10, Color.DarkGray);
DrawText("- Mouse Wheel to Zoom in-out, R to reset zoom", 40, 80, 10, Color.DarkGray); DrawText("- Mouse Wheel to Zoom in-out", 40, 80, 10, Color.DarkGray);
DrawText("- C to change camera mode", 40, 100, 10, Color.DarkGray); DrawText("- R to reset position + zoom", 40, 100, 10, Color.DarkGray);
DrawText("Current camera mode:", 20, 120, 10, Color.Black); DrawText("- C to change camera mode", 40, 120, 10, Color.DarkGray);
DrawText(cameraDescriptions[cameraOption], 40, 140, 10, Color.DarkGray); DrawText("Current camera mode:", 20, 140, 10, Color.Black);
DrawText(cameraDescriptions[cameraOption], 40, 160, 10, Color.DarkGray);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization public void Unload()
//-------------------------------------------------------------------------------------- {
CloseWindow();
//--------------------------------------------------------------------------------------
return 0;
} }
static void UpdatePlayer(ref Player player, EnvItem[] envItems, float delta) private static void UpdatePlayer(ref Player player, EnvItem[] envItems, float delta)
{ {
if (IsKeyDown(KeyboardKey.Left)) if (IsKeyDown(KeyboardKey.Left))
{ {
@ -197,11 +205,11 @@ public class Camera2dPlatformer
player.CanJump = false; player.CanJump = false;
} }
int hitObstacle = 0; var hitObstacle = 0;
for (int i = 0; i < envItems.Length; i++) for (var i = 0; i < envItems.Length; i++)
{ {
EnvItem ei = envItems[i]; var ei = envItems[i];
Vector2 p = player.Position; var p = player.Position;
if (ei.Blocking != 0 && if (ei.Blocking != 0 &&
ei.Rect.X <= p.X && ei.Rect.X <= p.X &&
ei.Rect.X + ei.Rect.Width >= p.X && ei.Rect.X + ei.Rect.Width >= p.X &&
@ -211,6 +219,7 @@ public class Camera2dPlatformer
hitObstacle = 1; hitObstacle = 1;
player.Speed = 0.0f; player.Speed = 0.0f;
player.Position.Y = ei.Rect.Y; player.Position.Y = ei.Rect.Y;
break;
} }
} }
@ -226,7 +235,7 @@ public class Camera2dPlatformer
} }
} }
static void UpdateCameraCenter( private static void UpdateCameraCenter(
ref Camera2D camera, ref Camera2D camera,
ref Player player, ref Player player,
EnvItem[] envItems, EnvItem[] envItems,
@ -235,11 +244,11 @@ public class Camera2dPlatformer
int height int height
) )
{ {
camera.Offset = new Vector2(width / 2, height / 2); camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
camera.Target = player.Position; camera.Target = player.Position;
} }
static void UpdateCameraCenterInsideMap( private static void UpdateCameraCenterInsideMap(
ref Camera2D camera, ref Camera2D camera,
ref Player player, ref Player player,
EnvItem[] envItems, EnvItem[] envItems,
@ -248,43 +257,43 @@ public class Camera2dPlatformer
int height) int height)
{ {
camera.Target = player.Position; camera.Target = player.Position;
camera.Offset = new Vector2(width / 2, height / 2); camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000; float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;
for (int i = 0; i < envItems.Length; i++) for (var i = 0; i < envItems.Length; i++)
{ {
EnvItem ei = envItems[i]; var ei = envItems[i];
minX = Math.Min(ei.Rect.X, minX); minX = Math.Min(ei.Rect.X, minX);
maxX = Math.Max(ei.Rect.X + ei.Rect.Width, maxX); maxX = Math.Max(ei.Rect.X + ei.Rect.Width, maxX);
minY = Math.Min(ei.Rect.Y, minY); minY = Math.Min(ei.Rect.Y, minY);
maxY = Math.Max(ei.Rect.Y + ei.Rect.Height, maxY); maxY = Math.Max(ei.Rect.Y + ei.Rect.Height, maxY);
} }
Vector2 max = GetWorldToScreen2D(new Vector2(maxX, maxY), camera); var max = GetWorldToScreen2D(new Vector2(maxX, maxY), camera);
Vector2 min = GetWorldToScreen2D(new Vector2(minX, minY), camera); var min = GetWorldToScreen2D(new Vector2(minX, minY), camera);
if (max.X < width) if (max.X < width)
{ {
camera.Offset.X = width - (max.X - width / 2); camera.Offset.X = width - (max.X - width / 2.0f);
} }
if (max.Y < height) if (max.Y < height)
{ {
camera.Offset.Y = height - (max.Y - height / 2); camera.Offset.Y = height - (max.Y - height / 2.0f);
} }
if (min.X > 0) if (min.X > 0)
{ {
camera.Offset.X = width / 2 - min.X; camera.Offset.X = width / 2.0f - min.X;
} }
if (min.Y > 0) if (min.Y > 0)
{ {
camera.Offset.Y = height / 2 - min.Y; camera.Offset.Y = height / 2.0f - min.Y;
} }
} }
static void UpdateCameraCenterSmoothFollow( private static void UpdateCameraCenterSmoothFollow(
ref Camera2D camera, ref Camera2D camera,
ref Player player, ref Player player,
EnvItem[] envItems, EnvItem[] envItems,
@ -297,18 +306,18 @@ public class Camera2dPlatformer
const float minEffectLength = 10; const float minEffectLength = 10;
const float fractionSpeed = 0.8f; const float fractionSpeed = 0.8f;
camera.Offset = new Vector2(width / 2, height / 2); camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
Vector2 diff = Vector2Subtract(player.Position, camera.Target); var diff = Vector2Subtract(player.Position, camera.Target);
float length = Vector2Length(diff); var length = Vector2Length(diff);
if (length > minEffectLength) if (length > minEffectLength)
{ {
float speed = Math.Max(fractionSpeed * length, minSpeed); var speed = Math.Max(fractionSpeed * length, minSpeed);
camera.Target = Vector2Add(camera.Target, Vector2Scale(diff, speed * delta / length)); camera.Target = Vector2Add(camera.Target, Vector2Scale(diff, speed * delta / length));
} }
} }
static void UpdateCameraEvenOutOnLanding( private static void UpdateCameraEvenOutOnLanding(
ref Camera2D camera, ref Camera2D camera,
ref Player player, ref Player player,
EnvItem[] envItems, EnvItem[] envItems,
@ -318,10 +327,10 @@ public class Camera2dPlatformer
) )
{ {
float evenOutSpeed = 700; float evenOutSpeed = 700;
int eveningOut = 0; var eveningOut = 0;
float evenOutTarget = 0.0f; var evenOutTarget = 0.0f;
camera.Offset = new Vector2(width / 2, height / 2); camera.Offset = new Vector2(width / 2.0f, height / 2.0f);
camera.Target.X = player.Position.X; camera.Target.X = player.Position.X;
if (eveningOut != 0) if (eveningOut != 0)
@ -357,7 +366,7 @@ public class Camera2dPlatformer
} }
} }
static void UpdateCameraPlayerBoundsPush( private static void UpdateCameraPlayerBoundsPush(
ref Camera2D camera, ref Camera2D camera,
ref Player player, ref Player player,
EnvItem[] envItems, EnvItem[] envItems,
@ -368,11 +377,11 @@ public class Camera2dPlatformer
{ {
Vector2 bbox = new(0.2f, 0.2f); Vector2 bbox = new(0.2f, 0.2f);
Vector2 bboxWorldMin = GetScreenToWorld2D( var bboxWorldMin = GetScreenToWorld2D(
new Vector2((1 - bbox.X) * 0.5f * width, (1 - bbox.Y) * 0.5f * height), new Vector2((1 - bbox.X) * 0.5f * width, (1 - bbox.Y) * 0.5f * height),
camera camera
); );
Vector2 bboxWorldMax = GetScreenToWorld2D( var bboxWorldMax = GetScreenToWorld2D(
new Vector2((1 + bbox.X) * 0.5f * width, new Vector2((1 + bbox.X) * 0.5f * width,
(1 + bbox.Y) * 0.5f * height), (1 + bbox.Y) * 0.5f * height),
camera camera
@ -399,4 +408,32 @@ public class Camera2dPlatformer
camera.Target.Y = bboxWorldMin.Y + (player.Position.Y - bboxWorldMax.Y); camera.Target.Y = bboxWorldMin.Y + (player.Position.Y - bboxWorldMax.Y);
} }
} }
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera platformer");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new Camera2dPlatformer();
game.Init();
// Main game loop
while (!WindowShouldClose())
{
game.Update();
}
game.Unload();
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
} }

View file

@ -2,10 +2,14 @@
* *
* raylib [core] example - 3d camera first person * raylib [core] example - 3d camera first person
* *
* This example has been created using raylib 1.3 (www.raylib.com) * Example complexity rating: [★★☆☆] 2/4
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2015 Ramon Santamaria (@raysan5) * Example originally created with raylib 1.3, last time updated with raylib 1.3
*
* 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) 2015-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -14,46 +18,51 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class Camera3dFirstPerson public partial class Camera3dFirstPerson : IExample
{ {
public const int MaxColumns = 20; public const int MaxColumns = 20;
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Camera 3D First Person";
public string Title => "raylib [core] example - 3d camera first person";
public bool CursorDisabled => true;
private Camera3D camera;
private CameraMode cameraMode;
private float[] heights;
private Vector3[] positions;
private Color[] colors;
public void Init()
{ {
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
// Define the camera to look into our 3d world (position, target, up vector) // Define the camera to look into our 3d world (position, target, up vector)
Camera3D camera = new(); camera = new();
camera.Position = new Vector3(4.0f, 2.0f, 4.0f); camera.Position = new Vector3(0.0f, 2.0f, 4.0f); // Camera position
camera.Target = new Vector3(0.0f, 1.8f, 0.0f); camera.Target = new Vector3(0.0f, 2.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 60.0f; camera.FovY = 60.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; camera.Projection = CameraProjection.Perspective; // Camera projection type
cameraMode = CameraMode.FirstPerson;
// Generates some random columns // Generates some random columns
float[] heights = new float[MaxColumns]; heights = new float[MaxColumns];
Vector3[] positions = new Vector3[MaxColumns]; positions = new Vector3[MaxColumns];
Color[] colors = new Color[MaxColumns]; colors = new Color[MaxColumns];
for (int i = 0; i < MaxColumns; i++) for (var i = 0; i < MaxColumns; i++)
{ {
heights[i] = (float)GetRandomValue(1, 12); heights[i] = (float)GetRandomValue(1, 12);
positions[i] = new Vector3(GetRandomValue(-15, 15), heights[i] / 2, GetRandomValue(-15, 15)); positions[i] = new Vector3(GetRandomValue(-15, 15), heights[i] / 2.0f, GetRandomValue(-15, 15));
colors[i] = new Color(GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255); colors[i] = new Color(GetRandomValue(20, 255), GetRandomValue(10, 55), 30, 255);
} }
}
CameraMode cameraMode = CameraMode.FirstPerson; public void Update()
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -112,8 +121,8 @@ public class Camera3dFirstPerson
// Update camera computes movement internally depending on the camera mode // Update camera computes movement internally depending on the camera mode
// Some default standard keyboard/mouse inputs are hardcoded to simplify use // Some default standard keyboard/mouse inputs are hardcoded to simplify use
// For advance camera controls, it's reecommended to compute camera movement manually // For advanced camera controls, it's recommended to compute camera movement manually
UpdateCamera(ref camera, cameraMode); UpdateCamera(ref camera, cameraMode); // Update camera
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -136,7 +145,7 @@ public class Camera3dFirstPerson
DrawCube(new Vector3(0.0f, 2.5f, 16.0f), 32.0f, 5.0f, 1.0f, Color.Gold); DrawCube(new Vector3(0.0f, 2.5f, 16.0f), 32.0f, 5.0f, 1.0f, Color.Gold);
// Draw some cubes around // Draw some cubes around
for (int i = 0; i < MaxColumns; i++) for (var i = 0; i < MaxColumns; i++)
{ {
DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]); DrawCube(positions[i], 2.0f, heights[i], 2.0f, colors[i]);
DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, Color.Maroon); DrawCubeWires(positions[i], 2.0f, heights[i], 2.0f, Color.Maroon);
@ -152,8 +161,8 @@ public class Camera3dFirstPerson
EndMode3D(); EndMode3D();
// Draw info boxes // Draw info boxes
DrawRectangle(5, 5, 330, 100, ColorAlpha(Color.SkyBlue, 0.5f)); DrawRectangle(5, 5, 330, 100, Fade(Color.SkyBlue, 0.5f));
DrawRectangleLines(10, 10, 330, 100, Color.Blue); DrawRectangleLines(5, 5, 330, 100, Color.Blue);
DrawText("Camera controls:", 15, 15, 10, Color.Black); DrawText("Camera controls:", 15, 15, 10, Color.Black);
DrawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 10, Color.Black); DrawText("- Move keys: W, A, S, D, Space, Left-Ctrl", 15, 30, 10, Color.Black);
@ -176,9 +185,35 @@ public class Camera3dFirstPerson
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera first person");
DisableCursor(); // Limit cursor to relative movement inside the window
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new Camera3dFirstPerson();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,11 +1,15 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [core] example - Initialize 3d camera free * raylib [core] example - 3d camera free
* *
* This example has been created using raylib 1.3 (www.raylib.com) * Example complexity rating: [★☆☆☆] 1/4
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2015 Ramon Santamaria (@raysan5) * Example originally created with raylib 1.3, last time updated with raylib 1.3
*
* 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) 2015-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -14,38 +18,40 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class Camera3dFree public partial class Camera3dFree : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Camera 3D Free";
public string Title => "raylib [core] example - 3d camera free";
public bool CursorDisabled => true;
private Camera3D camera;
private Vector3 cubePosition;
public void Init()
{ {
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free");
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
Camera3D camera; camera = new();
camera.Position = new Vector3(10.0f, 10.0f, 10.0f); camera.Position = new Vector3(10.0f, 10.0f, 10.0f); // Camera position
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; camera.Projection = CameraProjection.Perspective; // Camera projection type
Vector3 cubePosition = new(0.0f, 0.0f, 0.0f); cubePosition = new(0.0f, 0.0f, 0.0f);
}
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateCamera(ref camera, CameraMode.Free); UpdateCamera(ref camera, CameraMode.Free);
if (IsKeyDown(KeyboardKey.Z)) if (IsKeyPressed(KeyboardKey.Z))
{ {
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
} }
@ -65,23 +71,47 @@ public class Camera3dFree
EndMode3D(); EndMode3D();
DrawRectangle(10, 10, 320, 133, ColorAlpha(Color.SkyBlue, 0.5f)); DrawRectangle(10, 10, 320, 93, Fade(Color.SkyBlue, 0.5f));
DrawRectangleLines(10, 10, 320, 133, Color.Blue); DrawRectangleLines(10, 10, 320, 93, Color.Blue);
DrawText("Free camera default controls:", 20, 20, 10, Color.Black); DrawText("Free camera default controls:", 20, 20, 10, Color.Black);
DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, Color.DarkGray); DrawText("- Mouse Wheel to Zoom in-out", 40, 40, 10, Color.DarkGray);
DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, Color.DarkGray); DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, Color.DarkGray);
DrawText("- Alt + Mouse Wheel Pressed to Rotate", 40, 80, 10, Color.DarkGray); DrawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, Color.DarkGray);
DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 40, 100, 10, Color.DarkGray);
DrawText("- Z to zoom to (0, 0, 0)", 40, 120, 10, Color.DarkGray);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera free");
DisableCursor(); // Limit cursor to relative movement inside the window
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new Camera3dFree();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,11 +1,15 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [core] example - Initialize 3d camera mode * raylib [core] example - 3d camera mode
* *
* This example has been created using raylib 1.0 (www.raylib.com) * Example complexity rating: [★☆☆☆] 1/4
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2014 Ramon Santamaria (@raysan5) * Example originally created with raylib 1.0, last time updated with raylib 1.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) 2014-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -14,38 +18,33 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class Camera3dMode public partial class Camera3dMode : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
private Camera3D camera;
private Vector3 cubePosition;
public string Name => "Core / Camera 3D Mode";
public string Title => "raylib [core] example - 3d camera mode";
public void Init()
{ {
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera mode");
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
Camera3D camera = new(); camera = new();
camera.Position = new Vector3(0.0f, 10.0f, 10.0f); camera.Position = new Vector3(0.0f, 10.0f, 10.0f); // Camera position
camera.Target = new Vector3(0.0f, 0.0f, 0.0f); camera.Target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point
camera.Up = new Vector3(0.0f, 1.0f, 0.0f); camera.Up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target)
camera.FovY = 45.0f; camera.FovY = 45.0f; // Camera field-of-view Y
camera.Projection = CameraProjection.Perspective; camera.Projection = CameraProjection.Perspective; // Camera mode type
Vector3 cubePosition = new(0.0f, 0.0f, 0.0f); cubePosition = new(0.0f, 0.0f, 0.0f);
}
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
@ -68,9 +67,33 @@ public class Camera3dMode
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d camera mode");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new Camera3dMode();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,13 +1,17 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [core] example - Custom logging * raylib [core] example - custom logging
* *
* This example has been created using raylib 2.1 (www.raylib.com) * Example complexity rating: [★★★☆] 3/4
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) *
* Example originally created with raylib 2.5, last time updated with raylib 2.5
* *
* Example contributed by Pablo Marcos Oltra (@pamarcos) and reviewed by Ramon Santamaria (@raysan5) * Example contributed by Pablo Marcos Oltra (@pamarcos) and reviewed by Ramon Santamaria (@raysan5)
* *
* Copyright (c) 2018 Pablo Marcos Oltra (@pamarcos) 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) 2018-2025 Pablo Marcos Oltra (@pamarcos) and Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
@ -17,11 +21,23 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public unsafe class CustomLogging public unsafe partial class CustomLogging : IExample
{ {
private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Custom Logging";
public string Title => "raylib [core] example - custom logging";
[UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] [UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
private static void LogCustom(int logLevel, sbyte* text, sbyte* args) private static void LogCustom(int logLevel, sbyte* text, sbyte* args)
{ {
#if BROWSER
// WebAssembly can't invoke the C varargs vsprintf/vsnprintf that Logging.GetLogMessage relies on
// (the wasm runtime traps with "function signature mismatch"), so log the raw, unformatted text.
string message = Marshal.PtrToStringUTF8(new IntPtr(text)) ?? string.Empty;
#else
var message = Logging.GetLogMessage(new IntPtr(text), new IntPtr(args)); var message = Logging.GetLogMessage(new IntPtr(text), new IntPtr(args));
/*Console.ForegroundColor = (TraceLogLevel)logLevel switch /*Console.ForegroundColor = (TraceLogLevel)logLevel switch
@ -36,35 +52,21 @@ public unsafe class CustomLogging
TraceLogLevel.LOG_NONE => ConsoleColor.White, TraceLogLevel.LOG_NONE => ConsoleColor.White,
_ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null) _ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null)
};*/ };*/
#endif
Console.WriteLine($"Custom " + message); Console.WriteLine($"Custom " + message);
// Console.ResetColor(); // Console.ResetColor();
} }
public static int Main() public void Init()
{ {
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
// First thing we do is setting our custom logger to ensure everything raylib logs // First thing we do is setting our custom logger to ensure everything raylib logs
// will use our own logger instead of its internal one // will use our own logger instead of its internal one
SetTraceLogCallback(&LogCustom); SetTraceLogCallback(&LogCustom);
}
InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging"); public void Update()
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
@ -76,10 +78,38 @@ public unsafe class CustomLogging
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
#if !BROWSER
// Restore the default formatting logger on desktop. On WebAssembly we leave the (wasm-safe)
// custom logger in place — Logging.LogConsole formats via vsprintf, which traps on wasm.
SetTraceLogCallback(&Logging.LogConsole);
#endif
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - custom logging");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new CustomLogging();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
SetTraceLogCallback(&Logging.LogConsole);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -21,35 +21,40 @@ namespace Examples.Core;
using static Raylib_cs.Raylib; using static Raylib_cs.Raylib;
public class DeltaTime public partial class DeltaTime : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
// The speed applied to both circles
private const float speed = 10.0f;
private const float circleRadius = 32.0f;
private int currentFps;
// Store the position for the both of the circles
private Vector2 deltaCircle;
private Vector2 frameCircle;
public string Name => "Core / Delta Time";
public string Title => "raylib [core] example - delta time";
public void Init()
{ {
// Initialization currentFps = 60;
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time"); // Store the position for the both of the circles
deltaCircle = new Vector2(0, (float)screenHeight / 3.0f);
frameCircle = new Vector2(0, (float)screenHeight * (2.0f / 3.0f));
}
int currentFps = 60; public void Update()
Vector2 deltaCircle = new Vector2(0, (float)screenHeight / 3.0f);
Vector2 frameCircle = new Vector2(0, (float)screenHeight * (2.0f / 3.0f));
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
const float speed = 10.0f;
const float circleRadius = 32.0f;
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Adjust the FPS target based on the mouse wheel // Adjust the FPS target based on the mouse wheel
float mouseWheel = GetMouseWheelMove(); var mouseWheel = GetMouseWheelMove();
if (mouseWheel != 0) if (mouseWheel != 0)
{ {
currentFps += (int)mouseWheel; currentFps += (int)mouseWheel;
@ -120,9 +125,33 @@ public class DeltaTime
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - delta time");
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
var game = new DeltaTime();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -1,53 +1,69 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [core] example - Windows drop files * raylib [core] example - drop files
* *
* This example only works on platforms that support drag ref drop (Windows, Linux, OSX, Html5?) * Example complexity rating: [★★☆☆] 2/4
* *
* This example has been created using raylib 1.3 (www.raylib.com) * NOTE: This example only works on platforms that support drag & drop (Windows, Linux, OSX, Html5?)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2015 Ramon Santamaria (@raysan5) * Example originally created with raylib 1.3, last time updated with raylib 4.2
*
* 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) 2015-2025 Ramon Santamaria (@raysan5)
* *
********************************************************************************************/ ********************************************************************************************/
using System.Collections.Generic;
using static Raylib_cs.Raylib; using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class DropFiles public partial class DropFiles : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public const int MaxFilepathRecorded = 4096;
public string Name => "Core / Drop Files";
public string Title => "raylib [core] example - drop files";
private List<string> filePaths;
public void Init()
{ {
// Initialization // We will register a maximum of filepaths
//-------------------------------------------------------------------------------------- filePaths = new();
const int screenWidth = 800; }
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files"); public void Update()
string[] files = new string[0];
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsFileDropped()) if (IsFileDropped())
{ {
files = Raylib.GetDroppedFiles(); var droppedFiles = GetDroppedFiles();
for (var i = 0; i < droppedFiles.Length; i++)
{
if (filePaths.Count < (MaxFilepathRecorded - 1))
{
filePaths.Add(droppedFiles[i]);
}
}
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(Color.RayWhite); ClearBackground(Color.RayWhite);
if (files.Length == 0) if (filePaths.Count == 0)
{ {
DrawText("Drop your files to this window!", 100, 40, 20, Color.DarkGray); DrawText("Drop your files to this window!", 100, 40, 20, Color.DarkGray);
} }
@ -55,29 +71,54 @@ public class DropFiles
{ {
DrawText("Dropped files:", 100, 40, 20, Color.DarkGray); DrawText("Dropped files:", 100, 40, 20, Color.DarkGray);
for (int i = 0; i < files.Length; i++) for (var i = 0; i < filePaths.Count; i++)
{ {
if (i % 2 == 0) if (i % 2 == 0)
{ {
DrawRectangle(0, 85 + 40 * i, screenWidth, 40, ColorAlpha(Color.LightGray, 0.5f)); DrawRectangle(0, 85 + 40 * i, screenWidth, 40, Fade(Color.LightGray, 0.5f));
} }
else else
{ {
DrawRectangle(0, 85 + 40 * i, screenWidth, 40, ColorAlpha(Color.LightGray, 0.3f)); DrawRectangle(0, 85 + 40 * i, screenWidth, 40, Fade(Color.LightGray, 0.3f));
}
DrawText(files[i], 120, 100 + 40 * i, 10, Color.Gray);
} }
DrawText("Drop new files...", 100, 110 + 40 * files.Length, 20, Color.DarkGray); DrawText(filePaths[i], 120, 100 + 40 * i, 10, Color.Gray);
}
DrawText("Drop new files...", 100, 110 + 40 * filePaths.Count, 20, Color.DarkGray);
} }
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - drop files");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new DropFiles();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -19,14 +19,16 @@
* *
********************************************************************************************/ ********************************************************************************************/
using System.Numerics; using System.Numerics;
using static Raylib_cs.Raylib; using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class InputGamepad public partial class InputGamepad : IExample
{ {
private const int screenWidth = 800;
private const int screenHeight = 450;
// NOTE: Gamepad name ID depends on drivers and OS // NOTE: Gamepad name ID depends on drivers and OS
// These are some possible names the gamepads could have. // These are some possible names the gamepads could have.
public const string XBOX_ALIAS_1 = "xbox"; public const string XBOX_ALIAS_1 = "xbox";
@ -34,28 +36,38 @@ public class InputGamepad
public const string PS_ALIAS_1 = "playstation"; public const string PS_ALIAS_1 = "playstation";
public const string PS_ALIAS_2 = "sony"; public const string PS_ALIAS_2 = "sony";
public static int Main() // Set axis deadzones
private const float leftStickDeadzoneX = 0.1f;
private const float leftStickDeadzoneY = 0.1f;
private const float rightStickDeadzoneX = 0.1f;
private const float rightStickDeadzoneY = 0.1f;
private const float leftTriggerDeadzone = -0.9f;
private const float rightTriggerDeadzone = -0.9f;
private Texture2D texPs3Pad;
private Texture2D texXboxPad;
private Rectangle vibrateButton;
private int gamepad; // which gamepad to display
public string Name => "Core / Input Gamepad";
public string Title => "raylib [core] example - input gamepad";
public ConfigFlags ConfigFlags => ConfigFlags.Msaa4xHint;
public void Init()
{ {
// Initialization texPs3Pad = LoadTexture("resources/ps3.png");
//-------------------------------------------------------------------------------------- texXboxPad = LoadTexture("resources/xbox.png");
const int screenWidth = 800;
const int screenHeight = 450;
// Set MSAA 4X hint before windows creation vibrateButton = new Rectangle();
SetConfigFlags(ConfigFlags.Msaa4xHint);
InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input");
Texture2D texPs3Pad = LoadTexture("resources/ps3.png"); gamepad = 0; // which gamepad to display
Texture2D texXboxPad = LoadTexture("resources/xbox.png"); }
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
int gamepad = 0;
Rectangle vibrateButton = new Rectangle();
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -69,14 +81,13 @@ public class InputGamepad
gamepad++; gamepad++;
} }
Vector2 mousePosition = GetMousePosition(); var mousePosition = GetMousePosition();
vibrateButton = new Rectangle(10, 70.0f + 20 * GetGamepadAxisCount(gamepad) + 20, 75, 24); vibrateButton = new Rectangle(10, 70.0f + 20 * GetGamepadAxisCount(gamepad) + 20, 75, 24);
if (IsMouseButtonPressed(MouseButton.Left) && CheckCollisionPointRec(mousePosition, vibrateButton)) if (IsMouseButtonPressed(MouseButton.Left) && CheckCollisionPointRec(mousePosition, vibrateButton))
{ {
SetGamepadVibration(gamepad, 1.0f, 1.0f, 1.0f); SetGamepadVibration(gamepad, 1.0f, 1.0f, 1.0f);
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -86,9 +97,48 @@ public class InputGamepad
if (IsGamepadAvailable(gamepad)) if (IsGamepadAvailable(gamepad))
{ {
string gamepadName = GetGamepadName_(gamepad); var gamepadName = GetGamepadName_(gamepad);
DrawText($"GP{gamepad}: {gamepadName}", 10, 10, 10, Color.Black); DrawText($"GP{gamepad}: {gamepadName}", 10, 10, 10, Color.Black);
// Get axis values
var leftStickX = GetGamepadAxisMovement(gamepad, GamepadAxis.LeftX);
var leftStickY = GetGamepadAxisMovement(gamepad, GamepadAxis.LeftY);
var rightStickX = GetGamepadAxisMovement(gamepad, GamepadAxis.RightX);
var rightStickY = GetGamepadAxisMovement(gamepad, GamepadAxis.RightY);
var leftTrigger = GetGamepadAxisMovement(gamepad, GamepadAxis.LeftTrigger);
var rightTrigger = GetGamepadAxisMovement(gamepad, GamepadAxis.RightTrigger);
// Calculate deadzones
if (leftStickX > -leftStickDeadzoneX && leftStickX < leftStickDeadzoneX)
{
leftStickX = 0.0f;
}
if (leftStickY > -leftStickDeadzoneY && leftStickY < leftStickDeadzoneY)
{
leftStickY = 0.0f;
}
if (rightStickX > -rightStickDeadzoneX && rightStickX < rightStickDeadzoneX)
{
rightStickX = 0.0f;
}
if (rightStickY > -rightStickDeadzoneY && rightStickY < rightStickDeadzoneY)
{
rightStickY = 0.0f;
}
if (leftTrigger < leftTriggerDeadzone)
{
leftTrigger = -1.0f;
}
if (rightTrigger < rightTriggerDeadzone)
{
rightTrigger = -1.0f;
}
if (gamepadName.Contains(XBOX_ALIAS_1, StringComparison.OrdinalIgnoreCase) || if (gamepadName.Contains(XBOX_ALIAS_1, StringComparison.OrdinalIgnoreCase) ||
gamepadName.Contains(XBOX_ALIAS_2, StringComparison.OrdinalIgnoreCase)) gamepadName.Contains(XBOX_ALIAS_2, StringComparison.OrdinalIgnoreCase))
{ {
@ -166,31 +216,32 @@ public class InputGamepad
} }
// Draw axis: left joystick // Draw axis: left joystick
var leftGamepadColor = Color.Black;
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftThumb))
{
leftGamepadColor = Color.Red;
}
DrawCircle(259, 152, 39, Color.Black); DrawCircle(259, 152, 39, Color.Black);
DrawCircle(259, 152, 34, Color.LightGray); DrawCircle(259, 152, 34, Color.LightGray);
DrawCircle( DrawCircle(259 + (int)(leftStickX * 20), 152 + (int)(leftStickY * 20), 25, leftGamepadColor);
259 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.LeftX) * 20),
152 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.LeftY) * 20),
25,
Color.Black
);
// Draw axis: right joystick // Draw axis: right joystick
var rightGamepadColor = Color.Black;
if (IsGamepadButtonDown(gamepad, GamepadButton.RightThumb))
{
rightGamepadColor = Color.Red;
}
DrawCircle(461, 237, 38, Color.Black); DrawCircle(461, 237, 38, Color.Black);
DrawCircle(461, 237, 33, Color.LightGray); DrawCircle(461, 237, 33, Color.LightGray);
DrawCircle( DrawCircle(461 + (int)(rightStickX * 20), 237 + (int)(rightStickY * 20), 25, rightGamepadColor);
461 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.RightX) * 20),
237 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.RightY) * 20),
25, Color.Black
);
// Draw axis: left-right triggers // Draw axis: left-right triggers
float leftTriggerX = GetGamepadAxisMovement(gamepad, GamepadAxis.LeftTrigger);
float rightTriggerX = GetGamepadAxisMovement(gamepad, GamepadAxis.RightTrigger);
DrawRectangle(170, 30, 15, 70, Color.Gray); DrawRectangle(170, 30, 15, 70, Color.Gray);
DrawRectangle(604, 30, 15, 70, Color.Gray); DrawRectangle(604, 30, 15, 70, Color.Gray);
DrawRectangle(170, 30, 15, (int)(((1.0f + leftTriggerX) / 2.0f) * 70), Color.Red); DrawRectangle(170, 30, 15, (int)(((1 + leftTrigger) / 2) * 70), Color.Red);
DrawRectangle(604, 30, 15, (int)(((1.0f + rightTriggerX) / 2.0f) * 70), Color.Red); DrawRectangle(604, 30, 15, (int)(((1 + rightTrigger) / 2) * 70), Color.Red);
} }
else if (gamepadName.Contains(PS_ALIAS_1, StringComparison.OrdinalIgnoreCase) || gamepadName.Contains(PS_ALIAS_2, StringComparison.OrdinalIgnoreCase)) else if (gamepadName.Contains(PS_ALIAS_1, StringComparison.OrdinalIgnoreCase) || gamepadName.Contains(PS_ALIAS_2, StringComparison.OrdinalIgnoreCase))
{ {
@ -273,45 +324,154 @@ public class InputGamepad
} }
// Draw axis: left joystick // Draw axis: left joystick
var leftGamepadColor = Color.Black;
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftThumb))
{
leftGamepadColor = Color.Red;
}
DrawCircle(319, 255, 35, Color.Black); DrawCircle(319, 255, 35, Color.Black);
DrawCircle(319, 255, 31, Color.LightGray); DrawCircle(319, 255, 31, Color.LightGray);
DrawCircle( DrawCircle(319 + (int)(leftStickX * 20), 255 + (int)(leftStickY * 20), 25, leftGamepadColor);
319 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.LeftX) * 20),
255 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.LeftY) * 20),
25,
Color.Black
);
// Draw axis: right joystick // Draw axis: right joystick
var rightGamepadColor = Color.Black;
if (IsGamepadButtonDown(gamepad, GamepadButton.RightThumb))
{
rightGamepadColor = Color.Red;
}
DrawCircle(475, 255, 35, Color.Black); DrawCircle(475, 255, 35, Color.Black);
DrawCircle(475, 255, 31, Color.LightGray); DrawCircle(475, 255, 31, Color.LightGray);
DrawCircle( DrawCircle(475 + (int)(rightStickX * 20), 255 + (int)(rightStickY * 20), 25, rightGamepadColor);
475 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.RightX) * 20),
255 + (int)(GetGamepadAxisMovement(gamepad, GamepadAxis.RightY) * 20),
25,
Color.Black
);
// Draw axis: left-right triggers // Draw axis: left-right triggers
float leftTriggerX = GetGamepadAxisMovement(gamepad, GamepadAxis.LeftTrigger);
float rightTriggerX = GetGamepadAxisMovement(gamepad, GamepadAxis.RightTrigger);
DrawRectangle(169, 48, 15, 70, Color.Gray); DrawRectangle(169, 48, 15, 70, Color.Gray);
DrawRectangle(611, 48, 15, 70, Color.Gray); DrawRectangle(611, 48, 15, 70, Color.Gray);
DrawRectangle(169, 48, 15, (int)(((1.0f - leftTriggerX) / 2.0f) * 70), Color.Red); DrawRectangle(169, 48, 15, (int)(((1 + leftTrigger) / 2) * 70), Color.Red);
DrawRectangle(611, 48, 15, (int)(((1.0f - rightTriggerX) / 2.0f) * 70), Color.Red); DrawRectangle(611, 48, 15, (int)(((1 + rightTrigger) / 2) * 70), Color.Red);
} }
else else
{ {
DrawText("- GENERIC GAMEPAD -", 280, 180, 20, Color.Gray); // Draw background: generic
// TODO: Draw generic gamepad DrawRectangleRounded(new Rectangle(175, 110, 460, 220), 0.3f, 16, Color.DarkGray);
// Draw buttons: basic
DrawCircle(365, 170, 12, Color.RayWhite);
DrawCircle(405, 170, 12, Color.RayWhite);
DrawCircle(445, 170, 12, Color.RayWhite);
DrawCircle(516, 191, 17, Color.RayWhite);
DrawCircle(551, 227, 17, Color.RayWhite);
DrawCircle(587, 191, 17, Color.RayWhite);
DrawCircle(551, 155, 17, Color.RayWhite);
if (IsGamepadButtonDown(gamepad, GamepadButton.MiddleLeft))
{
DrawCircle(365, 170, 10, Color.Red);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.Middle))
{
DrawCircle(405, 170, 10, Color.Green);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.MiddleRight))
{
DrawCircle(445, 170, 10, Color.Blue);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.RightFaceLeft))
{
DrawCircle(516, 191, 15, Color.Gold);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.RightFaceDown))
{
DrawCircle(551, 227, 15, Color.Blue);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.RightFaceRight))
{
DrawCircle(587, 191, 15, Color.Green);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.RightFaceUp))
{
DrawCircle(551, 155, 15, Color.Red);
}
// Draw buttons: d-pad
DrawRectangle(245, 145, 28, 88, Color.RayWhite);
DrawRectangle(215, 174, 88, 29, Color.RayWhite);
DrawRectangle(247, 147, 24, 84, Color.Black);
DrawRectangle(217, 176, 84, 25, Color.Black);
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftFaceUp))
{
DrawRectangle(247, 147, 24, 29, Color.Red);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftFaceDown))
{
DrawRectangle(247, 147 + 54, 24, 30, Color.Red);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftFaceLeft))
{
DrawRectangle(217, 176, 30, 25, Color.Red);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftFaceRight))
{
DrawRectangle(217 + 54, 176, 30, 25, Color.Red);
}
// Draw buttons: left-right back
DrawRectangleRounded(new Rectangle(215, 98, 100, 10), 0.5f, 16, Color.DarkGray);
DrawRectangleRounded(new Rectangle(495, 98, 100, 10), 0.5f, 16, Color.DarkGray);
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftTrigger1))
{
DrawRectangleRounded(new Rectangle(215, 98, 100, 10), 0.5f, 16, Color.Red);
}
if (IsGamepadButtonDown(gamepad, GamepadButton.RightTrigger1))
{
DrawRectangleRounded(new Rectangle(495, 98, 100, 10), 0.5f, 16, Color.Red);
}
// Draw axis: left joystick
var leftGamepadColor = Color.Black;
if (IsGamepadButtonDown(gamepad, GamepadButton.LeftThumb))
{
leftGamepadColor = Color.Red;
}
DrawCircle(345, 260, 40, Color.Black);
DrawCircle(345, 260, 35, Color.LightGray);
DrawCircle(345 + (int)(leftStickX * 20), 260 + (int)(leftStickY * 20), 25, leftGamepadColor);
// Draw axis: right joystick
var rightGamepadColor = Color.Black;
if (IsGamepadButtonDown(gamepad, GamepadButton.RightThumb))
{
rightGamepadColor = Color.Red;
}
DrawCircle(465, 260, 40, Color.Black);
DrawCircle(465, 260, 35, Color.LightGray);
DrawCircle(465 + (int)(rightStickX * 20), 260 + (int)(rightStickY * 20), 25, rightGamepadColor);
// Draw axis: left-right triggers
DrawRectangle(151, 110, 15, 70, Color.Gray);
DrawRectangle(644, 110, 15, 70, Color.Gray);
DrawRectangle(151, 110, 15, (int)(((1 + leftTrigger) / 2) * 70), Color.Red);
DrawRectangle(644, 110, 15, (int)(((1 + rightTrigger) / 2) * 70), Color.Red);
} }
DrawText($"DETECTED AXIS [{GetGamepadAxisCount(gamepad)}]:", 10, 50, 10, Color.Maroon); DrawText($"DETECTED AXIS [{GetGamepadAxisCount(gamepad)}]:", 10, 50, 10, Color.Maroon);
for (int i = 0; i < GetGamepadAxisCount(gamepad); i++) for (var i = 0; i < GetGamepadAxisCount(gamepad); i++)
{ {
DrawText( DrawText(
$"AXIS {i}: {GetGamepadAxisMovement(gamepad, (GamepadAxis)i)}", $"AXIS {i}: {GetGamepadAxisMovement(gamepad, (GamepadAxis)i):F2}",
20, 20,
70 + 20 * i, 70 + 20 * i,
10, 10,
@ -319,10 +479,10 @@ public class InputGamepad
); );
} }
// Draw vibrate button
DrawRectangleRec(vibrateButton, Color.SkyBlue); DrawRectangleRec(vibrateButton, Color.SkyBlue);
DrawText("VIBRATE", (int)(vibrateButton.X + 14), (int)(vibrateButton.Y + 1), 10, Color.DarkGray); DrawText("VIBRATE", (int)(vibrateButton.X + 14), (int)(vibrateButton.Y + 1), 10, Color.DarkGray);
if (GetGamepadButtonPressed() != (int)GamepadButton.Unknown) if (GetGamepadButtonPressed() != (int)GamepadButton.Unknown)
{ {
DrawText($"DETECTED BUTTON: {GetGamepadButtonPressed()}", 10, 430, 10, Color.Red); DrawText($"DETECTED BUTTON: {GetGamepadButtonPressed()}", 10, 430, 10, Color.Red);
@ -342,12 +502,37 @@ public class InputGamepad
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
// De-Initialization public void Unload()
//-------------------------------------------------------------------------------------- {
UnloadTexture(texPs3Pad); UnloadTexture(texPs3Pad);
UnloadTexture(texXboxPad); UnloadTexture(texXboxPad);
}
CloseWindow(); public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
SetConfigFlags(ConfigFlags.Msaa4xHint); // Set MSAA 4X hint before windows creation
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gamepad");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new InputGamepad();
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; return 0;

View file

@ -18,35 +18,41 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class InputGestures public partial class InputGestures : IExample
{ {
public const int MaxGestureStrings = 20; public const int MaxGestureStrings = 20;
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
private Vector2 touchPosition;
private Rectangle touchArea;
private int gesturesCount;
private string[] gestureStrings;
private Gesture currentGesture;
private Gesture lastGesture;
public string Name => "Core / Input Gestures";
public string Title => "raylib [core] example - input gestures";
public void Init()
{ {
// Initialization touchPosition = new(0, 0);
//-------------------------------------------------------------------------------------- touchArea = new(220, 10, screenWidth - 230, screenHeight - 20);
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures"); gesturesCount = 0;
gestureStrings = new string[MaxGestureStrings];
Vector2 touchPosition = new(0, 0); currentGesture = Gesture.None;
Rectangle touchArea = new(220, 10, screenWidth - 230, screenHeight - 20); lastGesture = Gesture.None;
int gesturesCount = 0;
string[] gestureStrings = new string[MaxGestureStrings];
Gesture currentGesture = Gesture.None;
Gesture lastGesture = Gesture.None;
// SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected // SetGesturesEnabled(0b0000000000001001); // Enable only some gestures to be detected
}
SetTargetFPS(60); public void Update()
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -100,7 +106,7 @@ public class InputGestures
// Reset gestures strings // Reset gestures strings
if (gesturesCount >= MaxGestureStrings) if (gesturesCount >= MaxGestureStrings)
{ {
for (int i = 0; i < MaxGestureStrings; i++) for (var i = 0; i < MaxGestureStrings; i++)
{ {
gestureStrings[i] = " "; gestureStrings[i] = " ";
} }
@ -118,17 +124,17 @@ public class InputGestures
DrawRectangleRec(touchArea, Color.Gray); DrawRectangleRec(touchArea, Color.Gray);
DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, Color.RayWhite); DrawRectangle(225, 15, screenWidth - 240, screenHeight - 30, Color.RayWhite);
DrawText("GESTURES TEST AREA", screenWidth - 270, screenHeight - 40, 20, ColorAlpha(Color.Gray, 0.5f)); DrawText("GESTURES TEST AREA", screenWidth - 270, screenHeight - 40, 20, Fade(Color.Gray, 0.5f));
for (int i = 0; i < gesturesCount; i++) for (var i = 0; i < gesturesCount; i++)
{ {
if (i % 2 == 0) if (i % 2 == 0)
{ {
DrawRectangle(10, 30 + 20 * i, 200, 20, ColorAlpha(Color.LightGray, 0.5f)); DrawRectangle(10, 30 + 20 * i, 200, 20, Fade(Color.LightGray, 0.5f));
} }
else else
{ {
DrawRectangle(10, 30 + 20 * i, 200, 20, ColorAlpha(Color.LightGray, 0.3f)); DrawRectangle(10, 30 + 20 * i, 200, 20, Fade(Color.LightGray, 0.3f));
} }
if (i < gesturesCount - 1) if (i < gesturesCount - 1)
@ -153,10 +159,35 @@ public class InputGestures
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new InputGestures();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;
} }
} }

View file

@ -15,80 +15,105 @@
* *
********************************************************************************************/ ********************************************************************************************/
using System;
using System.Numerics; using System.Numerics;
using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
using static Raylib_cs.Raylib; public partial class InputGesturesTestBed : IExample
public class InputGesturesTestBed
{ {
private const int screenWidth = 800;
private const int screenHeight = 450;
public const int GESTURE_LOG_SIZE = 20; public const int GESTURE_LOG_SIZE = 20;
public const int MAX_TOUCH_COUNT = 32; public const int MAX_TOUCH_COUNT = 32;
//------------------------------------------------------------------------------------ public string Name => "Core / Input Gestures Test Bed";
// Program main entry point
//------------------------------------------------------------------------------------
public static int Main() public string Title => "raylib [core] example - input gestures testbed";
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures testbed"); private Vector2 messagePosition;
Vector2 messagePosition = new Vector2(160, 7);
// Last gesture variables definitions // Last gesture variables definitions
Gesture lastGesture = 0; private Gesture lastGesture;
Vector2 lastGesturePosition = new Vector2(165, 130); private Vector2 lastGesturePosition;
// Gesture log variables definitions // Gesture log variables definitions
// NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures // NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures
string[] gestureLog = new string[GESTURE_LOG_SIZE + 1]; private string[] gestureLog;
for (int i = 0; i < GESTURE_LOG_SIZE; i++)
{
gestureLog[i] = new string(new char[12]);
}
;
// NOTE: The index for the inverted circular queue (moving from last to first direction, then looping around) // NOTE: The index for the inverted circular queue (moving from last to first direction, then looping around)
int gestureLogIndex = GESTURE_LOG_SIZE; private int gestureLogIndex;
Gesture previousGesture = 0; private Gesture previousGesture;
// Log mode values: // Log mode values:
// - 0 shows repeated events // - 0 shows repeated events
// - 1 hides repeated events // - 1 hides repeated events
// - 2 shows repeated events but hide hold events // - 2 shows repeated events but hide hold events
// - 3 hides repeated events and hide hold events // - 3 hides repeated events and hide hold events
int logMode = 1; private int logMode;
Color gestureColor = new Color(0, 0, 0, 255); private Color gestureColor;
Rectangle logButton1 = new Rectangle(53, 7, 48, 26); private Rectangle logButton1;
Rectangle logButton2 = new Rectangle(108, 7, 36, 26); private Rectangle logButton2;
Vector2 gestureLogPosition = new Vector2(10, 10); private Vector2 gestureLogPosition;
// Protractor variables definitions // Protractor variables definitions
float angleLength = 90.0f; private float angleLength;
float currentAngleDegrees = 0.0f; private float currentAngleDegrees;
Vector2 finalVector = new Vector2(0.0f, 0.0f); private Vector2 finalVector;
Vector2 protractorPosition = new Vector2(266.0f, 315.0f); private Vector2 protractorPosition;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second public void Init()
//-------------------------------------------------------------------------------------- {
messagePosition = new Vector2(160, 7);
// Main game loop // Last gesture variables definitions
while (!WindowShouldClose()) // Detect window close button or ESC key lastGesture = 0;
lastGesturePosition = new Vector2(165, 130);
// Gesture log variables definitions
// NOTE: The gesture log uses an array (as an inverted circular queue) to store the performed gestures
gestureLog = new string[GESTURE_LOG_SIZE + 1];
for (var i = 0; i < GESTURE_LOG_SIZE; i++)
{
gestureLog[i] = new string(new char[12]);
}
// NOTE: The index for the inverted circular queue (moving from last to first direction, then looping around)
gestureLogIndex = GESTURE_LOG_SIZE;
previousGesture = 0;
// Log mode values:
// - 0 shows repeated events
// - 1 hides repeated events
// - 2 shows repeated events but hide hold events
// - 3 hides repeated events and hide hold events
logMode = 1;
gestureColor = new Color(0, 0, 0, 255);
logButton1 = new Rectangle(53, 7, 48, 26);
logButton2 = new Rectangle(108, 7, 36, 26);
gestureLogPosition = new Vector2(10, 10);
// Protractor variables definitions
angleLength = 90.0f;
currentAngleDegrees = 0.0f;
finalVector = new Vector2(0.0f, 0.0f);
protractorPosition = new Vector2(266.0f, 315.0f);
}
public void Update()
{ {
// Update // Update
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Handle common gestures data // Handle common gestures data
int i, ii; // Iterators that will be reused by all for loops int i, ii; // Iterators that will be reused by all for loops
Gesture currentGesture = GetGestureDetected(); var currentGesture = GetGestureDetected();
float currentDragDegrees = GetGestureDragAngle(); var currentDragDegrees = GetGestureDragAngle();
float currentPitchDegrees = GetGesturePinchAngle(); var currentPitchDegrees = GetGesturePinchAngle();
int touchCount = GetTouchPointCount(); var touchCount = GetTouchPointCount();
// Handle last gesture // Handle last gesture
if ((currentGesture != 0) && ((int)currentGesture != 4) && (currentGesture != previousGesture)) if ((currentGesture != 0) && ((int)currentGesture != 4) && (currentGesture != previousGesture))
@ -137,7 +162,7 @@ public class InputGesturesTestBed
} }
} }
int fillLog = 0; // Gate variable to be used to allow or not the gesture log to be filled var fillLog = 0; // Gate variable to be used to allow or not the gesture log to be filled
if (currentGesture != 0) if (currentGesture != 0)
{ {
if (logMode == 3) // 3 hides repeated events and hide hold events if (logMode == 3) // 3 hides repeated events and hide hold events
@ -195,19 +220,18 @@ public class InputGesturesTestBed
currentAngleDegrees = 0.0f; // Tap, Doubletap, Hold and Grab currentAngleDegrees = 0.0f; // Tap, Doubletap, Hold and Grab
} }
float currentAngleRadians = var currentAngleRadians =
((currentAngleDegrees + 90.0f) * MathF.PI / 180); // Convert the current angle to Radians ((currentAngleDegrees + 90.0f) * MathF.PI / 180); // Convert the current angle to Radians
// Calculate the final vector for display // Calculate the final vector for display
finalVector = new Vector2( finalVector = new Vector2(
(angleLength * MathF.Sin(currentAngleRadians)) + protractorPosition.X, (angleLength * MathF.Sin(currentAngleRadians)) + protractorPosition.X,
(angleLength * MathF.Cos(currentAngleRadians)) + protractorPosition.Y (angleLength * MathF.Cos(currentAngleRadians)) + protractorPosition.Y
) );
;
// Handle touch and mouse pointer points // Handle touch and mouse pointer points
Vector2[] touchPosition = new Vector2[MAX_TOUCH_COUNT]; var touchPosition = new Vector2[MAX_TOUCH_COUNT];
Vector2 mousePosition = Vector2.Zero; var mousePosition = Vector2.Zero;
if (currentGesture != Gesture.None) if (currentGesture != Gesture.None)
{ {
if (touchCount != 0) if (touchCount != 0)
@ -334,9 +358,9 @@ public class InputGesturesTestBed
DrawText("Angle", (int)protractorPosition.X + 55, (int)protractorPosition.Y + 76, 10, Color.Black); DrawText("Angle", (int)protractorPosition.X + 55, (int)protractorPosition.Y + 76, 10, Color.Black);
// Note: Official it's using raylibs functions for string manipulation. But in C# it will end up in an unsafe handling. // Note: Official it's using raylibs functions for string manipulation. But in C# it will end up in an unsafe handling.
string angleString = currentAngleDegrees.ToString("F3"); var angleString = currentAngleDegrees.ToString("F3");
int angleStringDot = angleString.IndexOf('.'); var angleStringDot = angleString.IndexOf('.');
string angleStringTrim = angleString.Substring(0, angleStringDot + 3); var angleStringTrim = angleString[..(angleStringDot + 3)];
DrawText(angleStringTrim, (int)protractorPosition.X + 55, (int)protractorPosition.Y + 92, 20, gestureColor); DrawText(angleStringTrim, (int)protractorPosition.X + 55, (int)protractorPosition.Y + 92, 20, gestureColor);
DrawCircleV(protractorPosition, 80.0f, Color.White); DrawCircleV(protractorPosition, 80.0f, Color.White);
@ -401,6 +425,30 @@ public class InputGesturesTestBed
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - input gestures testbed");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new InputGesturesTestBed();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
@ -409,7 +457,8 @@ public class InputGesturesTestBed
return 0; return 0;
} }
static string GetGestureName(int gesture) // Get text string for gesture value
private static string GetGestureName(int gesture)
{ {
switch (gesture) switch (gesture)
{ {
@ -441,7 +490,7 @@ public class InputGesturesTestBed
} }
// Get color for gesture value // Get color for gesture value
static Color GetGestureColor(int gesture) private static Color GetGestureColor(int gesture)
{ {
switch (gesture) switch (gesture)
{ {

View file

@ -18,24 +18,23 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class InputKeys public partial class InputKeys : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Input Keys";
public string Title => "raylib [core] example - input keys";
private Vector2 ballPosition;
public void Init()
{ {
// Initialization ballPosition = new((float)screenWidth / 2, (float)screenHeight / 2);
//-------------------------------------------------------------------------------------- }
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - keyboard input"); public void Update()
Vector2 ballPosition = new((float)screenWidth / 2, (float)screenHeight / 2);
SetTargetFPS(60); // Set target frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -73,9 +72,33 @@ public class InputKeys
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - input keys");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new InputKeys();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -19,29 +19,28 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class InputMouse public partial class InputMouse : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Input Mouse";
public string Title => "raylib [core] example - input mouse";
private Vector2 ballPosition;
private Color ballColor;
public void Init()
{ {
// Initialization ballPosition = new(-100.0f, -100.0f);
//-------------------------------------------------------------------------------------- ballColor = Color.DarkBlue;
const int screenWidth = 800; }
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - mouse input"); public void Update()
Vector2 ballPosition = new(-100.0f, -100.0f);
Color ballColor = Color.DarkBlue;
SetTargetFPS(60);
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
if (IsKeyPressed(KeyboardKey.H)) if (IsKeyPressed(KeyboardKey.H))
{ {
if (IsCursorHidden()) if (IsCursorHidden())
@ -68,6 +67,10 @@ public class InputMouse
{ {
ballColor = Color.DarkBlue; ballColor = Color.DarkBlue;
} }
else if (IsMouseButtonPressed(MouseButton.Side))
{
ballColor = Color.Purple;
}
else if (IsMouseButtonPressed(MouseButton.Extra)) else if (IsMouseButtonPressed(MouseButton.Extra))
{ {
ballColor = Color.Yellow; ballColor = Color.Yellow;
@ -105,9 +108,33 @@ public class InputMouse
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - input mouse");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------
var game = new InputMouse();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

View file

@ -17,25 +17,25 @@ using static Raylib_cs.Raylib;
namespace Examples.Core; namespace Examples.Core;
public class InputMouseWheel public partial class InputMouseWheel : IExample
{ {
public static int Main() private const int screenWidth = 800;
private const int screenHeight = 450;
public string Name => "Core / Mouse Wheel";
public string Title => "raylib [core] example - input mouse wheel";
private int boxPositionY;
private int scrollSpeed; // Scrolling speed in pixels
public void Init()
{ {
// Initialization boxPositionY = screenHeight / 2 - 40;
//-------------------------------------------------------------------------------------- scrollSpeed = 4; // Scrolling speed in pixels
const int screenWidth = 800; }
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - input mouse wheel"); public void Update()
int boxPositionY = screenHeight / 2 - 40;
int scrollSpeed = 4; // Scrolling speed in pixels
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose())
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -50,15 +50,39 @@ public class InputMouseWheel
DrawRectangle(screenWidth / 2 - 40, boxPositionY, 80, 80, Color.Maroon); DrawRectangle(screenWidth / 2 - 40, boxPositionY, 80, 80, Color.Maroon);
DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, Color.Gray); DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, Color.Gray);
DrawText($"Box position Y: {boxPositionY}", 10, 40, 20, Color.LightGray); DrawText($"Box position Y: {boxPositionY:000}", 10, 40, 20, Color.LightGray);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
public void Unload()
{
}
public static int Main()
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [core] example - input mouse wheel");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
var game = new InputMouseWheel();
game.Init();
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
game.Update();
}
game.Unload();
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;

Some files were not shown because too many files have changed in this diff Show more