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

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