Create OpenGL context and clear background :)

This commit is contained in:
2023-02-28 22:26:47 +01:00
parent d32390b21c
commit fdbd21f248
5 changed files with 111 additions and 24 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}

8
TestGame/Program.cs Normal file
View File

@@ -0,0 +1,8 @@
class Program
{
static void Main(string[] args)
{
var game = new TestGame();
game.Start();
}
}

35
TestGame/TestGame.cs Normal file
View File

@@ -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;
}

15
TestGame/TestGame.csproj Normal file
View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../DaggerFramework.csproj" />
</ItemGroup>
</Project>