Use properties in Renderer.

This commit is contained in:
2024-01-20 19:25:05 +01:00
parent 0299a0724e
commit 2c42856fc2
8 changed files with 123 additions and 41 deletions

View File

@@ -97,7 +97,7 @@ namespace DaggerFramework
return false; return false;
} }
public override bool IsKeyboardKeyDown(DaggerFramework.KeyboardKey key) public override bool IsKeyboardKeyDown(KeyboardKey key)
{ {
Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key; Raylib_cs.KeyboardKey rayKey = (Raylib_cs.KeyboardKey)key;
OnInput?.Invoke(); OnInput?.Invoke();

View File

@@ -7,44 +7,64 @@ namespace DaggerFramework.Rendering
{ {
public class RaylibRenderer : Renderer public class RaylibRenderer : Renderer
{ {
public override Vector2 WindowSize => new Vector2(Raylib.GetScreenWidth(), Raylib.GetScreenHeight());
public override bool ShouldRun => !WindowShouldClose(); public override bool ShouldRun => !WindowShouldClose();
public override Vector2 WindowSize => new Vector2(Raylib.GetScreenWidth(), Raylib.GetScreenHeight());
public override Vector2 MonitorSize => new Vector2(GetMonitorWidth(GetCurrentMonitor()), GetMonitorHeight(GetCurrentMonitor()));
public override string WindowTitle
{
get => _windowTitle; set
{
SetWindowTitle(value);
}
}
public override int TargetFps { get => _targetFps; set => SetTargetFps(value); }
public override bool VSync { get => _vsync; set => SetWindowVSync(value); }
// public override void CreateWindow(string title, Vector2 size)
// {
// Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
// _windowSize = size;
// Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
// }
public override void CreateWindow(WindowSettings windowSettings) public override void CreateWindow(WindowSettings windowSettings)
{ {
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE); Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
_windowSize = windowSettings.Size;
_windowSize = windowSettings.Size;
ConfigFlags windowFlags = 0; ConfigFlags windowFlags = 0;
windowFlags |= windowSettings.Resizable ? ConfigFlags.FLAG_WINDOW_RESIZABLE : 0; windowFlags |= windowSettings.Resizable ? ConfigFlags.FLAG_WINDOW_RESIZABLE : 0;
_windowTitle = windowSettings.Title;
if (_fullscreen)
{
var monitorSize = MonitorSize;
Raylib.InitWindow((int)monitorSize.X, (int)monitorSize.Y, windowSettings.Title);
}
else
{
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, windowSettings.Title); Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, windowSettings.Title);
}
Raylib.SetWindowState(windowFlags); Raylib.SetWindowState(windowFlags);
} }
public override void SetWindowTitle(string title) protected override void SetWindowTitle(string title)
{ {
Raylib.SetWindowTitle(title); Raylib.SetWindowTitle(title);
} }
public override void SetWindowVSync(bool value) protected override void SetWindowVSync(bool value)
{ {
_vsync = value;
// TODO: implement VSync toggle for Raylib.
} }
public override void SetTargetFps(int fps) protected override void SetTargetFps(int fps)
{ {
_targetFps = fps;
Raylib.SetTargetFPS(fps); Raylib.SetTargetFPS(fps);
} }
public override bool WindowShouldClose() protected override bool WindowShouldClose()
{ {
return Raylib.WindowShouldClose(); return Raylib.WindowShouldClose();
} }
@@ -181,6 +201,8 @@ namespace DaggerFramework.Rendering
public override void Initialize(RendererSettings settings) public override void Initialize(RendererSettings settings)
{ {
_targetFps = settings.TargetFps;
ConfigFlags flags = 0; ConfigFlags flags = 0;
// MSAA // MSAA
@@ -190,8 +212,10 @@ namespace DaggerFramework.Rendering
flags |= settings.UseVSync ? ConfigFlags.FLAG_VSYNC_HINT : 0; flags |= settings.UseVSync ? ConfigFlags.FLAG_VSYNC_HINT : 0;
// Fullscreen // Fullscreen
flags |= settings.Fullscreen ? ConfigFlags.FLAG_FULLSCREEN_MODE : 0; flags |= settings.Fullscreen ? ConfigFlags.FLAG_WINDOW_MAXIMIZED : 0;
flags |= settings.Fullscreen ? ConfigFlags.FLAG_WINDOW_UNDECORATED : 0;
_fullscreen = settings.Fullscreen;
Raylib.SetConfigFlags(flags); Raylib.SetConfigFlags(flags);
} }
@@ -200,8 +224,8 @@ namespace DaggerFramework.Rendering
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings) public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
{ {
CreateWindow(windowSettings);
Initialize(renderSettings); Initialize(renderSettings);
CreateWindow(windowSettings);
} }
private Raylib_cs.Color DaggerColorToRaylibColor(Color color) private Raylib_cs.Color DaggerColorToRaylibColor(Color color)
@@ -215,10 +239,30 @@ namespace DaggerFramework.Rendering
Raylib.DrawTextPro(font, text, _position, Vector2.Zero, _rotation, fontSize, 0.0f, DaggerColorToRaylibColor(color)); Raylib.DrawTextPro(font, text, _position, Vector2.Zero, _rotation, fontSize, 0.0f, DaggerColorToRaylibColor(color));
} }
protected override int GetMonitorWidth(int monitorId)
{
return Raylib.GetMonitorWidth(monitorId);
}
protected override int GetMonitorHeight(int monitorId)
{
return Raylib.GetMonitorHeight(monitorId);
}
protected override int GetCurrentMonitor()
{
return Raylib.GetCurrentMonitor();
}
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 Vector2 _position, _offset;
private float _rotation; private float _rotation;
private Vector2 _windowSize; private Vector2 _windowSize;
private bool _vsync;
private string _windowTitle = string.Empty;
private int _targetFps;
private bool _fullscreen;
} }
} }

