Restructured code moving somethings to separate methods.

This commit is contained in:
Harrison Deng 2020-02-22 00:00:59 -05:00
parent 5699caf9e4
commit 4a9c2b4e4d
3 changed files with 31 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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.");

15
RecrownedGTK/Utilities.cs Normal file
View File

@ -0,0 +1,15 @@
using System.IO;
using System.Reflection;
namespace RecrownedGTK {
public static class Utilities {
/// <summary>
/// Returns a stream with the embedded resource.
/// Remember to dispose when done.
/// </summary>
/// <param name="assemblyPath">The path to the file in the assembly.</param>
/// <returns></returns>
public static Stream ReadEmbeddedFile(string assemblyPath) {
return Assembly.GetExecutingAssembly().GetManifestResourceStream(assemblyPath);
}
}
}