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

@@ -7,44 +7,64 @@ namespace DaggerFramework.Rendering
{
public class RaylibRenderer : Renderer
{
public override Vector2 WindowSize => new Vector2(Raylib.GetScreenWidth(), Raylib.GetScreenHeight());
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)
{
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
_windowSize = windowSettings.Size;
_windowSize = windowSettings.Size;
ConfigFlags windowFlags = 0;
windowFlags |= windowSettings.Resizable ? ConfigFlags.FLAG_WINDOW_RESIZABLE : 0;
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, windowSettings.Title);
_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.SetWindowState(windowFlags);
}
public override void SetWindowTitle(string title)
protected override void SetWindowTitle(string 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);
}
public override bool WindowShouldClose()
protected override bool WindowShouldClose()
{
return Raylib.WindowShouldClose();
}
@@ -181,6 +201,8 @@ namespace DaggerFramework.Rendering
public override void Initialize(RendererSettings settings)
{
_targetFps = settings.TargetFps;
ConfigFlags flags = 0;
// MSAA
@@ -190,8 +212,10 @@ namespace DaggerFramework.Rendering
flags |= settings.UseVSync ? ConfigFlags.FLAG_VSYNC_HINT : 0;
// 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);
}
@@ -200,8 +224,8 @@ namespace DaggerFramework.Rendering
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
{
CreateWindow(windowSettings);
Initialize(renderSettings);
CreateWindow(windowSettings);
}
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));
}
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<Raylib_cs.Font> _fontPool = new();
private Vector2 _position, _offset;
private float _rotation;
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.
/// </summary>
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
/// <summary>
/// The size of the render window.
/// </summary>
public abstract Vector2 WindowSize { get; }
public abstract string WindowTitle { get; set; }
/// <summary>
/// Active monitor's size.
/// </summary>
public abstract Vector2 MonitorSize { get; }
/// <summary>
/// Creates a window.
/// </summary>
/// <param name="windowSettings">Window settings to use to create the window.</param>
public abstract void CreateWindow(WindowSettings windowSettings);
// TODO: use properties for these.
public abstract void SetWindowTitle(string title);
public abstract void SetWindowVSync(bool value);
public abstract void SetTargetFps(int fps);
public abstract bool WindowShouldClose();
protected abstract void SetWindowTitle(string title);
protected abstract void SetWindowVSync(bool value);
protected abstract void SetTargetFps(int fps);
protected abstract int GetMonitorWidth(int monitorId);
protected abstract int GetMonitorHeight(int monitorId);
protected abstract int GetCurrentMonitor();
protected abstract bool WindowShouldClose();
public abstract void Shutdown();
// DRAWING
@@ -135,10 +149,14 @@ namespace DaggerFramework.Rendering
public Msaa Msaa;
public bool UseVSync;
public bool Fullscreen;
public int TargetFps = 60;
public RendererSettings() { }
public static RendererSettings Default => new RendererSettings()
{
UseVSync = true,
TargetFps = 60
};
}

View File

@@ -16,6 +16,14 @@ namespace DaggerFramework.Rendering
/// <inheritdoc />
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 />
public override void Initialize(RendererSettings settings)
{
@@ -93,7 +101,7 @@ namespace DaggerFramework.Rendering
}
/// <inheritdoc />
public override void SetTargetFps(int fps)
protected override void SetTargetFps(int fps)
{
return;
}
@@ -111,19 +119,19 @@ namespace DaggerFramework.Rendering
}
/// <inheritdoc />
public override void SetWindowTitle(string title)
protected override void SetWindowTitle(string title)
{
SetWindowTitleUnsafe(title);
}
/// <inheritdoc />
public override void SetWindowVSync(bool value)
protected override void SetWindowVSync(bool value)
{
throw new NotImplementedException();
}
/// <inheritdoc />
public override bool WindowShouldClose() => WindowShouldCloseUnsafe();
protected override bool WindowShouldClose() => WindowShouldCloseUnsafe();
private unsafe void CreateWindowUnsafe(string title, Vector2 size)
{
@@ -178,6 +186,21 @@ namespace DaggerFramework.Rendering
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 Glfw _glfw;
private unsafe WindowHandle* _windowHandle;