More nullable fixes, make SetTransform a part of base Renderer.

This commit is contained in:
2024-01-21 17:58:11 +01:00
parent 5f4e32e2e0
commit 5bb16350f3
8 changed files with 59 additions and 59 deletions

View File

@@ -107,14 +107,7 @@ namespace DaggerFramework.Rendering
public override void DrawCircle(float radius, Color color) public override void DrawCircle(float radius, Color color)
{ {
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, DaggerColorToRaylibColor(color)); Raylib.DrawCircle((int)transformPosition.X, (int)transformPosition.Y, radius, DaggerColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, Vector2 offset, float rotation = 0)
{
_position = position;
_offset = offset;
_rotation = rotation;
} }
public override void DrawTexture(Texture2d texture, Color tint) public override void DrawTexture(Texture2d texture, Color tint)
@@ -124,7 +117,7 @@ namespace DaggerFramework.Rendering
LoadTexture(texture); 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) public override void DrawRectangle(Vector2 size, Color color)
@@ -132,16 +125,16 @@ namespace DaggerFramework.Rendering
// Raylib.DrawRectangleV(_position, size, SystemColorToRaylibColor(color)); // Raylib.DrawRectangleV(_position, size, SystemColorToRaylibColor(color));
Raylib.DrawRectanglePro(new Rectangle() Raylib.DrawRectanglePro(new Rectangle()
{ {
x = _position.X, x = transformPosition.X,
y = _position.Y, y = transformPosition.Y,
width = size.X, width = size.X,
height = size.Y height = size.Y
}, _offset, _rotation, DaggerColorToRaylibColor(color)); }, transformOffset, transformRotation, DaggerColorToRaylibColor(color));
} }
public override void DrawDebugText(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)transformPosition.X, (int)transformPosition.Y, fontSize, DaggerColorToRaylibColor(color));
} }
public override void DrawSdfText(string text, int fontSize, Color color) public override void DrawSdfText(string text, int fontSize, Color color)
@@ -191,7 +184,7 @@ namespace DaggerFramework.Rendering
} }
var rayFont = _fontPool[font.Handle]; 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) protected override int GetMonitorWidth(int monitorId)
@@ -266,8 +259,6 @@ namespace DaggerFramework.Rendering
private List<Texture2D> _texturePool = new(); private List<Texture2D> _texturePool = new();
private List<Raylib_cs.Font> _fontPool = new(); private List<Raylib_cs.Font> _fontPool = new();
private Vector2 _position, _offset;
private float _rotation;
private Vector2 _windowSize; private Vector2 _windowSize;
private bool _vsync; private bool _vsync;
private string _windowTitle = string.Empty; private string _windowTitle = string.Empty;

View File

@@ -79,13 +79,25 @@ namespace DaggerFramework.Rendering
/// <param name="color">Background color.</param> /// <param name="color">Background color.</param>
public abstract void ClearBackground(Color color); public abstract void ClearBackground(Color color);
public void ResetTransform()
{
transformPosition = Vector2.Zero;
transformOffset = Vector2.Zero;
transformRotation = 0.0f;
}
/// <summary> /// <summary>
/// Sets transforms for the next draw operation. /// Sets transforms for the next draw operation.
/// </summary> /// </summary>
/// <param name="position">Global transform position.</param> /// <param name="position">Global transform position.</param>
/// <param name="offset">Local offset point around which shapes will rotate.</param> /// <param name="offset">Local offset point around which shapes will rotate.</param>
/// <param name="rotation">Rotation.</param> /// <param name="rotation">Rotation.</param>
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;
}
/// <summary> /// <summary>
/// Sets the transform for the next draw operation. /// Sets the transform for the next draw operation.
/// </summary> /// </summary>
@@ -127,6 +139,9 @@ namespace DaggerFramework.Rendering
/// <param name="id">Texture to draw.</param> /// <param name="id">Texture to draw.</param>
/// <param name="tint">Texture tint.</param> /// <param name="tint">Texture tint.</param>
public abstract void DrawTexture(Texture2d texture, Color tint); public abstract void DrawTexture(Texture2d texture, Color tint);
protected Vector2 transformPosition, transformOffset;
protected float transformRotation;
} }
public enum Msaa public enum Msaa

View File

