From fdbd21f2481a02267272d504c69a5c2ac616594f Mon Sep 17 00:00:00 2001 From: dnesov Date: Tue, 28 Feb 2023 22:26:47 +0100 Subject: [PATCH] Create OpenGL context and clear background :) --- Source/Rendering/GlRenderer.cs | 75 +++++++++++++++++++++++----------- Source/Scene.cs | 2 +- TestGame/Program.cs | 8 ++++ TestGame/TestGame.cs | 35 ++++++++++++++++ TestGame/TestGame.csproj | 15 +++++++ 5 files changed, 111 insertions(+), 24 deletions(-) create mode 100644 TestGame/Program.cs create mode 100644 TestGame/TestGame.cs create mode 100644 TestGame/TestGame.csproj diff --git a/Source/Rendering/GlRenderer.cs b/Source/Rendering/GlRenderer.cs index 4e3a0c5..54a8734 100644 --- a/Source/Rendering/GlRenderer.cs +++ b/Source/Rendering/GlRenderer.cs @@ -1,30 +1,44 @@ using System.Drawing; using System.Numerics; +using Silk.NET.GLFW; +using Silk.NET.Maths; +using Silk.NET.OpenGL; + namespace DaggerFramework.Rendering { public class GlRenderer : Renderer { public override Vector2 WindowSize => throw new NotImplementedException(); + public override void Initialize(RendererSettings settings) + { + + } + public override void BeginFrame() { - throw new NotImplementedException(); + _glfw.PollEvents(); + _gl.Viewport(new Size((int)_windowSize.X, (int)_windowSize.Y)); + } + + public override void EndFrame() + { + // throw new NotImplementedException(); + EndFrameUnsafe(); } public override void ClearBackground(Color color) { - throw new NotImplementedException(); - } - - public override void CreateWindow(string title, Vector2 size) - { - throw new NotImplementedException(); + _gl.ClearColor(color); + _gl.Clear((uint)ClearBufferMask.ColorBufferBit); } + public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size); public override void CloseWindow() { - throw new NotImplementedException(); + CloseWindowUnsafe(); + _glfw.Terminate(); } public override void DrawCircle(float radius, Color color) @@ -47,19 +61,9 @@ namespace DaggerFramework.Rendering throw new NotImplementedException(); } - public override void EndFrame() - { - throw new NotImplementedException(); - } - public override double GetFrameTime() { - throw new NotImplementedException(); - } - - public override void Initialize(RendererSettings settings) - { - throw new NotImplementedException(); + return 0.0; } public override int LoadTexture(Texture2d texture) @@ -74,7 +78,7 @@ namespace DaggerFramework.Rendering public override void SetTargetFps(int fps) { - throw new NotImplementedException(); + return; } public override void SetTransform(Vector2 position, float rotation = 0) @@ -89,7 +93,7 @@ namespace DaggerFramework.Rendering public override void SetWindowTitle(string title) { - throw new NotImplementedException(); + SetWindowTitleUnsafe(title); } public override void SetWindowVSync(bool value) @@ -97,9 +101,34 @@ namespace DaggerFramework.Rendering throw new NotImplementedException(); } - public override bool WindowShouldClose() + public override bool WindowShouldClose() => WindowShouldCloseUnsafe(); + + private unsafe void CreateWindowUnsafe(string title, Vector2 size) { - throw new NotImplementedException(); + _glfw = GlfwProvider.GLFW.Value; + _glfw.Init(); + + _glfw.WindowHint(WindowHintInt.ContextVersionMajor, 4); + _glfw.WindowHint(WindowHintInt.ContextVersionMinor, 6); + _windowHandle = _glfw.CreateWindow((int)size.X, (int)size.Y, title, null, null); + + _glfw.MakeContextCurrent(_windowHandle); + _gl = GL.GetApi(_glfw.GetProcAddress); + _glfw.SwapInterval(1); + + _windowSize = size; } + private unsafe void CloseWindowUnsafe() => _glfw.DestroyWindow(_windowHandle); + private unsafe bool WindowShouldCloseUnsafe() => _glfw.WindowShouldClose(_windowHandle); + private unsafe void SetWindowTitleUnsafe(string title) => _glfw.SetWindowTitle(_windowHandle, title); + private unsafe void EndFrameUnsafe() + { + _glfw.SwapBuffers(_windowHandle); + } + + private GL _gl; + private Glfw _glfw; + private unsafe WindowHandle* _windowHandle; + private Vector2 _windowSize; } } \ No newline at end of file diff --git a/Source/Scene.cs b/Source/Scene.cs index 7073ea5..591ff34 100755 --- a/Source/Scene.cs +++ b/Source/Scene.cs @@ -69,7 +69,7 @@ namespace DaggerFramework private void SetupRenderer() { - Renderer.CreateWindow("RogueMine", new Vector2(1280, 720)); + Renderer.CreateWindow("Game", new Vector2(1280, 720)); Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true }); Renderer.SetTargetFps(60); } diff --git a/TestGame/Program.cs b/TestGame/Program.cs new file mode 100644 index 0000000..524cd2a --- /dev/null +++ b/TestGame/Program.cs @@ -0,0 +1,8 @@ +class Program +{ + static void Main(string[] args) + { + var game = new TestGame(); + game.Start(); + } +} \ No newline at end of file diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs new file mode 100644 index 0000000..8e46f37 --- /dev/null +++ b/TestGame/TestGame.cs @@ -0,0 +1,35 @@ +using DaggerFramework; +using DaggerFramework.Audio; +using DaggerFramework.Rendering; + +public class TestGame : Game +{ + public override void Shutdown() => scene.Renderer.CloseWindow(); + protected override void OnStart() + { + _renderer = new GlRenderer(); + _inputHandler = new RaylibInputHandler(); + + scene = new Scene(_renderer, _inputHandler, new DummyAudioBackend()); + + var mainGameLayer = new EntityLayer(); + scene.AddLayer("World", mainGameLayer); + + scene.Init(); + scene.Start(); + + MainLoop(); + } + + protected override void LoadResources() + { + + } + + protected override void MainLoop() + { + while (!scene.ShouldStop()) scene.Update(); + } + private GlRenderer _renderer; + private RaylibInputHandler _inputHandler; +} \ No newline at end of file diff --git a/TestGame/TestGame.csproj b/TestGame/TestGame.csproj new file mode 100644 index 0000000..86342ac --- /dev/null +++ b/TestGame/TestGame.csproj @@ -0,0 +1,15 @@ + + + + Exe + net7.0 + enable + enable + false + false + + + + + + \ No newline at end of file