Update method names for Game, add documentation for renderers, remove position argument from DrawSdfText and DrawDebugText in Renderer.

This commit is contained in:
2023-06-17 23:09:21 +02:00
parent 52a3b2f87d
commit d5f5fb5614
7 changed files with 148 additions and 246 deletions

View File

@@ -6,16 +6,23 @@ using Silk.NET.OpenGL;
namespace DaggerFramework.Rendering
{
public class GlRenderer : Renderer
/// <summary>
/// A standard, OpenGL-based renderer.
/// </summary>
public class StandardRenderer : Renderer
{
/// <inheritdoc />
public override Vector2 WindowSize => throw new NotImplementedException();
/// <inheritdoc />
public override bool ShouldRun => throw new NotImplementedException();
/// <inheritdoc />
public override void Initialize(RendererSettings settings)
{
}
/// <inheritdoc />
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
{
CreateWindow(windowSettings);
@@ -28,80 +35,97 @@ namespace DaggerFramework.Rendering
_gl.Viewport(new Size((int)_windowSize.X, (int)_windowSize.Y));
}
/// <inheritdoc />
public override void EndFrame()
{
// throw new NotImplementedException();
EndFrameUnsafe();
}
/// <inheritdoc />
public override void ClearBackground(Color color)
{
_gl.ClearColor(color.ToSystemColor());
_gl.Clear((uint)ClearBufferMask.ColorBufferBit);
}
/// <inheritdoc />
public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size);
/// <inheritdoc />
public override void Shutdown()
{
CloseWindowUnsafe();
_glfw.Terminate();
}
/// <inheritdoc />
public override void DrawCircle(float radius, Color color)
{
throw new NotImplementedException();
}
public override void DrawDebugText(Vector2 position, string text, int fontSize, Color color)
/// <inheritdoc />
public override void DrawDebugText(string text, int fontSize, Color color)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override void DrawRectangle(Vector2 size, Color color)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override void DrawTexture(int id, Color tint)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override double GetFrameTime()
{
return 0.0;
}
/// <inheritdoc />
public override int LoadTexture(Texture2d texture)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override void SetTargetFps(int fps)
{
return;
}
/// <inheritdoc />
public override void SetTransform(Vector2 position, float rotation = 0)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override void SetTransform(Matrix4x4 transform)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override void SetWindowTitle(string title)
{
SetWindowTitleUnsafe(title);
}
/// <inheritdoc />
public override void SetWindowVSync(bool value)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override bool WindowShouldClose() => WindowShouldCloseUnsafe();
private unsafe void CreateWindowUnsafe(string title, Vector2 size)
@@ -127,7 +151,7 @@ namespace DaggerFramework.Rendering
_glfw.SwapBuffers(_windowHandle);
}
public override void DrawSdfText(Vector2 position, string text, int fontSize, Color color)
public override void DrawSdfText(string text, int fontSize, Color color)
{
throw new NotImplementedException();
}

View File

@@ -115,12 +115,12 @@ namespace DaggerFramework.Rendering
}, Vector2.Zero, _rotation, DaggerColorToRaylibColor(color));
}
public override void DrawDebugText(Vector2 position, string text, int fontSize, Color color)
public override void DrawDebugText(string text, int fontSize, Color color)
{
Raylib.DrawText(text, (int)position.X, (int)position.Y, fontSize, DaggerColorToRaylibColor(color));
Raylib.DrawText(text, (int)_position.X, (int)_position.Y, fontSize, DaggerColorToRaylibColor(color));
}
public override void DrawSdfText(Vector2 position, string text, int fontSize, Color color)
public override void DrawSdfText(string text, int fontSize, Color color)
{
throw new NotImplementedException();
}

View File

