Started refactors for logging and level loading

This commit is contained in:
2017-02-27 01:04:30 -05:00
parent a7c2f5bb29
commit 84608d5981
20 changed files with 85 additions and 86 deletions

View File

@@ -25,10 +25,7 @@ namespace Diamond.Buffers
GL.BindBuffer(Target, Id); GL.BindBuffer(Target, Id);
} }
protected override void Delete() protected override void Delete() => GL.DeleteBuffer(Id);
{
GL.DeleteBuffer(Id);
}
public void Data<T>(T[] data) where T : struct public void Data<T>(T[] data) where T : struct
{ {

View File

@@ -45,28 +45,28 @@ namespace Diamond.Buffers
yield return Array[Offset + i]; 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) foreach (var subArray in subArrays)
{ {
uniqueArrays.Add(subArray.Array); 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]; if (uniqueArrays.Count == 1) return uniqueArrays.ToArray()[0];
var length = 0; var length = 0;
var offsets = new Dictionary<T[], int>(); var offsets = new Dictionary<T1[], int>();
foreach (var uniqueArray in uniqueArrays) foreach (var uniqueArray in uniqueArrays)
{ {
offsets[uniqueArray] = length; offsets[uniqueArray] = length;
length += uniqueArray.Length; length += uniqueArray.Length;
} }
var array = new T[length]; var array = new T1[length];
foreach (var uniqueArray in uniqueArrays) foreach (var uniqueArray in uniqueArrays)
{ {
System.Array.ConstrainedCopy(uniqueArray, 0, array, offsets[uniqueArray], uniqueArray.Length); System.Array.ConstrainedCopy(uniqueArray, 0, array, offsets[uniqueArray], uniqueArray.Length);

View File

@@ -1,4 +1,8 @@
using System; using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
namespace Diamond namespace Diamond
{ {
@@ -19,6 +23,7 @@ namespace Diamond
protected GLObject(uint id) protected GLObject(uint id)
{ {
Id = id; Id = id;
Debug.WriteLine($"Created GLObject {ToString()} ({Id})");
} }
/// <summary> /// <summary>
@@ -26,16 +31,18 @@ namespace Diamond
/// </summary> /// </summary>
protected abstract void Delete(); protected abstract void Delete();
private bool _disposed = false;
/// <summary> /// <summary>
/// Free the name of this object /// Free the name of this object
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
if (_disposed) return; if (GraphicsContext.CurrentContext == null)
_disposed = true; {
Debug.WriteLine($"No active context. Assuming {this} ({Id}) is disposed.", "[Warning]");
return;
}
Delete(); Delete();
Debug.WriteLine($"Disposed GLObject {ToString()} ({Id})");
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }

View File

@@ -19,10 +19,7 @@ namespace Diamond.Shaders
{ {
} }
protected override void Delete() protected override void Delete() => GL.DeleteProgram(Id);
{
GL.DeleteProgram(Id);
}
public void Attach(Shader shader) => GL.AttachShader(Id, shader.Id); public void Attach(Shader shader) => GL.AttachShader(Id, shader.Id);

View File

@@ -13,7 +13,12 @@ namespace Diamond.Shaders
/// The type of this shader. /// The type of this shader.
/// </summary> /// </summary>
public readonly ShaderType Type; public readonly ShaderType Type;
/// <summary>
/// The source file name, if it was loaded from a file.
/// </summary>
public string SourceFile { get; private set; }
/// <summary> /// <summary>
/// Gets and sets the shader source with <code>glShaderSource</code> and <code>glGetShaderSource</code>. /// Gets and sets the shader source with <code>glShaderSource</code> and <code>glGetShaderSource</code>.
/// </summary> /// </summary>
@@ -55,13 +60,7 @@ namespace Diamond.Shaders
Type = type; Type = type;
} }
/// <summary> protected override void Delete() => GL.DeleteShader(Id);
/// Frees this gl object. Called by <code>GLObject.Dispose()</code>.
/// </summary>
protected override void Delete()
{
GL.DeleteShader(Id);
}
/// <summary> /// <summary>
/// Compile the shader. /// Compile the shader.
@@ -93,8 +92,11 @@ namespace Diamond.Shaders
/// <returns>The compiled shader</returns> /// <returns>The compiled shader</returns>
public static Shader FromFile(string path, ShaderType type, out bool success) public static Shader FromFile(string path, ShaderType type, out bool success)
{ {
var s = new Shader(type); var s = new Shader(type)
s.Source = File.ReadAllText(path); {
Source = File.ReadAllText(path),
SourceFile = ""
};
success = s.Compile(); success = s.Compile();
return s; return s;
} }

View File

@@ -18,10 +18,7 @@ namespace Diamond.Textures
Target = target; Target = target;
} }
protected override void Delete() protected override void Delete() => GL.DeleteTexture(Id);
{
GL.DeleteTexture(Id);
}
public void Bind() public void Bind()
{ {

View File

@@ -16,20 +16,6 @@ namespace hexworld
public static void Main(string[] args) public static void Main(string[] args)
{ {
using (var gw = new HexRender(1280, 720)) gw.Run(); 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();
} }
} }
} }

