From 84608d5981b95d72adc03b8ea70eaccd6dcd8cac Mon Sep 17 00:00:00 2001 From: David Allemang Date: Mon, 27 Feb 2017 01:04:30 -0500 Subject: [PATCH] Started refactors for logging and level loading --- Diamond/Buffers/GLBuffer.cs | 5 +- Diamond/Buffers/SubArray.cs | 12 ++-- Diamond/GLObject.cs | 15 +++-- Diamond/Shaders/Program.cs | 5 +- Diamond/Shaders/Shader.cs | 22 +++---- Diamond/Textures/Texture.cs | 5 +- hexworld/Driver.cs | 14 ----- hexworld/HexRender.cs | 71 ++++++++++++++--------- hexworld/hexworld.csproj | 22 +++---- hexworld/{ => res}/data_tile_grass.json | 0 hexworld/{ => res}/data_tile_gray.json | 0 hexworld/{ => res}/data_tile_stone.json | 0 hexworld/{ => res}/data_vert_cubes.json | 0 hexworld/{ => res}/data_vert_panels.json | 0 hexworld/{ => res}/data_vert_sides.json | 0 hexworld/{ => res}/grass.png | Bin hexworld/{ => res}/gray.png | Bin hexworld/{ => res}/s.fs.glsl | 0 hexworld/{ => res}/s.vs.glsl | 0 hexworld/{ => res}/stone.png | Bin 20 files changed, 85 insertions(+), 86 deletions(-) rename hexworld/{ => res}/data_tile_grass.json (100%) rename hexworld/{ => res}/data_tile_gray.json (100%) rename hexworld/{ => res}/data_tile_stone.json (100%) rename hexworld/{ => res}/data_vert_cubes.json (100%) rename hexworld/{ => res}/data_vert_panels.json (100%) rename hexworld/{ => res}/data_vert_sides.json (100%) rename hexworld/{ => res}/grass.png (100%) rename hexworld/{ => res}/gray.png (100%) rename hexworld/{ => res}/s.fs.glsl (100%) rename hexworld/{ => res}/s.vs.glsl (100%) rename hexworld/{ => res}/stone.png (100%) diff --git a/Diamond/Buffers/GLBuffer.cs b/Diamond/Buffers/GLBuffer.cs index 6537e4b..dfad2e8 100644 --- a/Diamond/Buffers/GLBuffer.cs +++ b/Diamond/Buffers/GLBuffer.cs @@ -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[] data) where T : struct { diff --git a/Diamond/Buffers/SubArray.cs b/Diamond/Buffers/SubArray.cs index 822beb1..351bf00 100644 --- a/Diamond/Buffers/SubArray.cs +++ b/Diamond/Buffers/SubArray.cs @@ -45,28 +45,28 @@ namespace Diamond.Buffers yield return Array[Offset + i]; } - public static T[] Join(params SubArray[] subArrays) => Join((IEnumerable>) subArrays); + public static T1[] Join(params SubArray[] subArrays) => Join((IEnumerable>) subArrays); - public static T[] Join(IEnumerable> subArrays) + public static T1[] Join(IEnumerable> subArrays) { - HashSet uniqueArrays = new HashSet(); + HashSet uniqueArrays = new HashSet(); 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(); + var offsets = new Dictionary(); 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); diff --git a/Diamond/GLObject.cs b/Diamond/GLObject.cs index 23eabbc..ab3d62a 100644 --- a/Diamond/GLObject.cs +++ b/Diamond/GLObject.cs @@ -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})"); } /// @@ -26,16 +31,18 @@ namespace Diamond /// protected abstract void Delete(); - private bool _disposed = false; - /// /// Free the name of this object /// 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); } diff --git a/Diamond/Shaders/Program.cs b/Diamond/Shaders/Program.cs index ffec477..7298774 100644 --- a/Diamond/Shaders/Program.cs +++ b/Diamond/Shaders/Program.cs @@ -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); diff --git a/Diamond/Shaders/Shader.cs b/Diamond/Shaders/Shader.cs index 4a05290..0ce79ad 100644 --- a/Diamond/Shaders/Shader.cs +++ b/Diamond/Shaders/Shader.cs @@ -13,7 +13,12 @@ namespace Diamond.Shaders /// The type of this shader. /// public readonly ShaderType Type; - + + /// + /// The source file name, if it was loaded from a file. + /// + public string SourceFile { get; private set; } + /// /// Gets and sets the shader source with glShaderSource and glGetShaderSource. /// @@ -55,13 +60,7 @@ namespace Diamond.Shaders Type = type; } - /// - /// Frees this gl object. Called by GLObject.Dispose(). - /// - protected override void Delete() - { - GL.DeleteShader(Id); - } + protected override void Delete() => GL.DeleteShader(Id); /// /// Compile the shader. @@ -93,8 +92,11 @@ namespace Diamond.Shaders /// The compiled shader 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; } diff --git a/Diamond/Textures/Texture.cs b/Diamond/Textures/Texture.cs index da57ddf..f9f29aa 100644 --- a/Diamond/Textures/Texture.cs +++ b/Diamond/Textures/Texture.cs @@ -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() { diff --git a/hexworld/Driver.cs b/hexworld/Driver.cs index 2a0606a..dc52878 100644 --- a/hexworld/Driver.cs +++ b/hexworld/Driver.cs @@ -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(new[] {1, 3, 5, 7, 9}); -// var s2 = new SubArray(new[] {0, 2, 4, 6, 8}); -// -// Console.Out.WriteLine($"s1 = {s1}"); -// Console.Out.WriteLine($"s2 = {s2}"); -// -// var arr = SubArray.Join(s1, s2); -// -// Console.Out.WriteLine($"arr = {string.Join(", ", arr)}"); -// Console.Out.WriteLine($"s1 = {s1}"); -// Console.Out.WriteLine($"s2 = {s2}"); -// -// Console.ReadKey(); } } } \ No newline at end of file diff --git a/hexworld/HexRender.cs b/hexworld/HexRender.cs index 606ef32..fd74eda 100644 --- a/hexworld/HexRender.cs +++ b/hexworld/HexRender.cs @@ -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( - JsonConvert.DeserializeObject(File.ReadAllText("data_vert_cubes.json"))); + JsonConvert.DeserializeObject(File.ReadAllText(@"res\data_vert_cubes.json"))); _panelVertices = new SubArray( - JsonConvert.DeserializeObject(File.ReadAllText("data_vert_panels.json"))); + JsonConvert.DeserializeObject(File.ReadAllText(@"res\data_vert_panels.json"))); _sidesVertices = new SubArray( - JsonConvert.DeserializeObject(File.ReadAllText("data_vert_sides.json"))); + JsonConvert.DeserializeObject(File.ReadAllText(@"res\data_vert_sides.json"))); _grassTiles = new SubArray( - JsonConvert.DeserializeObject(File.ReadAllText("data_tile_grass.json"))); + JsonConvert.DeserializeObject(File.ReadAllText(@"res\data_tile_grass.json"))); _stoneTiles = new SubArray( - JsonConvert.DeserializeObject(File.ReadAllText("data_tile_stone.json"))); + JsonConvert.DeserializeObject(File.ReadAllText(@"res\data_tile_stone.json"))); _grayTiles = new SubArray( - JsonConvert.DeserializeObject(File.ReadAllText("data_tile_gray.json"))); + JsonConvert.DeserializeObject(File.ReadAllText(@"res\data_tile_gray.json"))); _allTiles = SubArray.Join(_stoneTiles, _grassTiles, _grayTiles); _allVertices = SubArray.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(); - } } } \ No newline at end of file diff --git a/hexworld/hexworld.csproj b/hexworld/hexworld.csproj index e6417f5..9bf43e8 100644 --- a/hexworld/hexworld.csproj +++ b/hexworld/hexworld.csproj @@ -59,41 +59,41 @@ - + Always - + Always - + Always - + Always - + Always - + Always - + Always - + Always - + Always - + Always - + Always diff --git a/hexworld/data_tile_grass.json b/hexworld/res/data_tile_grass.json similarity index 100% rename from hexworld/data_tile_grass.json rename to hexworld/res/data_tile_grass.json diff --git a/hexworld/data_tile_gray.json b/hexworld/res/data_tile_gray.json similarity index 100% rename from hexworld/data_tile_gray.json rename to hexworld/res/data_tile_gray.json diff --git a/hexworld/data_tile_stone.json b/hexworld/res/data_tile_stone.json similarity index 100% rename from hexworld/data_tile_stone.json rename to hexworld/res/data_tile_stone.json diff --git a/hexworld/data_vert_cubes.json b/hexworld/res/data_vert_cubes.json similarity index 100% rename from hexworld/data_vert_cubes.json rename to hexworld/res/data_vert_cubes.json diff --git a/hexworld/data_vert_panels.json b/hexworld/res/data_vert_panels.json similarity index 100% rename from hexworld/data_vert_panels.json rename to hexworld/res/data_vert_panels.json diff --git a/hexworld/data_vert_sides.json b/hexworld/res/data_vert_sides.json similarity index 100% rename from hexworld/data_vert_sides.json rename to hexworld/res/data_vert_sides.json diff --git a/hexworld/grass.png b/hexworld/res/grass.png similarity index 100% rename from hexworld/grass.png rename to hexworld/res/grass.png diff --git a/hexworld/gray.png b/hexworld/res/gray.png similarity index 100% rename from hexworld/gray.png rename to hexworld/res/gray.png diff --git a/hexworld/s.fs.glsl b/hexworld/res/s.fs.glsl similarity index 100% rename from hexworld/s.fs.glsl rename to hexworld/res/s.fs.glsl diff --git a/hexworld/s.vs.glsl b/hexworld/res/s.vs.glsl similarity index 100% rename from hexworld/s.vs.glsl rename to hexworld/res/s.vs.glsl diff --git a/hexworld/stone.png b/hexworld/res/stone.png similarity index 100% rename from hexworld/stone.png rename to hexworld/res/stone.png