@@ -2,22 +2,47 @@ using System.Numerics;
namespace DaggerFramework.Rendering
{
/// <summary>
/// An abstract class representing the graphics renderer.
/// </summary>
public abstract class Renderer
{
// INIT
/// <summary>
/// Creates the renderer window and initializes internal resources.
/// </summary>
/// <param name="windowSettings">Settings for the rendering window.</param>
/// <param name="renderSettings">Rendering settings.</param>
public abstract void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings);
/// <summary>
/// Initializes internal resources. Should be called before other methods.
/// </summary>
/// <param name="settings">Rendering settings.</param>
public abstract void Initialize(RendererSettings settings);
// UTIL
/// <summary>
/// Indicates if the renderer will render the next frame.
/// </summary>
public abstract bool ShouldRun { get; }
// WINDOW
/// <summary>
/// The size of the render window.
/// </summary>
public abstract Vector2 WindowSize { get; }
/// <summary>
/// Creates the window with a given title and size.
/// </summary>
/// <param name="title">Title of the window.</param>
/// <param name="size">Vector2 representing size.</param>
public abstract void CreateWindow(string title, Vector2 size);
public void CreateWindow(WindowSettings windowSettings)
{
CreateWindow(windowSettings.Title, windowSettings.Size);
}
// TODO: use properties for these.
public abstract void SetWindowTitle(string title);
public abstract void SetWindowVSync(bool value);
public abstract void SetTargetFps(int fps);
@@ -25,17 +50,70 @@ namespace DaggerFramework.Rendering
public abstract void Shutdown();
// DRAWING
/// <summary>
/// Prepares the renderer for drawing the next frame.
/// </summary>
public abstract void BeginFrame();
/// <summary>
/// Ends rendering of the frame.
/// </summary>
public abstract void EndFrame();
/// <summary>
/// Clears the render canvas and sets a background color.
/// </summary>
/// <param name="color">Background color.</param>
public abstract void ClearBackground(Color color);
public abstract double GetFrameTime();
/// <summary>
/// Loads the texture onto the GPU for later use in DrawTexture or other Texture related methods.
/// </summary>
/// <param name="texture">Texture to load.</param>
/// <returns>A texture handler on the GPU.</returns>
public abstract int LoadTexture(Texture2d texture);
/// <summary>
/// Sets transforms for the next draw operation.
/// </summary>
/// <param name="position">Global transform position.</param>
/// <param name="rotation">Rotation.</param>
public abstract void SetTransform(Vector2 position, float rotation = 0.0f);
/// <summary>
/// Sets the transform for the next draw operation.
/// </summary>
/// <param name="transform">Transform matrix.</param>
public abstract void SetTransform(Matrix4x4 transform);
/// <summary>
/// Draws a filled circle.
/// </summary>
/// <param name="radius">Radius of a circle.</param>
/// <param name="color">Fill color.</param>
public abstract void DrawCircle(float radius, Color color);
/// <summary>
/// Draws a filled rectangle.
/// </summary>
/// <param name="size">Rectangle size.</param>
/// <param name="color">Fill color.</param>
public abstract void DrawRectangle(Vector2 size, Color color);
public abstract void DrawDebugText(Vector2 position, string text, int fontSize, Color color);
public abstract void DrawSdfText(Vector2 position, string text, int fontSize, Color color);
/// <summary>
/// Draws a debug text with a default font.
/// </summary>
/// <param name="position"></param>
/// <param name="text"></param>
/// <param name="fontSize"></param>
/// <param name="color"></param>
public abstract void DrawDebugText(string text, int fontSize, Color color);
/// <summary>
/// Draws text using a signed distance field font atlas.
/// </summary>
/// <param name="text">Text to draw.</param>
/// <param name="fontSize">Size of the font.</param>
/// <param name="color">Color of the text.</param>
public abstract void DrawSdfText(string text, int fontSize, Color color);
/// <summary>
/// Draws the texture.
/// </summary>
/// <param name="id">Texture handle.</param>
/// <param name="tint">Texture tint.</param>
public abstract void DrawTexture(int id, Color tint);
}

View File

