Files
voxel/Window.cs
2025-09-03 15:08:21 +02:00

98 lines
2.7 KiB
C#

using OpenTK.Graphics.OpenGL4;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Common.Input;
using OpenTK.Windowing.Desktop;
namespace Voxel
{
public class Window(int width, int height, string title) : GameWindow(GameWindowSettings.Default, new NativeWindowSettings() { ClientSize = (width, height), Title = title })
{
public readonly int Width = width;
public readonly int Height = height;
public Player Player;
public uint frames = 0;
public double timeElapsed = 0;
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (Input.GetKey(OpenTK.Windowing.GraphicsLibraryFramework.Keys.Escape))
{
Close();
}
if (Player != null)
{
Player.Update((float)e.Time);
}
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
frames++;
timeElapsed += e.Time;
if (timeElapsed >= 1)
{
Console.WriteLine("FPS: " + frames.ToString());
timeElapsed = 0;
frames = 0;
}
Camera.Update((float)e.Time);
Renderer.Render();
SwapBuffers();
}
protected override void OnFramebufferResize(FramebufferResizeEventArgs e)
{
base.OnFramebufferResize(e);
Camera.UpdateProjection(e.Width, e.Height);
GL.Viewport(0, 0, e.Width, e.Height);
}
protected override void OnLoad()
{
base.OnLoad();
GL.ClearColor(0.72f, 0.88f, 0.97f, 1f);
GL.Enable(EnableCap.CullFace);
GL.Enable(EnableCap.DepthTest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
CursorState = CursorState.Grabbed;
VSync = VSyncMode.On;
Camera.UpdateProjection(Width, Height);
}
protected override void OnMouseMove(MouseMoveEventArgs e)
{
base.OnMouseMove(e);
Camera.UpdateMouse(e.Delta);
}
protected override void OnKeyUp(KeyboardKeyEventArgs e)
{
base.OnKeyUp(e);
Input.SetKey(e.Key, false);
}
protected override void OnKeyDown(KeyboardKeyEventArgs e)
{
base.OnKeyDown(e);
Input.SetKey(e.Key, true);
}
}
}