Skip to content

Commit

Permalink
Start working on Graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
MrScautHD committed Aug 17, 2024
1 parent 1f1f738 commit a43108f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 34 deletions.
48 changes: 15 additions & 33 deletions src/Bliss.Test/Game.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;

Expand All @@ -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...");
Expand All @@ -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;
Expand All @@ -74,47 +78,25 @@ 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() { }

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() { }

Expand Down
3 changes: 3 additions & 0 deletions src/Bliss.Test/GameSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using Veldrid;
using Veldrid.StartupUtilities;

namespace Bliss.Test;

Expand All @@ -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; }

/// <summary>
Expand All @@ -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;
}
}
1 change: 0 additions & 1 deletion src/Bliss/Bliss.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<ItemGroup>
<Folder Include="CSharp\Audio\" />
<Folder Include="CSharp\Rendering\" />
</ItemGroup>

<!-- Libraries -->
Expand Down
61 changes: 61 additions & 0 deletions src/Bliss/CSharp/Rendering/Graphics.cs
Original file line number Diff line number Diff line change
@@ -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; }

/// <summary>
/// Initializes a new instance of the <see cref="Graphics"/> class with the specified graphics device and command list.
/// </summary>
/// <param name="graphicsDevice">The graphics device used for rendering operations.</param>
/// <param name="commandList">The command list used to issue rendering commands.</param>
public Graphics(GraphicsDevice graphicsDevice, CommandList commandList) {
this.GraphicsDevice = graphicsDevice;
this.CommandList = commandList;
}

/// <summary>
/// Begins the drawing operations.
/// </summary>
public void BeginDrawing() {
this.CommandList.Begin();
this.CommandList.SetFramebuffer(this.GraphicsDevice.SwapchainFramebuffer);
}

/// <summary>
/// Ends the drawing operations.
/// </summary>
public void EndDrawing() {
this.CommandList.End();
this.GraphicsDevice.SubmitCommands(this.CommandList);
this.GraphicsDevice.SwapBuffers();
}

/// <summary>
/// Clears the background color and depth stencil of the command list.
/// </summary>
/// <param name="index">The index of the color target to clear.</param>
/// <param name="clearColor">The color used to clear the background.</param>
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 --------------------------------- */
}

0 comments on commit a43108f

Please sign in to comment.