From a43108fd4b1bae20203c34ab2bc72f578cb305fc Mon Sep 17 00:00:00 2001 From: MrScautHD <65916181+MrScautHD@users.noreply.github.com> Date: Sat, 17 Aug 2024 21:30:49 +0200 Subject: [PATCH] Start working on Graphics --- src/Bliss.Test/Game.cs | 48 +++++++------------- src/Bliss.Test/GameSettings.cs | 3 ++ src/Bliss/Bliss.csproj | 1 - src/Bliss/CSharp/Rendering/Graphics.cs | 61 ++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 src/Bliss/CSharp/Rendering/Graphics.cs diff --git a/src/Bliss.Test/Game.cs b/src/Bliss.Test/Game.cs index af0cd73..3a65f64 100644 --- a/src/Bliss.Test/Game.cs +++ b/src/Bliss.Test/Game.cs @@ -1,10 +1,9 @@ using Bliss.CSharp; using Bliss.CSharp.Logging; -using Bliss.CSharp.Shaders; +using Bliss.CSharp.Rendering; using Bliss.CSharp.Windowing; using Veldrid; using Veldrid.Sdl2; -using Veldrid.StartupUtilities; namespace Bliss.Test; @@ -15,7 +14,9 @@ public class Game : Disposable { public Window Window { get; private set; } public GraphicsDevice GraphicsDevice { get; private set; } + public CommandList CommandList { get; private set; } + public Graphics Graphics { get; private set; } private double _fixedFrameRate; @@ -41,7 +42,7 @@ public void Run() { PreferDepthRangeZeroToOne = true }; - this.Window = new Window(this.Settings.Width, this.Settings.Height, this.Settings.Title, options, VeldridStartup.GetPlatformDefaultBackend(), out GraphicsDevice graphicsDevice); + this.Window = new Window(this.Settings.Width, this.Settings.Height, this.Settings.Title, options, this.Settings.Backend, out GraphicsDevice graphicsDevice); this.GraphicsDevice = graphicsDevice; Logger.Info("Initialize time..."); @@ -50,12 +51,15 @@ public void Run() { Logger.Info($"Set target FPS to: {this.Settings.TargetFps}"); this.SetTargetFps(this.Settings.TargetFps); - Logger.Info("Create CommandList"); + Logger.Info("Initialize command list..."); this.CommandList = this.GraphicsDevice.ResourceFactory.CreateCommandList(); + Logger.Info("Initialize graphics..."); + this.Graphics = new Graphics(this.GraphicsDevice, this.CommandList); + this.Init(); - Logger.Info("Start main Loops..."); + Logger.Info("Start main loops..."); while (Window.Exists) { if (this.GetTargetFps() != 0 && Time.Timer.Elapsed.TotalSeconds <= this._fixedFrameRate) { continue; @@ -74,17 +78,17 @@ public void Run() { this._fixedUpdateTimer -= this._fixedUpdateTimeStep; } - this.Draw(this.GraphicsDevice, this.CommandList); + this.Graphics.BeginDrawing(); + this.Graphics.ClearBackground(0, RgbaFloat.Grey); + this.Draw(this.Graphics); + this.Graphics.EndDrawing(); } Logger.Warn("Application shuts down!"); this.OnClose(); } - protected virtual void Init() { - (Shader, Shader) shader = ShaderHelper.Load(this.GraphicsDevice.ResourceFactory, "content/shaders/default_shader.vert", "content/shaders/default_shader.frag"); - - } + protected virtual void Init() { } protected virtual void Update() { } @@ -92,29 +96,7 @@ protected virtual void AfterUpdate() { } protected virtual void FixedUpdate() { } - protected virtual void Draw(GraphicsDevice graphicsDevice, CommandList commandList) { - //commandList.Begin(); - // - //commandList.SetFramebuffer(graphicsDevice.SwapchainFramebuffer); - //commandList.ClearColorTarget(0, RgbaFloat.Grey); - // - //commandList.SetVertexBuffer(0, this._vertexBuffer); - //commandList.SetIndexBuffer(this._indexBuffer, IndexFormat.UInt16); - //commandList.SetPipeline(this.Pipeline); - //commandList.SetGraphicsResourceSet(0, this.ResourceSet); - // - //commandList.DrawIndexed( - // indexCount: 4, - // instanceCount: 1, - // indexStart: 0, - // vertexOffset: 0, - // instanceStart: 0); - // - //commandList.End(); - //graphicsDevice.SubmitCommands(commandList); - // - //graphicsDevice.SwapBuffers(); - } + protected virtual void Draw(Graphics graphics) { } protected virtual void OnClose() { } diff --git a/src/Bliss.Test/GameSettings.cs b/src/Bliss.Test/GameSettings.cs index 3de2fd5..0cf5cd1 100644 --- a/src/Bliss.Test/GameSettings.cs +++ b/src/Bliss.Test/GameSettings.cs @@ -1,5 +1,6 @@ using System.Reflection; using Veldrid; +using Veldrid.StartupUtilities; namespace Bliss.Test; @@ -10,6 +11,7 @@ public struct GameSettings { public int Height { get; init; } public int TargetFps { get; init; } public double FixedTimeStep { get; init; } + public GraphicsBackend Backend { get; init; } public TextureSampleCount SampleCount { get; init; } /// @@ -21,6 +23,7 @@ public GameSettings() { this.Height = 720; this.TargetFps = 0; this.FixedTimeStep = 1.0F / 60.0F; + this.Backend = VeldridStartup.GetPlatformDefaultBackend(); this.SampleCount = 0; } } \ No newline at end of file diff --git a/src/Bliss/Bliss.csproj b/src/Bliss/Bliss.csproj index 071e39b..cfd86f2 100644 --- a/src/Bliss/Bliss.csproj +++ b/src/Bliss/Bliss.csproj @@ -10,7 +10,6 @@ - diff --git a/src/Bliss/CSharp/Rendering/Graphics.cs b/src/Bliss/CSharp/Rendering/Graphics.cs new file mode 100644 index 0000000..83c8e9f --- /dev/null +++ b/src/Bliss/CSharp/Rendering/Graphics.cs @@ -0,0 +1,61 @@ +using Bliss.CSharp.Textures; +using Veldrid; + +namespace Bliss.CSharp.Rendering; + +public class Graphics { + public GraphicsDevice GraphicsDevice { get; private set; } + public CommandList CommandList { get; private set; } + + /// + /// Initializes a new instance of the class with the specified graphics device and command list. + /// + /// The graphics device used for rendering operations. + /// The command list used to issue rendering commands. + public Graphics(GraphicsDevice graphicsDevice, CommandList commandList) { + this.GraphicsDevice = graphicsDevice; + this.CommandList = commandList; + } + + /// + /// Begins the drawing operations. + /// + public void BeginDrawing() { + this.CommandList.Begin(); + this.CommandList.SetFramebuffer(this.GraphicsDevice.SwapchainFramebuffer); + } + + /// + /// Ends the drawing operations. + /// + public void EndDrawing() { + this.CommandList.End(); + this.GraphicsDevice.SubmitCommands(this.CommandList); + this.GraphicsDevice.SwapBuffers(); + } + + /// + /// Clears the background color and depth stencil of the command list. + /// + /// The index of the color target to clear. + /// The color used to clear the background. + public void ClearBackground(uint index, RgbaFloat clearColor) { + this.CommandList.ClearColorTarget(index, clearColor); + + if (this.GraphicsDevice.SwapchainFramebuffer.DepthTarget != null) { + this.CommandList.ClearDepthStencil(1, 0); + } + } + + /* --------------------------------- Texture Drawing --------------------------------- */ + + public void Draw(Texture2D texture) { + // Something like that get added here! + } + + /* --------------------------------- Text Drawing --------------------------------- */ + + /* --------------------------------- Shape Drawing --------------------------------- */ + + /* --------------------------------- Model Drawing --------------------------------- */ +} \ No newline at end of file