From 4a9c2b4e4d3833f04994bca1825eea2c404d2c24 Mon Sep 17 00:00:00 2001 From: Harrison Date: Sat, 22 Feb 2020 00:00:59 -0500 Subject: [PATCH] Restructured code moving somethings to separate methods. --- .../Graphics/Render/Shaders/Shader.cs | 5 ++-- RecrownedGTK/Graphics/TextureData.cs | 25 +++++++++++-------- RecrownedGTK/Utilities.cs | 15 +++++++++++ 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 RecrownedGTK/Utilities.cs diff --git a/RecrownedGTK/Graphics/Render/Shaders/Shader.cs b/RecrownedGTK/Graphics/Render/Shaders/Shader.cs index 449091f..52bb252 100644 --- a/RecrownedGTK/Graphics/Render/Shaders/Shader.cs +++ b/RecrownedGTK/Graphics/Render/Shaders/Shader.cs @@ -92,10 +92,9 @@ namespace RecrownedGTK.Graphics.Render.Shaders { Dispose(false); } public static Shader CreateBasicShader() { - Assembly assembly = Assembly.GetExecutingAssembly(); string resourceLoc = "RecrownedGTK.Graphics.Render.Shaders.{0}"; - Shader shader = new Shader(new StreamReader(assembly.GetManifestResourceStream(String.Format(resourceLoc, "default.vert"))), - new StreamReader(assembly.GetManifestResourceStream(String.Format(resourceLoc, "default.frag")))); + Shader shader = new Shader(new StreamReader(Utilities.ReadEmbeddedFile(String.Format(resourceLoc, "default.vert"))), + new StreamReader(Utilities.ReadEmbeddedFile(String.Format(resourceLoc, "default.frag")))); return shader; } } diff --git a/RecrownedGTK/Graphics/TextureData.cs b/RecrownedGTK/Graphics/TextureData.cs index 04a64a4..f158247 100644 --- a/RecrownedGTK/Graphics/TextureData.cs +++ b/RecrownedGTK/Graphics/TextureData.cs @@ -14,19 +14,9 @@ namespace RecrownedGTK.Graphics { public TextureMagFilter textureMagFilter; public TextureData(string path) { - byte[] textureData = null; int width; int height; - using (FileStream file = new FileStream(path, FileMode.Open)) { - using (Bitmap bitmap = new Bitmap(file)) { - ImageConverter converter = new ImageConverter(); - textureData = (byte[]) converter.ConvertTo(bitmap, typeof(byte[])); - width = bitmap.Width; - height = bitmap.Height; - } - } - this.width = width; - this.height = height; + byte[] textureData = LoadPNG(path, out width, out height); GenerateTexture(textureData, width, height); } @@ -39,6 +29,19 @@ namespace RecrownedGTK.Graphics { this.height = height; GenerateTexture(textureData, width, height); } + + private byte[] LoadPNG(string path, out int width, out int height) { + byte[] textureData = null; + using (FileStream file = new FileStream(path, FileMode.Open)) { + using (Bitmap bitmap = new Bitmap(file)) { + ImageConverter converter = new ImageConverter(); + textureData = (byte[]) converter.ConvertTo(bitmap, typeof(byte[])); + width = bitmap.Width; + height = bitmap.Height; + } + } + return textureData; + } private void GenerateTexture(byte[] textureData, int width, int height) { if (handle != 0) throw new InvalidOperationException("This texture data already holds a texture."); diff --git a/RecrownedGTK/Utilities.cs b/RecrownedGTK/Utilities.cs new file mode 100644 index 0000000..1be109f --- /dev/null +++ b/RecrownedGTK/Utilities.cs @@ -0,0 +1,15 @@ +using System.IO; +using System.Reflection; +namespace RecrownedGTK { + public static class Utilities { + /// + /// Returns a stream with the embedded resource. + /// Remember to dispose when done. + /// + /// The path to the file in the assembly. + /// + public static Stream ReadEmbeddedFile(string assemblyPath) { + return Assembly.GetExecutingAssembly().GetManifestResourceStream(assemblyPath); + } + } +} \ No newline at end of file