View File

@@ -25,23 +25,37 @@ namespace DaggerFramework.Rendering
/// Indicates if the renderer will render the next frame. /// Indicates if the renderer will render the next frame.
/// </summary> /// </summary>
public abstract bool ShouldRun { get; } public abstract bool ShouldRun { get; }
/// <summary>
/// Target frames per second for rendering.
/// </summary>
public abstract int TargetFps { get; set; }
public abstract bool VSync { get; set; }
// WINDOW // WINDOW
/// <summary> /// <summary>
/// The size of the render window. /// The size of the render window.
/// </summary> /// </summary>
public abstract Vector2 WindowSize { get; } public abstract Vector2 WindowSize { get; }
public abstract string WindowTitle { get; set; }
/// <summary>
/// Active monitor's size.
/// </summary>
public abstract Vector2 MonitorSize { get; }
/// <summary> /// <summary>
/// Creates a window. /// Creates a window.
/// </summary> /// </summary>
/// <param name="windowSettings">Window settings to use to create the window.</param> /// <param name="windowSettings">Window settings to use to create the window.</param>
public abstract void CreateWindow(WindowSettings windowSettings); public abstract void CreateWindow(WindowSettings windowSettings);
protected abstract void SetWindowTitle(string title);
// TODO: use properties for these. protected abstract void SetWindowVSync(bool value);
public abstract void SetWindowTitle(string title); protected abstract void SetTargetFps(int fps);
public abstract void SetWindowVSync(bool value); protected abstract int GetMonitorWidth(int monitorId);
public abstract void SetTargetFps(int fps); protected abstract int GetMonitorHeight(int monitorId);
public abstract bool WindowShouldClose(); protected abstract int GetCurrentMonitor();
protected abstract bool WindowShouldClose();
public abstract void Shutdown(); public abstract void Shutdown();
// DRAWING // DRAWING
@@ -135,10 +149,14 @@ namespace DaggerFramework.Rendering
public Msaa Msaa; public Msaa Msaa;
public bool UseVSync; public bool UseVSync;
public bool Fullscreen; public bool Fullscreen;
public int TargetFps = 60;
public RendererSettings() { }
public static RendererSettings Default => new RendererSettings() public static RendererSettings Default => new RendererSettings()
{ {
UseVSync = true, UseVSync = true,
TargetFps = 60
}; };
} }

View File

