From f1141d836c7e34da0c6758ea4536dcccc5f3b2e8 Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Fri, 21 Mar 2025 00:35:49 -0400 Subject: [PATCH] WIP First Person Maze example --- Config.cs | 8 +++ GlobalUsings.cs | 5 ++ PlayBark.csproj | 1 + Program.cs | 111 +++++++++++++++++++---------------------- Tracer.cs | 66 ++++++++++++++++++++++++ World.cs | 17 +++++++ resources/cubicmap.png | Bin 0 -> 164 bytes 7 files changed, 147 insertions(+), 61 deletions(-) create mode 100644 Config.cs create mode 100644 Tracer.cs create mode 100644 World.cs create mode 100644 resources/cubicmap.png diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..9e05381 --- /dev/null +++ b/Config.cs @@ -0,0 +1,8 @@ +// I hereby waive this project under the public domain - see UNLICENSE for details. +namespace PlayBark; + +internal class Config +{ + public int Width { get; set; } + public int Height { get; set; } +} \ No newline at end of file diff --git a/GlobalUsings.cs b/GlobalUsings.cs index 2674d67..22016e3 100644 --- a/GlobalUsings.cs +++ b/GlobalUsings.cs @@ -1,2 +1,7 @@ +global using System.Diagnostics; global using System.Numerics; +global using Tomlyn; +global using Tomlyn.Model; +global using static Raylib_cs.Raylib; global using Raylib_cs; +global using PlayBark; \ No newline at end of file diff --git a/PlayBark.csproj b/PlayBark.csproj index c99d292..8ae31a4 100644 --- a/PlayBark.csproj +++ b/PlayBark.csproj @@ -9,6 +9,7 @@ + diff --git a/Program.cs b/Program.cs index 1ecd9e0..04ccc09 100644 --- a/Program.cs +++ b/Program.cs @@ -1,73 +1,62 @@ // I hereby waive this project under the public domain - see UNLICENSE for details. -// Initialization -//-------------------------------------------------------------------------------------- -const int screenWidth = 600; -const int screenHeight = 450; -Raylib.InitWindow(screenWidth, screenHeight, "PlayBark"); - -// Based on WavingCube example: -// https://github.com/raylib-cs/raylib-cs/blob/master/Examples/Models/WavingCubes.cs - -// Initialize the camera -var camera = new Camera3D(); -camera.Position = new Vector3(30.0f, 20.0f, 30.0f); -camera.Target = new Vector3(0.0f, 0.0f, 0.0f); -camera.Up = new Vector3(0.0f, 1.0f, 0.0f); -camera.FovY = 70.0f; -camera.Projection = CameraProjection.Perspective; - -const int numBlocks = 15; - -Raylib.SetTargetFPS(60); - -// Main game loop -while (!Raylib.WindowShouldClose()) +/// +/// Retrieves configuration settings from a TOML file if it exists; otherwise, returns a default configuration. +/// +/// The name of the configuration file (defaults to "config.toml"). +/// A Config object populated with values from the file, or a default Config instance if the file is not found. +static Config ReadConfig(string file) { - var time = Raylib.GetTime(); - var scale = (2.0f + (float)Math.Sin(time)) * 0.7f; + var cfgPath = Path.Combine(Tracer.AppDirectory, file); - var cameraTime = time * 0.3; - - camera.Position.X = (float)Math.Cos(cameraTime) * 40.0f; - camera.Position.Z = (float)Math.Cos(cameraTime) * 40.0f; - - Raylib.BeginDrawing(); - Raylib.ClearBackground(Color.RayWhite); - - Raylib.BeginMode3D(camera); - - Raylib.DrawGrid(10, 5.0f); - - for (int x = 0; x < numBlocks; x++) + if (!File.Exists(cfgPath)) { - for (int y = 0; y < numBlocks; y++) + Tracer.WriteLine("Config file not found. Switching to defaults."); + var config = new Config { - for (int z = 0; z < numBlocks; z++) - { - var blockScale = (x + y + z) / 30.0f; - var scatter = (float)Math.Sin(blockScale * 20.0f + (float)(time * 4.0f)); + Width = 600, + Height = 450 + }; - var cubePos = new Vector3( - (float)(x - numBlocks / 2) * (scale * 3.0f) + scatter, - (float)(x - numBlocks / 2) * (scale * 2.0f) + scatter, - (float)(x - numBlocks / 2) * (scale * 3.0f) + scatter - ); - - var cubeColor = Raylib.ColorFromHSV((float)(((x + y + z) * 18) % 360), 0.75f, 0.9f); - - var cubeSize = (2.4f - scale) * blockScale; - - Raylib.DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor); - } - } + return config; } - Raylib.EndMode3D(); + Tracer.WriteLine($"Discovered config file: {cfgPath}"); + var toml = File.ReadAllText(cfgPath); + var model = Toml.ToModel(toml); - Raylib.DrawFPS(10, 10); - - Raylib.EndDrawing(); + return model; } -Raylib.CloseWindow(); +void Init(int screenWidth, int screenHeight, int fps) +{ + var pos = new Vector3(0.2f, 0.4f, 0.2f); + var target = new Vector3(0.0f, 0.0f, 0.0f); + var up = new Vector3(0.0f, 1.0f, 0.0f); + + InitWindow(screenWidth, screenHeight, $"PlayBark"); + World.Camera(pos, target, up, CameraProjection.Perspective); + SetTargetFPS(fps); +} + +int GameLoop() +{ + var config = ReadConfig("config.toml"); + Init(config.Width, config.Height, 60); + + while (!WindowShouldClose()) + { + BeginDrawing(); + ClearBackground(Color.White); + + DrawFPS(10, 10); + + EndDrawing(); + } + + CloseWindow(); + + return 0; +} + +GameLoop(); diff --git a/Tracer.cs b/Tracer.cs new file mode 100644 index 0000000..78116e7 --- /dev/null +++ b/Tracer.cs @@ -0,0 +1,66 @@ +// I hereby waive this project under the public domain - see UNLICENSE for details. +namespace PlayBark; + +/// +/// Provides debug-only console output methods. +/// These methods are only executed when the application is compiled in DEBUG mode. +/// +internal static class Tracer +{ + /// + /// Writes a line of text to the console, but only when in DEBUG mode. + /// + /// The text to write to the console. + [Conditional("DEBUG")] + internal static void WriteLine(string content) => + Console.WriteLine(content); + + /// + /// Writes text to the console without a newline, but only when in DEBUG mode. + /// + /// The text to write to the console. + [Conditional("DEBUG")] + internal static void Write(string content) => + Console.Write(content); + + /// + /// Writes multiple lines of text to the console, but only when in DEBUG mode. + /// + /// A collection of text lines to write to the console. + [Conditional("DEBUG")] + internal static void WriteLine(IEnumerable contents) + { + foreach (var content in contents) + { + Console.WriteLine(content); + } + } + + /// + /// Writes multiple text entries to the console without newlines, but only when in DEBUG mode. + /// + /// A collection of text entries to write to the console. + [Conditional("DEBUG")] + internal static void Write(IEnumerable contents) + { + foreach (var content in contents) + { + Console.Write(content); + } + } + + /// + /// Gets the current working directory in DEBUG mode or the application's base directory in release mode. + /// + internal static string AppDirectory + { + get + { +#if DEBUG + return Directory.GetCurrentDirectory(); +#else + return AppDomain.CurrentDomain.BaseDirectory; +#endif + } + } +} diff --git a/World.cs b/World.cs new file mode 100644 index 0000000..4c73478 --- /dev/null +++ b/World.cs @@ -0,0 +1,17 @@ +// I hereby waive this project under the public domain - see UNLICENSE for details. +namespace PlayBark; + +internal static class World +{ + public static Camera3D Camera(Vector3 pos, Vector3 target, Vector3 up, CameraProjection projection) + { + Camera3D camera = new(); + camera.Position = pos; + camera.Target = target; + camera.Up = up; + camera.FovY = 45.0f; + camera.Projection = projection; + + return camera; + } +} \ No newline at end of file diff --git a/resources/cubicmap.png b/resources/cubicmap.png new file mode 100644 index 0000000000000000000000000000000000000000..392dbf2e63e43edccd212954cc5742a3aa62c1f2 GIT binary patch literal 164 zcmeAS@N?(olHy`uVBq!ia0vp^3P3Et!2~3KB)#GTQrVs^jv*DdlK%YvZ_n(@=&Q>h zX}3wY;dQHlLGPkjtUDG7YaHS@)YFquw8N!A#YuekN!+4D!}*d?uuyC<9u{ N;OXk;vd$@?2>`lcIXD0S literal 0 HcmV?d00001