From d5f5fb5614e666488e78d549dd54a4338c3e1cdb Mon Sep 17 00:00:00 2001 From: dnesov Date: Sat, 17 Jun 2023 23:09:21 +0200 Subject: [PATCH] Update method names for Game, add documentation for renderers, remove position argument from DrawSdfText and DrawDebugText in Renderer. --- Source/Game.cs | 31 +++- Source/Rendering/GlRenderer.cs | 30 +++- Source/Rendering/RaylibRenderer.cs | 6 +- Source/Rendering/Renderer.cs | 82 +++++++++- Source/Rendering/StandardRenderer.cs | 227 --------------------------- Source/SceneGraph/Entities/Text2d.cs | 2 +- TestGame/TestGame.cs | 16 +- 7 files changed, 148 insertions(+), 246 deletions(-) delete mode 100644 Source/Rendering/StandardRenderer.cs diff --git a/Source/Game.cs b/Source/Game.cs index e61c20a..9a94f80 100755 --- a/Source/Game.cs +++ b/Source/Game.cs @@ -3,15 +3,38 @@ namespace DaggerFramework public abstract class Game { public abstract string ResourceRoot { get; } + + /// + /// 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. + /// public void Start() { + Initialize(); LoadResources(); - OnStart(); - MainLoop(); + Ready(); + Run(); } - protected abstract void OnStart(); + + /// + /// Called when it's time to initialize the subsystems. + /// + public abstract void Initialize(); + /// + /// Called when it's time to load the application's resources, such as images or sounds. + /// protected abstract void LoadResources(); - protected abstract void MainLoop(); + /// + /// Called when it's safe to manipulate with the resources or/and systems. + /// + protected abstract void Ready(); + /// + /// Called when everything has been readied to start the main loop. + /// + protected abstract void Run(); + /// + /// Called when the application quits and it's safe to clean up. + /// public abstract void Shutdown(); } } \ No newline at end of file diff --git a/Source/Rendering/GlRenderer.cs b/Source/Rendering/GlRenderer.cs index e60a65a..203c6c4 100644 --- a/Source/Rendering/GlRenderer.cs +++ b/Source/Rendering/GlRenderer.cs @@ -6,16 +6,23 @@ using Silk.NET.OpenGL; namespace DaggerFramework.Rendering { - public class GlRenderer : Renderer + /// + /// A standard, OpenGL-based renderer. + /// + public class StandardRenderer : Renderer { + /// public override Vector2 WindowSize => throw new NotImplementedException(); + /// public override bool ShouldRun => throw new NotImplementedException(); + /// public override void Initialize(RendererSettings settings) { } + /// 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)); } + /// public override void EndFrame() { // throw new NotImplementedException(); EndFrameUnsafe(); } + /// public override void ClearBackground(Color color) { _gl.ClearColor(color.ToSystemColor()); _gl.Clear((uint)ClearBufferMask.ColorBufferBit); } + /// public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size); + + /// public override void Shutdown() { CloseWindowUnsafe(); _glfw.Terminate(); } + /// public override void DrawCircle(float radius, Color color) { throw new NotImplementedException(); } - public override void DrawDebugText(Vector2 position, string text, int fontSize, Color color) + /// + public override void DrawDebugText(string text, int fontSize, Color color) { throw new NotImplementedException(); } + /// public override void DrawRectangle(Vector2 size, Color color) { throw new NotImplementedException(); } + /// public override void DrawTexture(int id, Color tint) { throw new NotImplementedException(); } + /// public override double GetFrameTime() { return 0.0; } + /// public override int LoadTexture(Texture2d texture) { throw new NotImplementedException(); } + /// public override void SetTargetFps(int fps) { return; } + /// public override void SetTransform(Vector2 position, float rotation = 0) { throw new NotImplementedException(); } + /// public override void SetTransform(Matrix4x4 transform) { throw new NotImplementedException(); } + /// public override void SetWindowTitle(string title) { SetWindowTitleUnsafe(title); } + /// public override void SetWindowVSync(bool value) { throw new NotImplementedException(); } + /// 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(); } diff --git a/Source/Rendering/RaylibRenderer.cs b/Source/Rendering/RaylibRenderer.cs index 1763af3..34a1978 100755 --- a/Source/Rendering/RaylibRenderer.cs +++ b/Source/Rendering/RaylibRenderer.cs @@ -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(); } diff --git a/Source/Rendering/Renderer.cs b/Source/Rendering/Renderer.cs index a8712ed..4c1e45d 100755 --- a/Source/Rendering/Renderer.cs +++ b/Source/Rendering/Renderer.cs @@ -2,22 +2,47 @@ using System.Numerics; namespace DaggerFramework.Rendering { + /// + /// An abstract class representing the graphics renderer. + /// public abstract class Renderer { // INIT + /// + /// Creates the renderer window and initializes internal resources. + /// + /// Settings for the rendering window. + /// Rendering settings. public abstract void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings); + /// + /// Initializes internal resources. Should be called before other methods. + /// + /// Rendering settings. public abstract void Initialize(RendererSettings settings); // UTIL + /// + /// Indicates if the renderer will render the next frame. + /// public abstract bool ShouldRun { get; } // WINDOW + /// + /// The size of the render window. + /// public abstract Vector2 WindowSize { get; } + /// + /// Creates the window with a given title and size. + /// + /// Title of the window. + /// Vector2 representing size. 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 + /// + /// Prepares the renderer for drawing the next frame. + /// public abstract void BeginFrame(); + /// + /// Ends rendering of the frame. + /// public abstract void EndFrame(); + /// + /// Clears the render canvas and sets a background color. + /// + /// Background color. public abstract void ClearBackground(Color color); public abstract double GetFrameTime(); + + /// + /// Loads the texture onto the GPU for later use in DrawTexture or other Texture related methods. + /// + /// Texture to load. + /// A texture handler on the GPU. public abstract int LoadTexture(Texture2d texture); + /// + /// Sets transforms for the next draw operation. + /// + /// Global transform position. + /// Rotation. public abstract void SetTransform(Vector2 position, float rotation = 0.0f); + /// + /// Sets the transform for the next draw operation. + /// + /// Transform matrix. public abstract void SetTransform(Matrix4x4 transform); + /// + /// Draws a filled circle. + /// + /// Radius of a circle. + /// Fill color. public abstract void DrawCircle(float radius, Color color); + /// + /// Draws a filled rectangle. + /// + /// Rectangle size. + /// Fill color. 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); + /// + /// Draws a debug text with a default font. + /// + /// + /// + /// + /// + public abstract void DrawDebugText(string text, int fontSize, Color color); + /// + /// Draws text using a signed distance field font atlas. + /// + /// Text to draw. + /// Size of the font. + /// Color of the text. + public abstract void DrawSdfText(string text, int fontSize, Color color); + /// + /// Draws the texture. + /// + /// Texture handle. + /// Texture tint. public abstract void DrawTexture(int id, Color tint); } diff --git a/Source/Rendering/StandardRenderer.cs b/Source/Rendering/StandardRenderer.cs deleted file mode 100644 index 6a9ea02..0000000 --- a/Source/Rendering/StandardRenderer.cs +++ /dev/null @@ -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; - // } -} \ No newline at end of file diff --git a/Source/SceneGraph/Entities/Text2d.cs b/Source/SceneGraph/Entities/Text2d.cs index 21a9750..13443e1 100644 --- a/Source/SceneGraph/Entities/Text2d.cs +++ b/Source/SceneGraph/Entities/Text2d.cs @@ -11,7 +11,7 @@ namespace DaggerFramework.SceneGraph public override void OnDraw(Renderer renderer) { - renderer.DrawDebugText(Position, _contents, _fontSize, _fontColor); + renderer.DrawDebugText(_contents, _fontSize, _fontColor); } private string _contents = string.Empty; diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 95f76d0..f46b9a7 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -6,7 +6,8 @@ using DaggerFramework.Audio; public class TestGame : Game { public override string ResourceRoot => "Resources/"; - protected override void OnStart() + + public override void Initialize() { _renderer = new RaylibRenderer(); _audioBackend = new FmodAudioBackend(); @@ -20,19 +21,21 @@ public class TestGame : Game _renderer.SetTargetFps(60); _audioBackend.Initialize(); - _audioBackend.AddBusEffect(new AudioEffectReverb()); - - _inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); } - protected override void LoadResources() { _soundLoader = new SoundLoader(); _testSound = _soundLoader.Load($"{ResourceRoot}sounds/test_sound.ogg"); } - protected override void MainLoop() + protected override void Ready() + { + _audioBackend.AddBusEffect(new AudioEffectReverb()); + _inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); + } + + protected override void Run() { while (_renderer.ShouldRun) { @@ -57,6 +60,7 @@ public class TestGame : Game _renderer.Shutdown(); _audioBackend.Shutdown(); } + private Renderer _renderer; private SoundLoader _soundLoader; private Sound _testSound;