@@ -1,227 +0,0 @@
using System.Drawing;
using System.Numerics;
using System.Text;
// using Veldrid;
// using Veldrid.Sdl2;
// using Veldrid.SPIRV;
// using Veldrid.StartupUtilities;
namespace DaggerFramework.Rendering
{
// public class StandardRenderer : Renderer
// {
// public override void BeginFrame()
// {
// _commandList.Begin();
// _commandList.SetFramebuffer(_graphicsDevice.SwapchainFramebuffer);
// }
// public override void ClearBackground(Color color)
// {
// _commandList.ClearColorTarget(0, SystemColorToRgbaFloat(color));
// }
// public override void CloseWindow()
// {
// _window.Close();
// }
// public override void CreateWindow(string title, Vector2 size)
// {
// var windowCreateInfo = new WindowCreateInfo()
// {
// // TODO: add position property to CreateWindow.
// X = (int)size.X / 2,
// Y = (int)size.Y / 2,
// WindowWidth = (int)size.X,
// WindowHeight = (int)size.Y,
// WindowTitle = title,
// };
// _window = VeldridStartup.CreateWindow(ref windowCreateInfo);
// }
// public override void DrawCircle(float radius, Color color)
// {
// throw new NotImplementedException();
// }
// public override void DrawDebugText(Vector2 position, string text, int fontSize, Color color)
// {
// throw new NotImplementedException();
// }
// public override void DrawRectangle(Vector2 size, Color color)
// {
// _commandList.SetVertexBuffer(0, _vertexBuffer);
// _commandList.SetIndexBuffer(_indexBuffer, IndexFormat.UInt16);
// _commandList.SetPipeline(_graphicsPipeline);
// _commandList.SetGraphicsResourceSet(0, _resourceSet);
// _graphicsDevice.UpdateBuffer(_uniformBuffer, 0, SystemColorToRgbaFloat(color));
// _commandList.DrawIndexed(indexCount: 4, instanceCount: 1, 0, 0, 0);
// }
// public override void DrawTexture(int id, Color tint)
// {
// throw new NotImplementedException();
// }
// public override void EndFrame()
// {
// _commandList.End();
// _graphicsDevice.SubmitCommands(_commandList);
// _graphicsDevice.SwapBuffers();
// }
// public override double GetFrameTime()
// {
// return 0.16f;
// }
// private void CreateResources()
// {
// _resourceFactory = _graphicsDevice.ResourceFactory;
// _commandList = _resourceFactory.CreateCommandList();
// _vertexBuffer = _resourceFactory.CreateBuffer(new BufferDescription(4 * 8, BufferUsage.VertexBuffer));
// _indexBuffer = _resourceFactory.CreateBuffer(new BufferDescription(4 * sizeof(ushort), BufferUsage.IndexBuffer));
// _uniformBuffer = _resourceFactory.CreateBuffer(new BufferDescription(4 * sizeof(float), BufferUsage.UniformBuffer));
// FillBuffers();
// var vertexDesc = new ShaderDescription(ShaderStages.Vertex, Encoding.UTF8.GetBytes(_colorRectShader.VertexSource), "main");
// var fragmentDesc = new ShaderDescription(ShaderStages.Fragment, Encoding.UTF8.GetBytes(_colorRectShader.FragmentSource), "main");
// _shaders = _resourceFactory.CreateFromSpirv(vertexDesc, fragmentDesc);
// CreatePipeline();
// }
// private void CreatePipeline()
// {
// VertexLayoutDescription vertexLayout = new VertexLayoutDescription(
// new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2));
// var pipelineDesc = new GraphicsPipelineDescription();
// pipelineDesc.BlendState = BlendStateDescription.SingleOverrideBlend;
// pipelineDesc.RasterizerState = new RasterizerStateDescription(
// cullMode: FaceCullMode.Back,
// fillMode: PolygonFillMode.Solid,
// frontFace: FrontFace.Clockwise,
// depthClipEnabled: true,
// scissorTestEnabled: false);
// pipelineDesc.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
// var layout = _resourceFactory.CreateResourceLayout(new ResourceLayoutDescription(
// new ResourceLayoutElementDescription("color", ResourceKind.UniformBuffer, ShaderStages.Fragment)
// ));
// _resourceSet = _resourceFactory.CreateResourceSet(new ResourceSetDescription(layout, _uniformBuffer));
// pipelineDesc.ResourceLayouts = new ResourceLayout[] { layout };
// pipelineDesc.ShaderSet = new ShaderSetDescription(vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, _shaders);
// pipelineDesc.Outputs = _graphicsDevice.SwapchainFramebuffer.OutputDescription;
// _graphicsPipeline = _resourceFactory.CreateGraphicsPipeline(pipelineDesc);
// }
// private void FillBuffers()
// {
// _graphicsDevice.UpdateBuffer(_vertexBuffer, 0, _quadVertices);
// _graphicsDevice.UpdateBuffer(_indexBuffer, 0, _quadIndices);
// }
// public override void Initialize(RendererSettings settings)
// {
// _settings = settings;
// _graphicsDevice = VeldridStartup.CreateGraphicsDevice(_window, new GraphicsDeviceOptions
// {
// PreferStandardClipSpaceYDirection = true,
// PreferDepthRangeZeroToOne = true
// });
// CreateResources();
// }
// public override int LoadTexture(Texture2d texture)
// {
// throw new NotImplementedException();
// }
// public override int LoadTexture(string path)
// {
// // throw new NotImplementedException();
// return -1;
// }
// public override void SetTargetFps(int fps)
// {
// _window.PollIntervalInMs = 1 / fps;
// }
// public override void SetTransform(Vector2 position)
// {
// // throw new NotImplementedException();
// }
// public override void SetWindowTitle(string title)
// {
// throw new NotImplementedException();
// }
// public override void SetWindowVSync(bool value)
// {
// throw new NotImplementedException();
// }
// public override bool WindowShouldClose()
// {
// _window.PumpEvents();
// return !_window.Exists;
// }
// private RgbaFloat SystemColorToRgbaFloat(System.Drawing.Color color)
// {
// return new RgbaFloat(color.R, color.G, color.B, color.A);
// }
// public override void SetTransform(Matrix4x4 transform)
// {
// _transform = transform;
// }
// // Transform
// private Matrix4x4 _transform;
// // TEMP: quad stuff.
// private readonly Vector2[] _quadVertices =
// {
// new Vector2(-0.5f, 0.5f),
// new Vector2(0.5f, 0.5f),
// new Vector2(-0.5f, -0.5f),
// new Vector2(0.5f, -0.5f)
// };
// private readonly ushort[] _quadIndices = { 0, 1, 2, 3 };
// private ColorRectShader _colorRectShader = new ColorRectShader();
// private Sdl2Window _window;
// private RendererSettings _settings;
// private GraphicsDevice _graphicsDevice;
// // Resources
// private ResourceFactory _resourceFactory;
// private CommandList _commandList;
// private Pipeline _graphicsPipeline;
// private Veldrid.Shader[] _shaders;
// // Quad resources
// private DeviceBuffer _vertexBuffer;
// private DeviceBuffer _indexBuffer;
// private DeviceBuffer _uniformBuffer;
// private ResourceSet _resourceSet;
// }
}