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

@@ -3,15 +3,38 @@ namespace DaggerFramework
public abstract class Game public abstract class Game
{ {
public abstract string ResourceRoot { get; } public abstract string ResourceRoot { get; }
/// <summary>
/// Starts the game application.
/// This involves initializing the required subsystems, loading resources from the disk, and then preparing the subsystems for running in the main loop.
/// </summary>
public void Start() public void Start()
{ {
Initialize();
LoadResources(); LoadResources();
OnStart(); Ready();
MainLoop(); Run();
} }
protected abstract void OnStart();
/// <summary>
/// Called when it's time to initialize the subsystems.
/// </summary>
public abstract void Initialize();
/// <summary>
/// Called when it's time to load the application's resources, such as images or sounds.
/// </summary>
protected abstract void LoadResources(); protected abstract void LoadResources();
protected abstract void MainLoop(); /// <summary>
/// Called when it's safe to manipulate with the resources or/and systems.
/// </summary>
protected abstract void Ready();
/// <summary>
/// Called when everything has been readied to start the main loop.
/// </summary>
protected abstract void Run();
/// <summary>
/// Called when the application quits and it's safe to clean up.
/// </summary>
public abstract void Shutdown(); public abstract void Shutdown();
} }
} }

View File

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

View File

@@ -115,12 +115,12 @@ namespace DaggerFramework.Rendering
}, Vector2.Zero, _rotation, DaggerColorToRaylibColor(color)); }, 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(); throw new NotImplementedException();
} }

View File

@@ -2,22 +2,47 @@ using System.Numerics;
namespace DaggerFramework.Rendering namespace DaggerFramework.Rendering
{ {
/// <summary>
/// An abstract class representing the graphics renderer.
/// </summary>
public abstract class Renderer public abstract class Renderer
{ {
// INIT // 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); 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); public abstract void Initialize(RendererSettings settings);
// UTIL // UTIL
/// <summary>
/// Indicates if the renderer will render the next frame.
/// </summary>
public abstract bool ShouldRun { get; } public abstract bool ShouldRun { get; }
// WINDOW // WINDOW
/// <summary>
/// The size of the render window.
/// </summary>
public abstract Vector2 WindowSize { get; } 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 abstract void CreateWindow(string title, Vector2 size);
public void CreateWindow(WindowSettings windowSettings) public void CreateWindow(WindowSettings windowSettings)
{ {
CreateWindow(windowSettings.Title, windowSettings.Size); CreateWindow(windowSettings.Title, windowSettings.Size);
} }
// TODO: use properties for these.
public abstract void SetWindowTitle(string title); public abstract void SetWindowTitle(string title);
public abstract void SetWindowVSync(bool value); public abstract void SetWindowVSync(bool value);
public abstract void SetTargetFps(int fps); public abstract void SetTargetFps(int fps);
@@ -25,17 +50,70 @@ namespace DaggerFramework.Rendering
public abstract void Shutdown(); public abstract void Shutdown();
// DRAWING // DRAWING
/// <summary>
/// Prepares the renderer for drawing the next frame.
/// </summary>
public abstract void BeginFrame(); public abstract void BeginFrame();
/// <summary>
/// Ends rendering of the frame.
/// </summary>
public abstract void EndFrame(); 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 void ClearBackground(Color color);
public abstract double GetFrameTime(); 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); 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); 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); 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); 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 DrawRectangle(Vector2 size, Color color);
public abstract void DrawDebugText(Vector2 position, string text, int fontSize, Color color); /// <summary>
public abstract void DrawSdfText(Vector2 position, string text, int fontSize, Color color); /// 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); 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;
// }
}

View File

@@ -11,7 +11,7 @@ namespace DaggerFramework.SceneGraph
public override void OnDraw(Renderer renderer) public override void OnDraw(Renderer renderer)
{ {
renderer.DrawDebugText(Position, _contents, _fontSize, _fontColor); renderer.DrawDebugText(_contents, _fontSize, _fontColor);
} }
private string _contents = string.Empty; private string _contents = string.Empty;

View File

@@ -6,7 +6,8 @@ using DaggerFramework.Audio;
public class TestGame : Game public class TestGame : Game
{ {
public override string ResourceRoot => "Resources/"; public override string ResourceRoot => "Resources/";
protected override void OnStart()
public override void Initialize()
{ {
_renderer = new RaylibRenderer(); _renderer = new RaylibRenderer();
_audioBackend = new FmodAudioBackend(); _audioBackend = new FmodAudioBackend();
@@ -20,19 +21,21 @@ public class TestGame : Game
_renderer.SetTargetFps(60); _renderer.SetTargetFps(60);
_audioBackend.Initialize(); _audioBackend.Initialize();
_audioBackend.AddBusEffect<AudioEffectReverb>(new AudioEffectReverb());
_inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) });
} }
protected override void LoadResources() protected override void LoadResources()
{ {
_soundLoader = new SoundLoader(); _soundLoader = new SoundLoader();
_testSound = _soundLoader.Load($"{ResourceRoot}sounds/test_sound.ogg"); _testSound = _soundLoader.Load($"{ResourceRoot}sounds/test_sound.ogg");
} }
protected override void MainLoop() protected override void Ready()
{
_audioBackend.AddBusEffect<AudioEffectReverb>(new AudioEffectReverb());
_inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) });
}
protected override void Run()
{ {
while (_renderer.ShouldRun) while (_renderer.ShouldRun)
{ {
@@ -57,6 +60,7 @@ public class TestGame : Game
_renderer.Shutdown(); _renderer.Shutdown();
_audioBackend.Shutdown(); _audioBackend.Shutdown();
} }
private Renderer _renderer; private Renderer _renderer;
private SoundLoader _soundLoader; private SoundLoader _soundLoader;
private Sound _testSound; private Sound _testSound;