121 lines
3.4 KiB
C#
121 lines
3.4 KiB
C#
using OpenTK.Graphics.OpenGL4;
|
|
using OpenTK.Windowing.Common;
|
|
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 uint frames = 0;
|
|
public double timeElapsed = 0;
|
|
|
|
// testing
|
|
public Chunk Chunk;
|
|
private Dictionary<int, bool> _cache = new Dictionary<int, bool>();
|
|
private bool chunkEmpty = false;
|
|
|
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
|
{
|
|
base.OnUpdateFrame(e);
|
|
|
|
if (Input.GetKey(OpenTK.Windowing.GraphicsLibraryFramework.Keys.Escape))
|
|
{
|
|
Close();
|
|
}
|
|
|
|
RemoveRandomBlocks();
|
|
}
|
|
|
|
private void RemoveRandomBlocks()
|
|
{
|
|
if (Chunk != null && !chunkEmpty)
|
|
{
|
|
Random rng = new Random();
|
|
|
|
int GetNum()
|
|
{
|
|
int max = 4096;
|
|
int num = rng.Next(max);
|
|
if (_cache.ContainsKey(num)) return GetNum();
|
|
_cache[num] = true;
|
|
if (_cache.Count == max) chunkEmpty = true;
|
|
return num;
|
|
}
|
|
|
|
Chunk.SetBlockIndex(GetNum(), Blocks.Air);
|
|
}
|
|
|
|
Renderer.ClearFaces();
|
|
Renderer.AddFaces(Chunk.GetChunkMesh().Faces);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|