Started refactors for logging and level loading
This commit is contained in:
@@ -25,10 +25,7 @@ namespace Diamond.Buffers
|
||||
GL.BindBuffer(Target, Id);
|
||||
}
|
||||
|
||||
protected override void Delete()
|
||||
{
|
||||
GL.DeleteBuffer(Id);
|
||||
}
|
||||
protected override void Delete() => GL.DeleteBuffer(Id);
|
||||
|
||||
public void Data<T>(T[] data) where T : struct
|
||||
{
|
||||
|
||||
@@ -45,28 +45,28 @@ namespace Diamond.Buffers
|
||||
yield return Array[Offset + i];
|
||||
}
|
||||
|
||||
public static T[] Join<T>(params SubArray<T>[] subArrays) => Join((IEnumerable<SubArray<T>>) subArrays);
|
||||
public static T1[] Join<T1>(params SubArray<T1>[] subArrays) => Join((IEnumerable<SubArray<T1>>) subArrays);
|
||||
|
||||
public static T[] Join<T>(IEnumerable<SubArray<T>> subArrays)
|
||||
public static T1[] Join<T1>(IEnumerable<SubArray<T1>> subArrays)
|
||||
{
|
||||
HashSet<T[]> uniqueArrays = new HashSet<T[]>();
|
||||
HashSet<T1[]> uniqueArrays = new HashSet<T1[]>();
|
||||
foreach (var subArray in subArrays)
|
||||
{
|
||||
uniqueArrays.Add(subArray.Array);
|
||||
}
|
||||
|
||||
if (uniqueArrays.Count == 0) return new T[0];
|
||||
if (uniqueArrays.Count == 0) return new T1[0];
|
||||
if (uniqueArrays.Count == 1) return uniqueArrays.ToArray()[0];
|
||||
|
||||
var length = 0;
|
||||
var offsets = new Dictionary<T[], int>();
|
||||
var offsets = new Dictionary<T1[], int>();
|
||||
foreach (var uniqueArray in uniqueArrays)
|
||||
{
|
||||
offsets[uniqueArray] = length;
|
||||
length += uniqueArray.Length;
|
||||
}
|
||||
|
||||
var array = new T[length];
|
||||
var array = new T1[length];
|
||||
foreach (var uniqueArray in uniqueArrays)
|
||||
{
|
||||
System.Array.ConstrainedCopy(uniqueArray, 0, array, offsets[uniqueArray], uniqueArray.Length);
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using OpenTK.Graphics;
|
||||
using OpenTK.Graphics.OpenGL;
|
||||
|
||||
namespace Diamond
|
||||
{
|
||||
@@ -19,6 +23,7 @@ namespace Diamond
|
||||
protected GLObject(uint id)
|
||||
{
|
||||
Id = id;
|
||||
Debug.WriteLine($"Created GLObject {ToString()} ({Id})");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -26,16 +31,18 @@ namespace Diamond
|
||||
/// </summary>
|
||||
protected abstract void Delete();
|
||||
|
||||
private bool _disposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Free the name of this object
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
if (GraphicsContext.CurrentContext == null)
|
||||
{
|
||||
Debug.WriteLine($"No active context. Assuming {this} ({Id}) is disposed.", "[Warning]");
|
||||
return;
|
||||
}
|
||||
Delete();
|
||||
Debug.WriteLine($"Disposed GLObject {ToString()} ({Id})");
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,7 @@ namespace Diamond.Shaders
|
||||
{
|
||||
}
|
||||
|
||||
protected override void Delete()
|
||||
{
|
||||
GL.DeleteProgram(Id);
|
||||
}
|
||||
protected override void Delete() => GL.DeleteProgram(Id);
|
||||
|
||||
public void Attach(Shader shader) => GL.AttachShader(Id, shader.Id);
|
||||
|
||||
|
||||
@@ -13,7 +13,12 @@ namespace Diamond.Shaders
|
||||
/// The type of this shader.
|
||||
/// </summary>
|
||||
public readonly ShaderType Type;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The source file name, if it was loaded from a file.
|
||||
/// </summary>
|
||||
public string SourceFile { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets and sets the shader source with <code>glShaderSource</code> and <code>glGetShaderSource</code>.
|
||||
/// </summary>
|
||||
@@ -55,13 +60,7 @@ namespace Diamond.Shaders
|
||||
Type = type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Frees this gl object. Called by <code>GLObject.Dispose()</code>.
|
||||
/// </summary>
|
||||
protected override void Delete()
|
||||
{
|
||||
GL.DeleteShader(Id);
|
||||
}
|
||||
protected override void Delete() => GL.DeleteShader(Id);
|
||||
|
||||
/// <summary>
|
||||
/// Compile the shader.
|
||||
@@ -93,8 +92,11 @@ namespace Diamond.Shaders
|
||||
/// <returns>The compiled shader</returns>
|
||||
public static Shader FromFile(string path, ShaderType type, out bool success)
|
||||
{
|
||||
var s = new Shader(type);
|
||||
s.Source = File.ReadAllText(path);
|
||||
var s = new Shader(type)
|
||||
{
|
||||
Source = File.ReadAllText(path),
|
||||
SourceFile = ""
|
||||
};
|
||||
success = s.Compile();
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -18,10 +18,7 @@ namespace Diamond.Textures
|
||||
Target = target;
|
||||
}
|
||||
|
||||
protected override void Delete()
|
||||
{
|
||||
GL.DeleteTexture(Id);
|
||||
}
|
||||
protected override void Delete() => GL.DeleteTexture(Id);
|
||||
|
||||
public void Bind()
|
||||
{
|
||||
|
||||
@@ -16,20 +16,6 @@ namespace hexworld
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
using (var gw = new HexRender(1280, 720)) gw.Run();
|
||||
|
||||
// var s1 = new SubArray<int>(new[] {1, 3, 5, 7, 9});
|
||||
// var s2 = new SubArray<int>(new[] {0, 2, 4, 6, 8});
|
||||
//
|
||||
// Console.Out.WriteLine($"s1 = {s1}");
|
||||
// Console.Out.WriteLine($"s2 = {s2}");
|
||||
//
|
||||
// var arr = SubArray<int>.Join(s1, s2);
|
||||
//
|
||||
// Console.Out.WriteLine($"arr = {string.Join(", ", arr)}");
|
||||
// Console.Out.WriteLine($"s1 = {s1}");
|
||||
// Console.Out.WriteLine($"s2 = {s2}");
|
||||
//
|
||||
// Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,12 +14,35 @@ namespace hexworld
|
||||
{
|
||||
public class HexRender : GameWindow
|
||||
{
|
||||
#region Fields
|
||||
|
||||
#region GLObjects
|
||||
|
||||
private Program _pgm;
|
||||
|
||||
private Texture _grass;
|
||||
private Texture _stone;
|
||||
private Texture _gray;
|
||||
|
||||
private GLBuffer _tileGLBuffer;
|
||||
private GLBuffer _vertexGLBuffer;
|
||||
|
||||
protected override void OnClosed(EventArgs e)
|
||||
{
|
||||
base.OnClosed(e);
|
||||
|
||||
_pgm.Dispose();
|
||||
|
||||
// _tileGLBuffer.Dispose();
|
||||
_vertexGLBuffer.Dispose();
|
||||
|
||||
_grass.Dispose();
|
||||
_stone.Dispose();
|
||||
_gray.Dispose();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private Matrix4 _view;
|
||||
private Matrix4 _proj;
|
||||
|
||||
@@ -34,11 +57,11 @@ namespace hexworld
|
||||
private Tile[] _allTiles;
|
||||
private Vertex[] _allVertices;
|
||||
|
||||
private GLBuffer _tileGLBuffer;
|
||||
private GLBuffer _vertexGLBuffer;
|
||||
|
||||
private double _time;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public HexRender(int width, int height)
|
||||
: base(width, height, new GraphicsMode(32, 24, 0, 0))
|
||||
{
|
||||
@@ -52,8 +75,11 @@ namespace hexworld
|
||||
{
|
||||
base.OnLoad(e);
|
||||
|
||||
using (var vs = Shader.FromFile("s.vs.glsl", ShaderType.VertexShader))
|
||||
using (var fs = Shader.FromFile("s.fs.glsl", ShaderType.FragmentShader))
|
||||
var vsPath = @"res\s.vs.glsl";
|
||||
var fsPath = @"res\s.fs.glsl";
|
||||
|
||||
using (var vs = Shader.FromFile(vsPath, ShaderType.VertexShader))
|
||||
using (var fs = Shader.FromFile(fsPath, ShaderType.FragmentShader))
|
||||
{
|
||||
if (!vs.Compiled | !fs.Compiled)
|
||||
{
|
||||
@@ -75,18 +101,18 @@ namespace hexworld
|
||||
}
|
||||
|
||||
_cubeVertices = new SubArray<Vertex>(
|
||||
JsonConvert.DeserializeObject<Vertex[]>(File.ReadAllText("data_vert_cubes.json")));
|
||||
JsonConvert.DeserializeObject<Vertex[]>(File.ReadAllText(@"res\data_vert_cubes.json")));
|
||||
_panelVertices = new SubArray<Vertex>(
|
||||
JsonConvert.DeserializeObject<Vertex[]>(File.ReadAllText("data_vert_panels.json")));
|
||||
JsonConvert.DeserializeObject<Vertex[]>(File.ReadAllText(@"res\data_vert_panels.json")));
|
||||
_sidesVertices = new SubArray<Vertex>(
|
||||
JsonConvert.DeserializeObject<Vertex[]>(File.ReadAllText("data_vert_sides.json")));
|
||||
JsonConvert.DeserializeObject<Vertex[]>(File.ReadAllText(@"res\data_vert_sides.json")));
|
||||
|
||||
_grassTiles = new SubArray<Tile>(
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText("data_tile_grass.json")));
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText(@"res\data_tile_grass.json")));
|
||||
_stoneTiles = new SubArray<Tile>(
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText("data_tile_stone.json")));
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText(@"res\data_tile_stone.json")));
|
||||
_grayTiles = new SubArray<Tile>(
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText("data_tile_gray.json")));
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText(@"res\data_tile_gray.json")));
|
||||
|
||||
_allTiles = SubArray<Tile>.Join(_stoneTiles, _grassTiles, _grayTiles);
|
||||
_allVertices = SubArray<Vertex>.Join(_panelVertices, _cubeVertices, _sidesVertices);
|
||||
@@ -100,9 +126,9 @@ namespace hexworld
|
||||
_pgm.SetAttribPointers(_tileGLBuffer, typeof(Tile));
|
||||
_pgm.SetAttribPointers(_vertexGLBuffer, typeof(Vertex));
|
||||
|
||||
_grass = Texture.FromBitmap(new Bitmap("grass.png"));
|
||||
_stone = Texture.FromBitmap(new Bitmap("stone.png"));
|
||||
_gray = Texture.FromBitmap(new Bitmap("gray.png"));
|
||||
_grass = Texture.FromBitmap(new Bitmap(@"res\grass.png"));
|
||||
_stone = Texture.FromBitmap(new Bitmap(@"res\stone.png"));
|
||||
_gray = Texture.FromBitmap(new Bitmap(@"res\gray.png"));
|
||||
}
|
||||
|
||||
protected override void OnUpdateFrame(FrameEventArgs e)
|
||||
@@ -111,7 +137,8 @@ namespace hexworld
|
||||
|
||||
_time += e.Time;
|
||||
|
||||
_view = Matrix4.CreateRotationZ((float) _time/3)*Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ);
|
||||
_view = Matrix4.CreateRotationZ((float) _time / 3) *
|
||||
Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ);
|
||||
_proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, -100, 100);
|
||||
|
||||
for (var i = 0; i < _grassTiles.Length; i++)
|
||||
@@ -179,19 +206,5 @@ namespace hexworld
|
||||
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
protected override void OnClosed(EventArgs e)
|
||||
{
|
||||
base.OnClosed(e);
|
||||
|
||||
_pgm.Dispose();
|
||||
|
||||
_tileGLBuffer.Dispose();
|
||||
_vertexGLBuffer.Dispose();
|
||||
|
||||
_grass.Dispose();
|
||||
_stone.Dispose();
|
||||
_gray.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,41 +59,41 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
<None Include="data_tile_gray.json">
|
||||
<None Include="res\data_tile_gray.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="data_tile_stone.json">
|
||||
<None Include="res\data_tile_stone.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="data_vert_sides.json">
|
||||
<None Include="res\data_vert_sides.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="data_vert_panels.json">
|
||||
<None Include="res\data_vert_panels.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="OpenTK.dll.config" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="s.fs.glsl">
|
||||
<None Include="res\s.fs.glsl">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="s.vs.glsl">
|
||||
<None Include="res\s.vs.glsl">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="data_vert_cubes.json">
|
||||
<None Include="res\data_vert_cubes.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="data_tile_grass.json">
|
||||
<None Include="res\data_tile_grass.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="grass.png">
|
||||
<Content Include="res\grass.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="gray.png">
|
||||
<Content Include="res\gray.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="stone.png">
|
||||
<Content Include="res\stone.png">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
|
Before Width: | Height: | Size: 771 B After Width: | Height: | Size: 771 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 670 B After Width: | Height: | Size: 670 B |
Reference in New Issue
Block a user