API renames, change ref renderer to in renderer in IDrawable, make Layer implement IDrawable.

This commit is contained in:
2023-06-15 22:39:34 +02:00
parent bf4fb6e1e3
commit 964b903500
18 changed files with 122 additions and 67 deletions

14
DaggerFramework.sln Normal file
View File

@@ -0,0 +1,14 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -9,7 +9,7 @@ namespace DaggerFramework
public float Radius { get => _radius; set => _radius = value; } public float Radius { get => _radius; set => _radius = value; }
public Color Color { get => _color; set => _color = value; } public Color Color { get => _color; set => _color = value; }
public override void OnDraw(ref Renderer renderer) public override void OnDraw(in Renderer renderer)
{ {
renderer.DrawCircle(_radius, _color); renderer.DrawCircle(_radius, _color);
} }

View File

@@ -4,12 +4,12 @@ namespace DaggerFramework
{ {
public abstract class Drawable2d : Entity2d, IDrawable public abstract class Drawable2d : Entity2d, IDrawable
{ {
public void Draw(ref Renderer renderer) public void Draw(in Renderer renderer)
{ {
renderer.SetTransform(position); renderer.SetTransform(position);
OnDraw(ref renderer); OnDraw(in renderer);
} }
public abstract void OnDraw(ref Renderer renderer); public abstract void OnDraw(in Renderer renderer);
} }
} }

View File

@@ -4,6 +4,6 @@ namespace DaggerFramework
{ {
public interface IDrawable public interface IDrawable
{ {
public void Draw(ref Renderer renderer); public void Draw(in Renderer renderer);
} }
} }

View File

@@ -21,7 +21,7 @@ namespace DaggerFramework
CleanupParticles(); CleanupParticles();
InitializeParticles(); InitializeParticles();
} }
public override void OnDraw(ref Renderer renderer) public override void OnDraw(in Renderer renderer)
{ {
foreach (var particle in _particles) foreach (var particle in _particles)
{ {

View File

@@ -14,7 +14,7 @@ namespace DaggerFramework
_texId = renderer.LoadTexture(_texture); _texId = renderer.LoadTexture(_texture);
} }
public override void OnDraw(ref Renderer renderer) public override void OnDraw(in Renderer renderer)
{ {
renderer.DrawTexture(_texId, Color.White); renderer.DrawTexture(_texId, Color.White);
} }

View File

@@ -9,7 +9,7 @@ namespace DaggerFramework
public int FontSize { get => _fontSize; set => _fontSize = value; } public int FontSize { get => _fontSize; set => _fontSize = value; }
public Color FontColor { get => _fontColor; set => _fontColor = value; } public Color FontColor { get => _fontColor; set => _fontColor = value; }
public override void OnDraw(ref Renderer renderer) public override void OnDraw(in Renderer renderer)
{ {
renderer.DrawDebugText(position, _contents, _fontSize, _fontColor); renderer.DrawDebugText(position, _contents, _fontSize, _fontColor);
} }

View File

@@ -45,11 +45,11 @@ namespace DaggerFramework
} }
} }
protected override void OnDraw(ref Renderer renderer) protected override void OnDraw(in Renderer renderer)
{ {
foreach (IDrawable drawable in _entities) foreach (IDrawable drawable in _entities)
{ {
drawable.Draw(ref renderer); drawable.Draw(in renderer);
} }
} }

View File

@@ -7,12 +7,11 @@ namespace DaggerFramework
{ {
LoadResources(); LoadResources();
OnStart(); OnStart();
MainLoop();
} }
protected abstract void OnStart(); protected abstract void OnStart();
protected abstract void LoadResources(); protected abstract void LoadResources();
protected abstract void MainLoop(); protected abstract void MainLoop();
public abstract void Shutdown(); public abstract void Shutdown();
protected Scene scene;
} }
} }

View File

