This commit is contained in:
2024-12-04 22:35:04 +03:00
commit 3f1740f41f
43 changed files with 1757 additions and 0 deletions

79
Engine/Window.cs Normal file
View File

@@ -0,0 +1,79 @@
using Engine.Renderer;
using Engine.Renderer.Pixel;
using Engine.Renderer.Texture;
using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
namespace Engine;
public class Window : IPresenter<Rgb8>
{
public bool IsExiting => _window.IsExiting;
public int Width => _window.ClientSize.X;
public int Height => _window.ClientSize.Y;
private readonly Engine _engine;
private readonly NativeWindow _window;
private readonly bool _headless;
public Window(Engine engine, NativeWindow window, bool headless)
{
_engine = engine;
_window = window;
_headless = headless;
_window.MakeCurrent();
_window.Resize += args => GL.Viewport(0, 0, args.Width, args.Height);
}
public void Update()
{
if (!_headless)
{
NativeWindow.ProcessWindowEvents(false);
_window.SwapBuffers();
}
}
public void Present(IConstTexture<Rgb8> texture)
{
if (_headless)
return;
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
_engine.Renderer.TextureInternal.Bind();
GL.Enable(EnableCap.Texture2D);
GL.Begin(PrimitiveType.Quads);
GL.Color3(1, 1, 1);
GL.TexCoord2(0, 0);
GL.Vertex2(0, 0);
GL.TexCoord2(1, 0);
GL.Vertex2(1, 0);
GL.TexCoord2(1, 1);
GL.Vertex2(1, 1);
GL.TexCoord2(0, 1);
GL.Vertex2(0, 1);
GL.End();
GL.Disable(EnableCap.Texture2D);
GL.Flush();
_engine.Renderer.TextureInternal.Unbind();
}
}
public static class NativeWindowExtensions
{
public static unsafe void SwapBuffers(this NativeWindow window)
{
GLFW.SwapBuffers(window.WindowPtr);
}
}