Refactor ResourceLoader, use only Texture2d in Renderer.

This commit is contained in:
2023-06-14 23:48:26 +02:00
parent fdbd21f248
commit 06da2c3f7f
20 changed files with 128 additions and 158 deletions

View File

@@ -11,7 +11,7 @@ namespace DaggerFramework
protected override void OnStart()
{
var renderer = Layer.Scene.Renderer;
_texId = renderer.LoadTexture(Texture.Path);
_texId = renderer.LoadTexture(_texture);
}
public override void OnDraw(ref Renderer renderer)

21
Source/Entities/Text2d.cs Normal file
View File

@@ -0,0 +1,21 @@
using DaggerFramework.Rendering;
using System.Drawing;
namespace DaggerFramework
{
public class Text2d : Drawable2d
{
public string Contents { get => _contents; set => _contents = value; }
public int FontSize { get => _fontSize; set => _fontSize = value; }
public Color FontColor { get => _fontColor; set => _fontColor = value; }
public override void OnDraw(ref Renderer renderer)
{
renderer.DrawDebugText(position, _contents, _fontSize, _fontColor);
}
private string _contents = string.Empty;
private int _fontSize = 16;
private Color _fontColor = Color.White;
}
}

View File

@@ -2,6 +2,7 @@ namespace DaggerFramework
{
public abstract class Game
{
public abstract string ResourceRoot { get; }
public void Start()
{
LoadResources();

View File

@@ -2,7 +2,6 @@ using System.Drawing;
using System.Numerics;
using Silk.NET.GLFW;
using Silk.NET.Maths;
using Silk.NET.OpenGL;
namespace DaggerFramework.Rendering
@@ -71,11 +70,6 @@ namespace DaggerFramework.Rendering
throw new NotImplementedException();
}
public override int LoadTexture(string path)
{
throw new NotImplementedException();
}
public override void SetTargetFps(int fps)
{
return;

View File

@@ -20,7 +20,7 @@ namespace DaggerFramework.Rendering
public override void CreateWindow(string title, Vector2 size)
{
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
// Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
_windowSize = size;
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
}
@@ -77,41 +77,30 @@ namespace DaggerFramework.Rendering
public override int LoadTexture(Texture2d texture)
{
Image image;
Texture2D rayTexture;
string ext = "png";
byte[] extBytes = Encoding.ASCII.GetBytes(ext);
Image image = new Image();
// TODO: This can cause memory leaks.
unsafe
{
fixed (byte* textureData = texture.Data)
fixed (void* dataPtr = texture.Buffer)
{
fixed (byte* bExt = extBytes)
{
sbyte* sbExt = (sbyte*)*bExt;
image = Raylib.LoadImageFromMemory(sbExt, textureData, texture.Data.Length);
}
image.data = dataPtr;
}
}
image.width = texture.Width;
image.height = texture.Height;
image.mipmaps = 1;
image.format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
Texture2D rayTexture;
rayTexture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override int LoadTexture(string path)
{
Image image = Raylib.LoadImage(path);
var rayTexture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override void DrawTexture(int id, System.Drawing.Color tint)
{
Raylib.DrawTextureV(_texturePool[id], _position, SystemColorToRaylibColor(tint));

View File

@@ -22,12 +22,6 @@ namespace DaggerFramework.Rendering
public abstract void ClearBackground(Color color);
public abstract double GetFrameTime();
public abstract int LoadTexture(Texture2d texture);
/// <summary>
/// Temporary workaround for Raylib's LoadImageFromMemory.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public abstract int LoadTexture(string path);
public abstract void SetTransform(Vector2 position, float rotation = 0.0f);
public abstract void SetTransform(Matrix4x4 transform);
public abstract void DrawCircle(float radius, Color color);

9
Source/Resources/Font.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace DaggerFramework;
public class Font : Resource
{
public int Size = 16;
public Font(string path, byte[] buffer) : base(path, buffer)
{
}
}

View File

@@ -0,0 +1,9 @@
namespace DaggerFramework;
public class FontLoader : ResourceLoader<Font>
{
public override Font Load(string path)
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,8 @@
namespace DaggerFramework
{
public abstract class ResourceLoader<T> : IDisposable where T : Resource
{
public void Dispose() { }
public abstract T Load(string path);
}
}

View File

@@ -1,12 +1,12 @@
namespace DaggerFramework
{
public class SoundLoader : ResourceLoader
public class SoundLoader : ResourceLoader<Sound>
{
public override Resource Load(string path)
public override Sound Load(string path)
{
// TODO
// var data = File.ReadAllBytes(path);
var sound = new Sound(Array.Empty<byte>());
var sound = new Sound(path, new byte[] { });
sound.Path = path;
return sound;

View File

@@ -0,0 +1,22 @@
using StbImageSharp;
namespace DaggerFramework
{
public class Texture2dLoader : ResourceLoader<Texture2d>
{
public override Texture2d Load(string path)
{
ImageResult image;
using (var stream = File.OpenRead(path))
{
image = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
}
Texture2d result = new Texture2d(path, image.Data);
result.Width = image.Width;
result.Height = image.Height;
return result;
}
}
}

View File

@@ -2,9 +2,22 @@ namespace DaggerFramework
{
public abstract class Resource : IDisposable
{
public string Path { get; set; }
public string? Path { get => _path; set => _path = value; }
public byte[]? Buffer { get => _buffer; set => _buffer = value; }
public Resource(string path, byte[] buffer)
{
_path = path;
_buffer = buffer;
}
public void Dispose()
{
Buffer = null;
Path = null;
}
private string? _path;
private byte[]? _buffer;
}
}

View File

@@ -1,7 +0,0 @@
namespace DaggerFramework
{
public class ResourceLoader
{
public virtual Resource Load(string path) { return default; }
}
}

View File

@@ -1,87 +0,0 @@
using System.IO;
namespace DaggerFramework
{
public static class ResourceManager
{
public static string ResourceRoot { get; set; } = "Resources/";
public static T Load<T>(string relativePath) where T : Resource
{
var path = GlobalizePath(relativePath);
var loader = GetAssociatedResourceLoader<T>();
var res = loader.Load(path);
res.Path = path;
return res as T;
}
public static T Load<T>(string name, string relativePath) where T : Resource
{
T res = null;
if (TryGetResourceFromCache<T>(name, out res)) return res as T;
res = Load<T>(relativePath);
AddResourceToCache(name, res);
return res as T;
}
/// <summary>
/// Loads a list of resources from a folder. Their names will be derived from the actual file name, excluding the extension.
/// </summary>
/// <typeparam name="T">Resource type for all of the resources.</typeparam>
/// <param name="localPath">Path to the folder, relative to the ResourceRoot.</param>
/// <returns></returns>
public static void LoadFolder<T>(string relativePath) where T : Resource
{
var files = Directory.GetFiles(GlobalizePath(relativePath));
foreach (var file in files)
{
var resName = Path.GetFileNameWithoutExtension(file);
var resRelativePath = Path.GetRelativePath(ResourceRoot, file);
Load<T>(resName, resRelativePath);
}
}
public static T Get<T>(string name) where T : Resource
{
T res = null;
if (TryGetResourceFromCache<T>(name, out res)) return res as T;
return res;
}
public static string GlobalizePath(string relativePath)
{
return Path.Join(ResourceRoot, relativePath);
}
private static void AddResourceToCache(string name, Resource resource)
{
_resourceCache.Add(name, resource);
}
private static bool TryGetResourceFromCache<T>(string name, out T resource) where T : Resource
{
resource = null;
if (!_resourceCache.ContainsKey(name)) return false;
resource = _resourceCache[name] as T;
return true;
}
private static ResourceLoader GetAssociatedResourceLoader<T>() where T : Resource
{
return _resourceLoaderAssociations[typeof(T)];
}
private static KeyValuePair<Type, ResourceLoader> CreateResourceAssociation(Type type, ResourceLoader loader)
{
return new KeyValuePair<Type, ResourceLoader>(type, loader);
}
private static Dictionary<string, Resource> _resourceCache = new Dictionary<string, Resource>();
private static readonly Dictionary<Type, ResourceLoader> _resourceLoaderAssociations = new Dictionary<Type, ResourceLoader>()
{
{typeof(Texture2d), new Texture2dLoader()},
{typeof(Sound), new SoundLoader()}
};
}
}

View File

@@ -4,10 +4,9 @@ namespace DaggerFramework
{
public float PitchScale { get; set; } = 1.0f;
public float Volume { get; set; } = 1.0f;
public Sound(byte[] data)
public Sound(string path, byte[] buffer) : base(path, buffer)
{
Data = data;
}
public byte[] Data;
}
}

View File

@@ -2,10 +2,10 @@ namespace DaggerFramework
{
public class Texture2d : Resource
{
public Texture2d(byte[] data)
public int Width { get; set; }
public int Height { get; set; }
public Texture2d(string path, byte[] buffer) : base(path, buffer)
{
Data = data;
}
public byte[] Data;
}
}

View File

@@ -1,14 +0,0 @@
namespace DaggerFramework
{
public class Texture2dLoader : ResourceLoader
{
public override Resource Load(string path)
{
var data = File.ReadAllBytes(path);
var texture = new Texture2d(data);
texture.Path = path;
return texture;
}
}
}