@@ -16,6 +16,14 @@ namespace DaggerFramework.Rendering
/// <inheritdoc /> /// <inheritdoc />
public override bool ShouldRun => throw new NotImplementedException(); public override bool ShouldRun => throw new NotImplementedException();
public override Vector2 MonitorSize => throw new NotImplementedException();
public override int TargetFps { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override bool VSync { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override string WindowTitle { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
/// <inheritdoc /> /// <inheritdoc />
public override void Initialize(RendererSettings settings) public override void Initialize(RendererSettings settings)
{ {
@@ -93,7 +101,7 @@ namespace DaggerFramework.Rendering
} }
/// <inheritdoc /> /// <inheritdoc />
public override void SetTargetFps(int fps) protected override void SetTargetFps(int fps)
{ {
return; return;
} }
@@ -111,19 +119,19 @@ namespace DaggerFramework.Rendering
} }
/// <inheritdoc /> /// <inheritdoc />
public override void SetWindowTitle(string title) protected override void SetWindowTitle(string title)
{ {
SetWindowTitleUnsafe(title); SetWindowTitleUnsafe(title);
} }
/// <inheritdoc /> /// <inheritdoc />
public override void SetWindowVSync(bool value) protected override void SetWindowVSync(bool value)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <inheritdoc /> /// <inheritdoc />
public override bool WindowShouldClose() => WindowShouldCloseUnsafe(); protected override bool WindowShouldClose() => WindowShouldCloseUnsafe();
private unsafe void CreateWindowUnsafe(string title, Vector2 size) private unsafe void CreateWindowUnsafe(string title, Vector2 size)
{ {
@@ -178,6 +186,21 @@ namespace DaggerFramework.Rendering
throw new NotImplementedException(); throw new NotImplementedException();
} }
protected override int GetMonitorWidth(int monitorId)
{
throw new NotImplementedException();
}
protected override int GetMonitorHeight(int monitorId)
{
throw new NotImplementedException();
}
protected override int GetCurrentMonitor()
{
throw new NotImplementedException();
}
private GL _gl; private GL _gl;
private Glfw _glfw; private Glfw _glfw;
private unsafe WindowHandle* _windowHandle; private unsafe WindowHandle* _windowHandle;

View File

@@ -2,10 +2,10 @@ namespace DaggerFramework.SceneGraph
{ {
public interface IMainLoop public interface IMainLoop
{ {
public void Init(); void Init();
public void Start(); void Start();
public bool ShouldStop(); void Update();
public void Update(); double DeltaTime { get; }
public double DeltaTime { get; } bool ShouldRun { get; }
} }
} }

View File

@@ -14,6 +14,7 @@ namespace DaggerFramework.SceneGraph
public ResourceManager ResourceManager => _resourceManager; public ResourceManager ResourceManager => _resourceManager;
public double DeltaTime => Renderer.GetFrameTime(); public double DeltaTime => Renderer.GetFrameTime();
public bool ShouldRun => Renderer.ShouldRun;
public Scene(Renderer renderer, InputHandler input, AudioBackend audioBackend, ResourceManager resourceManager) public Scene(Renderer renderer, InputHandler input, AudioBackend audioBackend, ResourceManager resourceManager)
{ {
@@ -56,8 +57,6 @@ namespace DaggerFramework.SceneGraph
_layers.Add(name, layer); _layers.Add(name, layer);
} }
public bool ShouldStop() => Renderer.WindowShouldClose();
private void Draw() private void Draw()
{ {
Renderer.BeginFrame(); Renderer.BeginFrame();
@@ -77,9 +76,7 @@ namespace DaggerFramework.SceneGraph
private void SetupRenderer() private void SetupRenderer()
{ {
// Renderer.CreateWindow("Game", new Vector2(1280, 720));
Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true }); Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true });
Renderer.SetTargetFps(60);
} }
private Dictionary<string, Layer> _layers; private Dictionary<string, Layer> _layers;

View File

@@ -21,10 +21,10 @@ public class TestGame : Game
{ {
Title = "Test Game", Title = "Test Game",
Size = new Vector2(1280, 720), Size = new Vector2(1280, 720),
Resizable = true,
}, new RendererSettings() }, new RendererSettings()
{ {
UseVSync = true UseVSync = true,
Fullscreen = false
}); });
_audioBackend.Initialize(); _audioBackend.Initialize();
@@ -62,9 +62,9 @@ public class TestGame : Game
_fontHandle = _renderer.LoadFont(_font); _fontHandle = _renderer.LoadFont(_font);
_scene.AddLayer("World", _worldLayer); _scene.AddLayer("World", _worldLayer);
_scene.AddLayer("UI", _uiLayer); // _scene.AddLayer("UI", _uiLayer);
_worldLayer.AddEntity(new World()); // _worldLayer.AddEntity(new World());
_worldLayer.AddEntity(new TestPlayer()); _worldLayer.AddEntity(new TestPlayer());
_scene.Start(); _scene.Start();
@@ -72,8 +72,9 @@ public class TestGame : Game
protected override void Run() protected override void Run()
{ {
while (!_scene.ShouldStop()) while (_scene.ShouldRun)
{ {
// _renderer.SetWindowTitle($"Test Game ({_renderer.GetFrameTime()} ms)");
_scene.Update(); _scene.Update();
} }
} }

View File

@@ -18,5 +18,4 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>
</Project> </Project>