@@ -100,12 +100,6 @@ namespace DaggerFramework.Rendering
return; return;
} }
/// <inheritdoc />
public override void SetTransform(Vector2 position, Vector2 offset, float rotation = 0)
{
throw new NotImplementedException();
}
/// <inheritdoc /> /// <inheritdoc />
public override void SetTransform(Matrix4x4 transform) public override void SetTransform(Matrix4x4 transform)
{ {

View File

@@ -6,7 +6,7 @@ namespace DaggerFramework.SceneGraph
public class EntityLayer : Layer public class EntityLayer : Layer
{ {
public List<Entity> Entities { get => _entities; } public List<Entity> Entities { get => _entities; }
public Camera2d CurrentCamera { get; set; } public Camera2d? CurrentCamera { get; set; }
public EntityLayer(List<Entity> entities) public EntityLayer(List<Entity> entities)
{ {
@@ -76,8 +76,7 @@ namespace DaggerFramework.SceneGraph
protected override void OnBeginDraw(Renderer renderer) protected override void OnBeginDraw(Renderer renderer)
{ {
var hasCamera = CurrentCamera != null; if (CurrentCamera is not null)
if (hasCamera)
{ {
renderer.BeginCamera2d(CurrentCamera.Offset, CurrentCamera.Position, 0f, CurrentCamera.Zoom); renderer.BeginCamera2d(CurrentCamera.Offset, CurrentCamera.Position, 0f, CurrentCamera.Zoom);
} }
@@ -85,8 +84,7 @@ namespace DaggerFramework.SceneGraph
protected override void OnEndDraw(Renderer renderer) protected override void OnEndDraw(Renderer renderer)
{ {
var hasCamera = CurrentCamera != null; if (CurrentCamera is not null)
if (hasCamera)
{ {
renderer.EndCamera2d(); renderer.EndCamera2d();
} }

View File

@@ -72,6 +72,8 @@ namespace DaggerFramework.SceneGraph
layer.BeginDraw(_renderer); layer.BeginDraw(_renderer);
layer.Draw(_renderer); layer.Draw(_renderer);
} }
Renderer.ResetTransform();
} }
public void EndDraw() public void EndDraw()

View File

@@ -37,8 +37,8 @@ public class TestGame : Game
public override void Shutdown() public override void Shutdown()
{ {
_renderer.Shutdown(); _renderer!.Shutdown();
_audioBackend.Shutdown(); _audioBackend?.Shutdown();
} }
protected override void LoadResources() protected override void LoadResources()
@@ -61,43 +61,37 @@ public class TestGame : Game
protected override void Ready() 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) }); _inputHandler.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) });
_scene.AddLayer("World", _worldLayer); _scene!.AddLayer("World", _worldLayer!);
_worldLayer.AddEntity(new World());
_worldLayer!.AddEntity(new World());
_worldLayer.AddEntity(new TestPlayer()); _worldLayer.AddEntity(new TestPlayer());
_scene.AddLayer("UI", _uiLayer!);
_scene.Start(); _scene.Start();
} }
protected override void Run() protected override void Run()
{ {
while (_scene.ShouldRun) while (_scene!.ShouldRun)
{ {
_scene.Update(); _scene.Update();
_scene.BeginDraw(); _scene.BeginDraw();
_scene.EndDraw(); _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 ResourceManager _resourceManager = new();
private Sound? _testSound; private Sound? _testSound;
private Font? _font; private Font? _font;
private FmodAudioBackend _audioBackend; private FmodAudioBackend? _audioBackend;
private InputHandler _inputHandler; private InputHandler? _inputHandler;
private Scene _scene; private Scene? _scene;
private UiLayer _uiLayer; private UiLayer? _uiLayer;
private EntityLayer _worldLayer; private EntityLayer? _worldLayer;
} }

View File

@@ -14,8 +14,11 @@ public class TestPlayer : RectangleShape2d
Current = true, Current = true,
}; };
if (Layer is not null)
{
Layer.AddEntity(_camera); Layer.AddEntity(_camera);
} }
}
protected override void OnUpdate(double dt) protected override void OnUpdate(double dt)
{ {
@@ -27,10 +30,13 @@ public class TestPlayer : RectangleShape2d
var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed; var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed;
Position += velocity * (float)dt; Position += velocity * (float)dt;
if (_camera is not null)
{
_camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f); _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f);
} }
}
private Logger _logger = new(nameof(TestPlayer)); private Logger _logger = new(nameof(TestPlayer));
private float _speed = 200f; private float _speed = 200f;
private Camera2d _camera; private Camera2d? _camera;
} }

View File

@@ -18,7 +18,7 @@ public class UiLayer : Layer
protected override void OnUpdate(double dt) protected override void OnUpdate(double dt)
{ {
base.OnUpdate(dt); base.OnUpdate(dt);
// _textLabel.Text = $"{MathF.Round(1 / (float)dt)} FPS"; if (Scene is null) return;
_screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize); _screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize);
} }
@@ -39,7 +39,8 @@ public class UiLayer : Layer
private void CreateUiElements() private void CreateUiElements()
{ {
_screenContainer = new Container(); if (Scene is null) return;
_screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize); _screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize);
var style = new PanelStyle() var style = new PanelStyle()
@@ -56,7 +57,7 @@ public class UiLayer : Layer
var exampleText = new TextLabel() var exampleText = new TextLabel()
{ {
Font = _defaultFont, Font = _defaultFont!,
FontSize = 20, 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?" 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 Font? _defaultFont;
private Container _screenContainer; private Container _screenContainer = new();
private Panel _panel, _contentPanel; private Panel? _panel;
private TextLabel _textLabel;
private Logger _logger = new(nameof(UiLayer)); private Logger _logger = new(nameof(UiLayer));
} }