Changed dependencies, added dependencies for TTF rendering.

This commit is contained in:
Harrison Deng 2020-06-26 22:10:29 -05:00
parent 0225f0821c
commit 364803bf6c
5 changed files with 43 additions and 32 deletions

View File

@ -1,38 +1,27 @@
using System; using System.IO;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SlatedGameToolkit.Framework.Exceptions;
using SlatedGameToolkit.Framework.Graphics.OpenGL; using SlatedGameToolkit.Framework.Graphics.OpenGL;
using SlatedGameToolkit.Framework.Graphics.Textures; using SlatedGameToolkit.Framework.Graphics.Textures;
using StbImageSharp;
namespace SlatedGameToolkit.Commons.Loaders namespace SlatedGameToolkit.Commons.Loaders
{ {
public static class TextureLoader public static class TextureLoader
{ {
/// <summary> /// <summary>
/// Loads a texture using SDL2's image library. /// Loads a texture using StbImage library.
/// Therefore, technically, this function should be able to laod any format SDL2 Image can load. /// Any format supported by StbImage is therefore supported.
/// </summary> /// </summary>
/// <param name="path">The path of the texture to load.</param> /// <param name="path">The path of the texture to load.</param>
/// <param name="glContext">The OpenGL context the texture is to be associated with. May be null.</param> /// <param name="glContext">The OpenGL context to associate the texture with.</param>
/// <returns>An OpenGL Texture associated with the given context.</returns> /// <returns>A texture.</returns>
public static Texture LoadTexture(string path, GLContext glContext) public static Texture Load2DTexture(string path, GLContext glContext = null)
{ {
TextureData textureData; using (Stream stream = File.OpenRead(path))
using (Image<Rgba32> image = Image.Load<Rgba32>(path))
{ {
byte[] pixelData; ImageResult image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
Span<Rgba32> pixelDataSpan; TextureData textureData = new TextureData(image.Width, image.Height, image.Data);
if (image.TryGetSinglePixelSpan(out pixelDataSpan)) { return new Texture(textureData, glContext);
pixelData = MemoryMarshal.AsBytes(pixelDataSpan).ToArray();
} else {
throw new FrameworkUsageException("Image too large!");
}
textureData = new TextureData(image.Width, image.Height, pixelData);
} }
return new Texture(textureData, glContext);
} }
} }
} }

View File

@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-rc0003" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\SlatedGameToolkit.Framework\SlatedGameToolkit.Framework.csproj" /> <ProjectReference Include="..\SlatedGameToolkit.Framework\SlatedGameToolkit.Framework.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="StbImageSharp" Version="2.22.4" />
</ItemGroup>
</Project> </Project>

View File

@ -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. /// 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.
/// </summary> /// </summary>
public class VertexArrayBuffers : IDisposable { public class VertexArrayBuffers : IDisposable {
private uint vertexBufferLength, indexBufferLength;
private GLContext glContext; private GLContext glContext;
private bool disposed; private bool disposed;
private uint vertexBufferHandle, vertexArrayHandle, indexBufferHandle; private uint vertexBufferHandle, vertexArrayHandle, indexBufferHandle;
/// <summary>
/// Create a vertex array, a elements buffer, and an vertice array buffer.
///
/// Automatically checks if resizing the buffers are nessecary.
/// </summary>
/// <param name="context"></param>
public VertexArrayBuffers(GLContext context) { public VertexArrayBuffers(GLContext context) {
this.glContext = context ?? WindowContextsManager.CurrentGL; this.glContext = context ?? WindowContextsManager.CurrentGL;
@ -33,15 +41,27 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
public unsafe void BufferVertices(float[] data, bool dynamic) { public unsafe void BufferVertices(float[] data, bool dynamic) {
Use(); Use();
uint requiredLength = (uint) (sizeof(float) * data.Length);
fixed (void* pointer = &data[0]) { 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) { public unsafe void BufferIndices(uint[] data, bool dynamic) {
Use(); Use();
uint requiredLength = (uint) (sizeof(uint) * data.Length);
fixed (void* pointer = &data[0]) { 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));
}
} }
} }

View File

@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="StbTrueTypeSharp" Version="1.24.6" />
<PackageReference Include="Serilog" Version="2.9.0" /> <PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" /> <PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup> </ItemGroup>

View File

@ -51,7 +51,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
camera = new Camera2D(2, 2); camera = new Camera2D(2, 2);
renderer = new MeshBatch(camera); 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 = new RectangleMesh(logoTexture, Color.White);
logo.Width = 0.5f; logo.Width = 0.5f;
logo.Height = 0.5f; logo.Height = 0.5f;
@ -59,7 +59,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
logo.Y = -0.25f; 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 = new RectangleMesh(fillerTexture, Color.White);
textureTester.Width = 0.15f; textureTester.Width = 0.15f;
textureTester.Height = 0.15f; textureTester.Height = 0.15f;