From 364803bf6cc357bea33031e3aed14fa115e3e128 Mon Sep 17 00:00:00 2001 From: Harrison Date: Fri, 26 Jun 2020 22:10:29 -0500 Subject: [PATCH] Changed dependencies, added dependencies for TTF rendering. --- .../Loaders/TextureLoader.cs | 33 +++++++------------ .../SlatedGameToolkit.Commons.csproj | 13 ++++---- .../Graphics/Render/VertexArrayBuffers.cs | 24 ++++++++++++-- .../SlatedGameToolkit.Framework.csproj | 1 + .../Utilities/Playground/MainState.cs | 4 +-- 5 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/SlatedGameToolkit.Commons/Loaders/TextureLoader.cs b/src/SlatedGameToolkit.Commons/Loaders/TextureLoader.cs index 47e73b4..b4d427e 100644 --- a/src/SlatedGameToolkit.Commons/Loaders/TextureLoader.cs +++ b/src/SlatedGameToolkit.Commons/Loaders/TextureLoader.cs @@ -1,38 +1,27 @@ -using System; -using System.Runtime.InteropServices; -using SixLabors.ImageSharp; -using SixLabors.ImageSharp.PixelFormats; -using SlatedGameToolkit.Framework.Exceptions; +using System.IO; using SlatedGameToolkit.Framework.Graphics.OpenGL; using SlatedGameToolkit.Framework.Graphics.Textures; +using StbImageSharp; namespace SlatedGameToolkit.Commons.Loaders { public static class TextureLoader { /// - /// Loads a texture using SDL2's image library. - /// Therefore, technically, this function should be able to laod any format SDL2 Image can load. + /// Loads a texture using StbImage library. + /// Any format supported by StbImage is therefore supported. /// /// The path of the texture to load. - /// The OpenGL context the texture is to be associated with. May be null. - /// An OpenGL Texture associated with the given context. - public static Texture LoadTexture(string path, GLContext glContext) + /// The OpenGL context to associate the texture with. + /// A texture. + public static Texture Load2DTexture(string path, GLContext glContext = null) { - TextureData textureData; - using (Image image = Image.Load(path)) + using (Stream stream = File.OpenRead(path)) { - byte[] pixelData; - Span pixelDataSpan; - if (image.TryGetSinglePixelSpan(out pixelDataSpan)) { - pixelData = MemoryMarshal.AsBytes(pixelDataSpan).ToArray(); - } else { - throw new FrameworkUsageException("Image too large!"); - } - textureData = new TextureData(image.Width, image.Height, pixelData); + ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha); + TextureData textureData = new TextureData(image.Width, image.Height, image.Data); + return new Texture(textureData, glContext); } - - return new Texture(textureData, glContext); } } } diff --git a/src/SlatedGameToolkit.Commons/SlatedGameToolkit.Commons.csproj b/src/SlatedGameToolkit.Commons/SlatedGameToolkit.Commons.csproj index cd9d73f..099bc47 100644 --- a/src/SlatedGameToolkit.Commons/SlatedGameToolkit.Commons.csproj +++ b/src/SlatedGameToolkit.Commons/SlatedGameToolkit.Commons.csproj @@ -1,15 +1,16 @@ - netstandard2.1 + netcoreapp3.1 + true - - - - - + + + + + diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/VertexArrayBuffers.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/VertexArrayBuffers.cs index 0fe0d79..4a54bb9 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/VertexArrayBuffers.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/VertexArrayBuffers.cs @@ -8,9 +8,17 @@ namespace SlatedGameToolkit.Framework.Graphics.Render /// A set of two buffers, one for the vertices, and one for the indices. Also defines an vertex array that defines the attributes of the buffers. /// public class VertexArrayBuffers : IDisposable { + private uint vertexBufferLength, indexBufferLength; private GLContext glContext; private bool disposed; private uint vertexBufferHandle, vertexArrayHandle, indexBufferHandle; + + /// + /// Create a vertex array, a elements buffer, and an vertice array buffer. + /// + /// Automatically checks if resizing the buffers are nessecary. + /// + /// public VertexArrayBuffers(GLContext context) { this.glContext = context ?? WindowContextsManager.CurrentGL; @@ -33,15 +41,27 @@ namespace SlatedGameToolkit.Framework.Graphics.Render public unsafe void BufferVertices(float[] data, bool dynamic) { Use(); + uint requiredLength = (uint) (sizeof(float) * data.Length); fixed (void* pointer = &data[0]) { - glContext.BufferData(OpenGL.BufferTargetARB.ArrayBuffer, (uint) (sizeof(float) * data.Length), new IntPtr(pointer), dynamic ? OpenGL.BufferUsageARB.DynamicDraw : OpenGL.BufferUsageARB.StaticDraw); + if (requiredLength > vertexBufferLength) { + glContext.BufferData(BufferTargetARB.ArrayBuffer, requiredLength, new IntPtr(pointer), dynamic ? OpenGL.BufferUsageARB.DynamicDraw : OpenGL.BufferUsageARB.StaticDraw); + vertexBufferLength = requiredLength; + } else { + glContext.BufferSubData(BufferTargetARB.ArrayBuffer, IntPtr.Zero, new IntPtr(requiredLength), new IntPtr(pointer)); + } } } public unsafe void BufferIndices(uint[] data, bool dynamic) { Use(); + uint requiredLength = (uint) (sizeof(uint) * data.Length); fixed (void* pointer = &data[0]) { - glContext.BufferData(OpenGL.BufferTargetARB.ElementArrayBuffer, (uint) (sizeof(uint) * data.Length), new IntPtr(pointer), dynamic ? OpenGL.BufferUsageARB.DynamicDraw : OpenGL.BufferUsageARB.StaticDraw); + if (requiredLength > indexBufferLength) { + glContext.BufferData(OpenGL.BufferTargetARB.ElementArrayBuffer, requiredLength, new IntPtr(pointer), dynamic ? OpenGL.BufferUsageARB.DynamicDraw : OpenGL.BufferUsageARB.StaticDraw); + indexBufferLength = requiredLength; + } else { + glContext.BufferSubData(BufferTargetARB.ElementArrayBuffer, IntPtr.Zero, new IntPtr(requiredLength), new IntPtr(pointer)); + } } } diff --git a/src/SlatedGameToolkit.Framework/SlatedGameToolkit.Framework.csproj b/src/SlatedGameToolkit.Framework/SlatedGameToolkit.Framework.csproj index ba806ab..9087452 100644 --- a/src/SlatedGameToolkit.Framework/SlatedGameToolkit.Framework.csproj +++ b/src/SlatedGameToolkit.Framework/SlatedGameToolkit.Framework.csproj @@ -6,6 +6,7 @@ + diff --git a/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs b/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs index 12a5b71..f7b61c7 100644 --- a/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs +++ b/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs @@ -51,7 +51,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground camera = new Camera2D(2, 2); renderer = new MeshBatch(camera); - logoTexture = TextureLoader.LoadTexture("Resources/Playground/yhdnbgnc.png", null); + logoTexture = TextureLoader.Load2DTexture("Resources/Playground/yhdnbgnc.png", null); logo = new RectangleMesh(logoTexture, Color.White); logo.Width = 0.5f; logo.Height = 0.5f; @@ -59,7 +59,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground logo.Y = -0.25f; - fillerTexture = TextureLoader.LoadTexture("Resources/Playground/filler.png", null); + fillerTexture = TextureLoader.Load2DTexture("Resources/Playground/filler.png", null); textureTester = new RectangleMesh(fillerTexture, Color.White); textureTester.Width = 0.15f; textureTester.Height = 0.15f;