@@ -8,10 +8,10 @@ namespace DaggerFramework
{ {
public class ImGuiRenderLayer : Layer public class ImGuiRenderLayer : Layer
{ {
protected override void OnDraw(ref Renderer renderer) protected override void OnDraw(in Renderer renderer)
{ {
Layout(); Layout();
_controller.Draw(ref renderer); _controller.Draw(in renderer);
} }
protected virtual void Layout() { } protected virtual void Layout() { }
@@ -307,7 +307,7 @@ namespace DaggerFramework
Rlgl.rlEnd(); Rlgl.rlEnd();
} }
public void Draw(ref Renderer renderer) public void Draw(in Renderer renderer)
{ {
ImGui.Render(); ImGui.Render();
RenderCommandLists(ImGui.GetDrawData()); RenderCommandLists(ImGui.GetDrawData());

View File

@@ -3,12 +3,12 @@ using DaggerFramework.Rendering;
namespace DaggerFramework namespace DaggerFramework
{ {
public abstract class Layer public abstract class Layer : IDrawable
{ {
public Scene Scene { get; set; } public Scene Scene { get; set; }
public InputHandler Input { get; set; } public InputHandler Input { get; set; }
public void Draw(ref Renderer renderer) => OnDraw(ref renderer); public void Draw(in Renderer renderer) => OnDraw(in renderer);
public void Start() => OnStart(); public void Start() => OnStart();
public void Update(double dt) => OnUpdate(dt); public void Update(double dt) => OnUpdate(dt);
@@ -17,6 +17,6 @@ namespace DaggerFramework
protected virtual void OnStart() { } protected virtual void OnStart() { }
protected virtual void OnUpdate(double dt) { } protected virtual void OnUpdate(double dt) { }
protected virtual void OnInput(InputHandler input) { } protected virtual void OnInput(InputHandler input) { }
protected abstract void OnDraw(ref Renderer renderer); protected abstract void OnDraw(in Renderer renderer);
} }
} }

View File

@@ -9,12 +9,19 @@ namespace DaggerFramework.Rendering
public class GlRenderer : Renderer public class GlRenderer : Renderer
{ {
public override Vector2 WindowSize => throw new NotImplementedException(); public override Vector2 WindowSize => throw new NotImplementedException();
public override bool ShouldRun => throw new NotImplementedException();
public override void Initialize(RendererSettings settings) public override void Initialize(RendererSettings settings)
{ {
} }
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
{
CreateWindow(windowSettings);
Initialize(renderSettings);
}
public override void BeginFrame() public override void BeginFrame()
{ {
_glfw.PollEvents(); _glfw.PollEvents();
@@ -34,7 +41,7 @@ namespace DaggerFramework.Rendering
} }
public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size); public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size);
public override void CloseWindow() public override void Shutdown()
{ {
CloseWindowUnsafe(); CloseWindowUnsafe();
_glfw.Terminate(); _glfw.Terminate();

View File

@@ -7,16 +7,7 @@ namespace DaggerFramework.Rendering
public class RaylibRenderer : Renderer public class RaylibRenderer : Renderer
{ {
public override Vector2 WindowSize => _windowSize; public override Vector2 WindowSize => _windowSize;
public override void DrawCircle(float radius, System.Drawing.Color color) public override bool ShouldRun => !WindowShouldClose();
{
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, SystemColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, float rotation)
{
_position = position;
_rotation = rotation;
}
public override void CreateWindow(string title, Vector2 size) public override void CreateWindow(string title, Vector2 size)
{ {
@@ -45,7 +36,7 @@ namespace DaggerFramework.Rendering
return Raylib.WindowShouldClose(); return Raylib.WindowShouldClose();
} }
public override void CloseWindow() public override void Shutdown()
{ {
Raylib.CloseWindow(); Raylib.CloseWindow();
} }
@@ -70,9 +61,15 @@ namespace DaggerFramework.Rendering
return (double)Raylib.GetFrameTime(); return (double)Raylib.GetFrameTime();
} }
private Raylib_cs.Color SystemColorToRaylibColor(System.Drawing.Color color) public override void DrawCircle(float radius, System.Drawing.Color color)
{ {
return new Color { r = color.R, g = color.G, b = color.B, a = color.A }; Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, SystemColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, float rotation)
{
_position = position;
_rotation = rotation;
} }
public override int LoadTexture(Texture2d texture) public override int LoadTexture(Texture2d texture)
@@ -125,7 +122,7 @@ namespace DaggerFramework.Rendering
public override void DrawSdfText(Vector2 position, string text, int fontSize, System.Drawing.Color color) public override void DrawSdfText(Vector2 position, string text, int fontSize, System.Drawing.Color color)
{ {
throw new NotImplementedException();
} }
public override void Initialize(RendererSettings settings) public override void Initialize(RendererSettings settings)
@@ -146,6 +143,17 @@ namespace DaggerFramework.Rendering
// TODO // TODO
public override void SetTransform(Matrix4x4 transform) { } public override void SetTransform(Matrix4x4 transform) { }
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
{
CreateWindow(windowSettings);
Initialize(renderSettings);
}
private Raylib_cs.Color SystemColorToRaylibColor(System.Drawing.Color color)
{
return new Color { r = color.R, g = color.G, b = color.B, a = color.A };
}
private List<Texture2D> _texturePool = new List<Texture2D>(); private List<Texture2D> _texturePool = new List<Texture2D>();
private Vector2 _position; private Vector2 _position;
private float _rotation; private float _rotation;

View File

@@ -6,15 +6,24 @@ namespace DaggerFramework.Rendering
public abstract class Renderer public abstract class Renderer
{ {
// INIT // INIT
public abstract void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings);
public abstract void Initialize(RendererSettings settings); public abstract void Initialize(RendererSettings settings);
// UTIL
public abstract bool ShouldRun { get; }
// WINDOW // WINDOW
public abstract Vector2 WindowSize { get; } public abstract Vector2 WindowSize { get; }
public abstract void CreateWindow(string title, Vector2 size); public abstract void CreateWindow(string title, Vector2 size);
public void CreateWindow(WindowSettings windowSettings)
{
CreateWindow(windowSettings.Title, windowSettings.Size);
}
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);
public abstract bool WindowShouldClose(); public abstract bool WindowShouldClose();
public abstract void CloseWindow(); public abstract void Shutdown();
// DRAWING // DRAWING
public abstract void BeginFrame(); public abstract void BeginFrame();
@@ -43,5 +52,22 @@ namespace DaggerFramework.Rendering
public Msaa Msaa; public Msaa Msaa;
public bool UseVSync; public bool UseVSync;
public bool Fullscreen; public bool Fullscreen;
public static RendererSettings Default => new RendererSettings()
{
UseVSync = true,
};
}
public struct WindowSettings
{
public string Title;
public Vector2 Size = new Vector2(640, 480);
public WindowSettings(string title, Vector2 size)
{
Title = title;
Size = size;
}
} }
} }

View File

@@ -60,7 +60,7 @@ namespace DaggerFramework
Renderer.ClearBackground(Color.Black); Renderer.ClearBackground(Color.Black);
foreach (var layer in _layers.Values) foreach (var layer in _layers.Values)
{ {
layer.Draw(ref _renderer); layer.Draw(in _renderer);
} }
Renderer.EndFrame(); Renderer.EndFrame();

View File

@@ -12,7 +12,7 @@ namespace DaggerFramework
base.OnUpdate(dt); base.OnUpdate(dt);
_time += dt; _time += dt;
} }
protected override void OnDraw(ref Renderer renderer) protected override void OnDraw(in Renderer renderer)
{ {
renderer.SetTransform(Vector2.Zero); renderer.SetTransform(Vector2.Zero);
renderer.DrawRectangle(new Vector2(720 / 2, 1280), Color.Green); renderer.DrawRectangle(new Vector2(720 / 2, 1280), Color.Green);

13
TestGame/Circle2d.cs Normal file
View File

@@ -0,0 +1,13 @@
using DaggerFramework.Rendering;
using System.Drawing;
namespace DaggerFramework;
public class Circle2d : Drawable2d
{
public override void OnDraw(in Renderer renderer)
{
renderer.DrawCircle(32f, Color.AliceBlue);
}
}

View File

@@ -1,52 +1,40 @@
using DaggerFramework; using DaggerFramework;
using DaggerFramework.Audio; using System.Numerics;
using DaggerFramework.Rendering; using DaggerFramework.Rendering;
public class TestGame : Game public class TestGame : Game
{ {
public override string ResourceRoot => "Resources/"; public override string ResourceRoot => "Resources/";
public override void Shutdown() => scene.Renderer.CloseWindow();
protected override void OnStart() protected override void OnStart()
{ {
_renderer = new RaylibRenderer(); _renderer = new RaylibRenderer();
_inputHandler = new RaylibInputHandler();
scene = new Scene(_renderer, _inputHandler, new DummyAudioBackend()); _renderer.CreateAndInitialize(new WindowSettings()
{
var mainGameLayer = new EntityLayer(); Title = "Test Game",
Size = new Vector2(1280, 720)
scene.AddLayer("World", mainGameLayer); }, RendererSettings.Default);
var text = new Text2d();
text.Contents = "Hello World!";
text.FontSize = 32;
var sprite = new Sprite2d();
sprite.Texture = _funnyTexture;
mainGameLayer.AddEntity(text);
mainGameLayer.AddEntity(sprite);
scene.Init();
scene.Start();
MainLoop();
} }
protected override void LoadResources() protected override void LoadResources()
{ {
_funnyTexture = _textureLoader.Load($"{ResourceRoot}icon.png");
} }
protected override void MainLoop() protected override void MainLoop()
{ {
while (!scene.ShouldStop()) scene.Update(); while (_renderer.ShouldRun)
{
_renderer.BeginFrame();
_renderer.ClearBackground(System.Drawing.Color.Black);
_renderer.EndFrame();
}
}
public override void Shutdown()
{
_renderer.Shutdown();
} }
private Renderer _renderer; private Renderer _renderer;
private RaylibInputHandler _inputHandler;
private Texture2dLoader _textureLoader = new();
private Texture2d? _funnyTexture;
} }