From 5bb16350f3b6846efd5ba5a73063874470e5ce86 Mon Sep 17 00:00:00 2001 From: dnesov Date: Sun, 21 Jan 2024 17:58:11 +0100 Subject: [PATCH] More nullable fixes, make SetTransform a part of base Renderer. --- .../Source/Rendering/RaylibRenderer.cs | 23 ++++-------- DaggerFramework/Source/Rendering/Renderer.cs | 17 ++++++++- .../Source/Rendering/StandardRenderer.cs | 6 ---- .../Source/SceneGraph/EntityLayer.cs | 8 ++--- DaggerFramework/Source/SceneGraph/Scene.cs | 2 ++ TestGame/TestGame.cs | 36 ++++++++----------- TestGame/TestPlayer.cs | 12 +++++-- TestGame/UiLayer.cs | 14 ++++---- 8 files changed, 59 insertions(+), 59 deletions(-) diff --git a/DaggerFramework/Source/Rendering/RaylibRenderer.cs b/DaggerFramework/Source/Rendering/RaylibRenderer.cs index c368475..19f9589 100644 --- a/DaggerFramework/Source/Rendering/RaylibRenderer.cs +++ b/DaggerFramework/Source/Rendering/RaylibRenderer.cs @@ -107,14 +107,7 @@ namespace DaggerFramework.Rendering public override void DrawCircle(float radius, Color color) { - Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, DaggerColorToRaylibColor(color)); - } - - public override void SetTransform(Vector2 position, Vector2 offset, float rotation = 0) - { - _position = position; - _offset = offset; - _rotation = rotation; + Raylib.DrawCircle((int)transformPosition.X, (int)transformPosition.Y, radius, DaggerColorToRaylibColor(color)); } public override void DrawTexture(Texture2d texture, Color tint) @@ -124,7 +117,7 @@ namespace DaggerFramework.Rendering LoadTexture(texture); } - Raylib.DrawTextureV(_texturePool[texture.Handle], _position, DaggerColorToRaylibColor(tint)); + Raylib.DrawTextureV(_texturePool[texture.Handle], transformPosition, DaggerColorToRaylibColor(tint)); } public override void DrawRectangle(Vector2 size, Color color) @@ -132,16 +125,16 @@ namespace DaggerFramework.Rendering // Raylib.DrawRectangleV(_position, size, SystemColorToRaylibColor(color)); Raylib.DrawRectanglePro(new Rectangle() { - x = _position.X, - y = _position.Y, + x = transformPosition.X, + y = transformPosition.Y, width = size.X, height = size.Y - }, _offset, _rotation, DaggerColorToRaylibColor(color)); + }, transformOffset, transformRotation, DaggerColorToRaylibColor(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)transformPosition.X, (int)transformPosition.Y, fontSize, DaggerColorToRaylibColor(color)); } public override void DrawSdfText(string text, int fontSize, Color color) @@ -191,7 +184,7 @@ namespace DaggerFramework.Rendering } var rayFont = _fontPool[font.Handle]; - Raylib.DrawTextPro(rayFont, text, _position, Vector2.Zero, _rotation, font.Size, 0.0f, DaggerColorToRaylibColor(color)); + Raylib.DrawTextPro(rayFont, text, transformPosition, transformOffset, transformRotation, font.Size, 0.0f, DaggerColorToRaylibColor(color)); } protected override int GetMonitorWidth(int monitorId) @@ -266,8 +259,6 @@ namespace DaggerFramework.Rendering private List _texturePool = new(); private List _fontPool = new(); - private Vector2 _position, _offset; - private float _rotation; private Vector2 _windowSize; private bool _vsync; private string _windowTitle = string.Empty; diff --git a/DaggerFramework/Source/Rendering/Renderer.cs b/DaggerFramework/Source/Rendering/Renderer.cs index b28f82d..7d20013 100644 --- a/DaggerFramework/Source/Rendering/Renderer.cs +++ b/DaggerFramework/Source/Rendering/Renderer.cs @@ -79,13 +79,25 @@ namespace DaggerFramework.Rendering /// Background color. public abstract void ClearBackground(Color color); + public void ResetTransform() + { + transformPosition = Vector2.Zero; + transformOffset = Vector2.Zero; + transformRotation = 0.0f; + } + /// /// Sets transforms for the next draw operation. /// /// Global transform position. /// Local offset point around which shapes will rotate. /// Rotation. - public abstract void SetTransform(Vector2 position, Vector2 offset, float rotation = 0.0f); + public void SetTransform(Vector2 position, Vector2 offset, float rotation = 0.0f) + { + transformPosition = position; + transformOffset = offset; + transformRotation = rotation; + } /// /// Sets the transform for the next draw operation. /// @@ -127,6 +139,9 @@ namespace DaggerFramework.Rendering /// Texture to draw. /// Texture tint. public abstract void DrawTexture(Texture2d texture, Color tint); + + protected Vector2 transformPosition, transformOffset; + protected float transformRotation; } public enum Msaa diff --git a/DaggerFramework/Source/Rendering/StandardRenderer.cs b/DaggerFramework/Source/Rendering/StandardRenderer.cs index e8defed..7b1bc34 100644 --- a/DaggerFramework/Source/Rendering/StandardRenderer.cs +++ b/DaggerFramework/Source/Rendering/StandardRenderer.cs @@ -100,12 +100,6 @@ namespace DaggerFramework.Rendering return; } - /// - public override void SetTransform(Vector2 position, Vector2 offset, float rotation = 0) - { - throw new NotImplementedException(); - } - /// public override void SetTransform(Matrix4x4 transform) { diff --git a/DaggerFramework/Source/SceneGraph/EntityLayer.cs b/DaggerFramework/Source/SceneGraph/EntityLayer.cs index 7903e6f..a5f5572 100644 --- a/DaggerFramework/Source/SceneGraph/EntityLayer.cs +++ b/DaggerFramework/Source/SceneGraph/EntityLayer.cs @@ -6,7 +6,7 @@ namespace DaggerFramework.SceneGraph public class EntityLayer : Layer { public List Entities { get => _entities; } - public Camera2d CurrentCamera { get; set; } + public Camera2d? CurrentCamera { get; set; } public EntityLayer(List entities) { @@ -76,8 +76,7 @@ namespace DaggerFramework.SceneGraph protected override void OnBeginDraw(Renderer renderer) { - var hasCamera = CurrentCamera != null; - if (hasCamera) + if (CurrentCamera is not null) { renderer.BeginCamera2d(CurrentCamera.Offset, CurrentCamera.Position, 0f, CurrentCamera.Zoom); } @@ -85,8 +84,7 @@ namespace DaggerFramework.SceneGraph protected override void OnEndDraw(Renderer renderer) { - var hasCamera = CurrentCamera != null; - if (hasCamera) + if (CurrentCamera is not null) { renderer.EndCamera2d(); } diff --git a/DaggerFramework/Source/SceneGraph/Scene.cs b/DaggerFramework/Source/SceneGraph/Scene.cs index 297e3f5..06b0913 100644 --- a/DaggerFramework/Source/SceneGraph/Scene.cs +++ b/DaggerFramework/Source/SceneGraph/Scene.cs @@ -72,6 +72,8 @@ namespace DaggerFramework.SceneGraph layer.BeginDraw(_renderer); layer.Draw(_renderer); } + + Renderer.ResetTransform(); } public void EndDraw() diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 6dae3ea..efcc75e 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -37,8 +37,8 @@ public class TestGame : Game public override void Shutdown() { - _renderer.Shutdown(); - _audioBackend.Shutdown(); + _renderer!.Shutdown(); + _audioBackend?.Shutdown(); } protected override void LoadResources() @@ -61,43 +61,37 @@ public class TestGame : Game protected override void Ready() { - _inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); + _inputHandler!.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) }); _inputHandler.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) }); - _scene.AddLayer("World", _worldLayer); - _worldLayer.AddEntity(new World()); + _scene!.AddLayer("World", _worldLayer!); + + _worldLayer!.AddEntity(new World()); _worldLayer.AddEntity(new TestPlayer()); + _scene.AddLayer("UI", _uiLayer!); + _scene.Start(); } protected override void Run() { - while (_scene.ShouldRun) + while (_scene!.ShouldRun) { _scene.Update(); _scene.BeginDraw(); _scene.EndDraw(); - - double frameTimeMs = _renderer.FrameTime * 1000f; - - _renderer.SetTransform(Vector2.One * 32f, Vector2.Zero, 0f); - - if (_font is not null) - { - _renderer.DrawText(_font, $"{frameTimeMs:0.0} ms", new Color(1f, 1f, 1f, 0.5f)); - } } } - private Renderer _renderer; + private Renderer? _renderer; private ResourceManager _resourceManager = new(); private Sound? _testSound; private Font? _font; - private FmodAudioBackend _audioBackend; - private InputHandler _inputHandler; - private Scene _scene; + private FmodAudioBackend? _audioBackend; + private InputHandler? _inputHandler; + private Scene? _scene; - private UiLayer _uiLayer; - private EntityLayer _worldLayer; + private UiLayer? _uiLayer; + private EntityLayer? _worldLayer; } \ No newline at end of file diff --git a/TestGame/TestPlayer.cs b/TestGame/TestPlayer.cs index 95b7b82..06aff2f 100644 --- a/TestGame/TestPlayer.cs +++ b/TestGame/TestPlayer.cs @@ -14,7 +14,10 @@ public class TestPlayer : RectangleShape2d Current = true, }; - Layer.AddEntity(_camera); + if (Layer is not null) + { + Layer.AddEntity(_camera); + } } protected override void OnUpdate(double dt) @@ -27,10 +30,13 @@ public class TestPlayer : RectangleShape2d var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed; Position += velocity * (float)dt; - _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f); + if (_camera is not null) + { + _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f); + } } private Logger _logger = new(nameof(TestPlayer)); private float _speed = 200f; - private Camera2d _camera; + private Camera2d? _camera; } \ No newline at end of file diff --git a/TestGame/UiLayer.cs b/TestGame/UiLayer.cs index 5c199a3..cb1b4a3 100644 --- a/TestGame/UiLayer.cs +++ b/TestGame/UiLayer.cs @@ -18,7 +18,7 @@ public class UiLayer : Layer protected override void OnUpdate(double dt) { base.OnUpdate(dt); - // _textLabel.Text = $"{MathF.Round(1 / (float)dt)} FPS"; + if (Scene is null) return; _screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize); } @@ -39,7 +39,8 @@ public class UiLayer : Layer private void CreateUiElements() { - _screenContainer = new Container(); + if (Scene is null) return; + _screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize); var style = new PanelStyle() @@ -56,7 +57,7 @@ public class UiLayer : Layer var exampleText = new TextLabel() { - Font = _defaultFont, + Font = _defaultFont!, FontSize = 20, Text = "This Panel will occupy 50% of the screen.\nThis text in particular is inside a panel with an absolute margin of 16px.\nHow cool is that?" }; @@ -89,10 +90,9 @@ public class UiLayer : Layer } - private Font _defaultFont; - private Container _screenContainer; - private Panel _panel, _contentPanel; - private TextLabel _textLabel; + private Font? _defaultFont; + private Container _screenContainer = new(); + private Panel? _panel; private Logger _logger = new(nameof(UiLayer)); } \ No newline at end of file