View File

@@ -14,12 +14,35 @@ namespace hexworld
{ {
public class HexRender : GameWindow public class HexRender : GameWindow
{ {
#region Fields
#region GLObjects
private Program _pgm; private Program _pgm;
private Texture _grass; private Texture _grass;
private Texture _stone; private Texture _stone;
private Texture _gray; 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 _view;
private Matrix4 _proj; private Matrix4 _proj;
@@ -34,11 +57,11 @@ namespace hexworld
private Tile[] _allTiles; private Tile[] _allTiles;
private Vertex[] _allVertices; private Vertex[] _allVertices;
private GLBuffer _tileGLBuffer;
private GLBuffer _vertexGLBuffer;
private double _time; private double _time;
#endregion
public HexRender(int width, int height) public HexRender(int width, int height)
: base(width, height, new GraphicsMode(32, 24, 0, 0)) : base(width, height, new GraphicsMode(32, 24, 0, 0))
{ {
@@ -52,8 +75,11 @@ namespace hexworld
{ {
base.OnLoad(e); base.OnLoad(e);
using (var vs = Shader.FromFile("s.vs.glsl", ShaderType.VertexShader)) var vsPath = @"res\s.vs.glsl";
using (var fs = Shader.FromFile("s.fs.glsl", ShaderType.FragmentShader)) 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) if (!vs.Compiled | !fs.Compiled)
{ {
@@ -75,18 +101,18 @@ namespace hexworld
} }
_cubeVertices = new SubArray<Vertex>( _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>( _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>( _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>( _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>( _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>( _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); _allTiles = SubArray<Tile>.Join(_stoneTiles, _grassTiles, _grayTiles);
_allVertices = SubArray<Vertex>.Join(_panelVertices, _cubeVertices, _sidesVertices); _allVertices = SubArray<Vertex>.Join(_panelVertices, _cubeVertices, _sidesVertices);
@@ -100,9 +126,9 @@ namespace hexworld
_pgm.SetAttribPointers(_tileGLBuffer, typeof(Tile)); _pgm.SetAttribPointers(_tileGLBuffer, typeof(Tile));
_pgm.SetAttribPointers(_vertexGLBuffer, typeof(Vertex)); _pgm.SetAttribPointers(_vertexGLBuffer, typeof(Vertex));
_grass = Texture.FromBitmap(new Bitmap("grass.png")); _grass = Texture.FromBitmap(new Bitmap(@"res\grass.png"));
_stone = Texture.FromBitmap(new Bitmap("stone.png")); _stone = Texture.FromBitmap(new Bitmap(@"res\stone.png"));
_gray = Texture.FromBitmap(new Bitmap("gray.png")); _gray = Texture.FromBitmap(new Bitmap(@"res\gray.png"));
} }
protected override void OnUpdateFrame(FrameEventArgs e) protected override void OnUpdateFrame(FrameEventArgs e)
@@ -111,7 +137,8 @@ namespace hexworld
_time += e.Time; _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); _proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, -100, 100);
for (var i = 0; i < _grassTiles.Length; i++) for (var i = 0; i < _grassTiles.Length; i++)
@@ -179,19 +206,5 @@ namespace hexworld
SwapBuffers(); SwapBuffers();
} }
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
_pgm.Dispose();
_tileGLBuffer.Dispose();
_vertexGLBuffer.Dispose();
_grass.Dispose();
_stone.Dispose();
_gray.Dispose();
}
} }
} }

View File

@@ -59,41 +59,41 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<None Include="data_tile_gray.json"> <None Include="res\data_tile_gray.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="data_tile_stone.json"> <None Include="res\data_tile_stone.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="data_vert_sides.json"> <None Include="res\data_vert_sides.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="data_vert_panels.json"> <None Include="res\data_vert_panels.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="OpenTK.dll.config" /> <None Include="OpenTK.dll.config" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="s.fs.glsl"> <None Include="res\s.fs.glsl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="s.vs.glsl"> <None Include="res\s.vs.glsl">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="data_vert_cubes.json"> <None Include="res\data_vert_cubes.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="data_tile_grass.json"> <None Include="res\data_tile_grass.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="grass.png"> <Content Include="res\grass.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Include="gray.png"> <Content Include="res\gray.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Include="stone.png"> <Content Include="res\stone.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
</ItemGroup> </ItemGroup>

View File

Before

Width:  |  Height:  |  Size: 771 B

After

Width:  |  Height:  |  Size: 771 B

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 670 B

After

Width:  |  Height:  |  Size: 670 B