Refactor ResourceLoader, use only Texture2d in Renderer.
This commit is contained in:
@@ -8,6 +8,8 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="ImGui.NET" Version="1.89.4" />
|
<PackageReference Include="ImGui.NET" Version="1.89.4" />
|
||||||
<PackageReference Include="Raylib-cs" Version="4.2.0.1" />
|
<PackageReference Include="Raylib-cs" Version="4.2.0.1" />
|
||||||
<PackageReference Include="Silk.NET" Version="2.16.0" />
|
<PackageReference Include="Silk.NET" Version="2.17.0" />
|
||||||
|
<PackageReference Include="Silk.NET.WebGPU" Version="2.17.0" />
|
||||||
|
<PackageReference Include="StbImageSharp" Version="2.27.13" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -11,7 +11,7 @@ namespace DaggerFramework
|
|||||||
protected override void OnStart()
|
protected override void OnStart()
|
||||||
{
|
{
|
||||||
var renderer = Layer.Scene.Renderer;
|
var renderer = Layer.Scene.Renderer;
|
||||||
_texId = renderer.LoadTexture(Texture.Path);
|
_texId = renderer.LoadTexture(_texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnDraw(ref Renderer renderer)
|
public override void OnDraw(ref Renderer renderer)
|
||||||
|
|||||||
21
Source/Entities/Text2d.cs
Normal file
21
Source/Entities/Text2d.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ namespace DaggerFramework
|
|||||||
{
|
{
|
||||||
public abstract class Game
|
public abstract class Game
|
||||||
{
|
{
|
||||||
|
public abstract string ResourceRoot { get; }
|
||||||
public void Start()
|
public void Start()
|
||||||
{
|
{
|
||||||
LoadResources();
|
LoadResources();
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ using System.Drawing;
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
using Silk.NET.GLFW;
|
using Silk.NET.GLFW;
|
||||||
using Silk.NET.Maths;
|
|
||||||
using Silk.NET.OpenGL;
|
using Silk.NET.OpenGL;
|
||||||
|
|
||||||
namespace DaggerFramework.Rendering
|
namespace DaggerFramework.Rendering
|
||||||
@@ -71,11 +70,6 @@ namespace DaggerFramework.Rendering
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int LoadTexture(string path)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void SetTargetFps(int fps)
|
public override void SetTargetFps(int fps)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace DaggerFramework.Rendering
|
|||||||
|
|
||||||
public override void CreateWindow(string title, Vector2 size)
|
public override void CreateWindow(string title, Vector2 size)
|
||||||
{
|
{
|
||||||
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
|
// Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
|
||||||
_windowSize = size;
|
_windowSize = size;
|
||||||
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
|
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
|
||||||
}
|
}
|
||||||
@@ -77,41 +77,30 @@ namespace DaggerFramework.Rendering
|
|||||||
|
|
||||||
public override int LoadTexture(Texture2d texture)
|
public override int LoadTexture(Texture2d texture)
|
||||||
{
|
{
|
||||||
Image image;
|
Image image = new Image();
|
||||||
Texture2D rayTexture;
|
|
||||||
string ext = "png";
|
|
||||||
byte[] extBytes = Encoding.ASCII.GetBytes(ext);
|
|
||||||
|
|
||||||
// TODO: This can cause memory leaks.
|
|
||||||
unsafe
|
unsafe
|
||||||
{
|
{
|
||||||
fixed (byte* textureData = texture.Data)
|
fixed (void* dataPtr = texture.Buffer)
|
||||||
{
|
{
|
||||||
fixed (byte* bExt = extBytes)
|
image.data = dataPtr;
|
||||||
{
|
|
||||||
sbyte* sbExt = (sbyte*)*bExt;
|
|
||||||
image = Raylib.LoadImageFromMemory(sbExt, textureData, texture.Data.Length);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
image.width = texture.Width;
|
||||||
|
image.height = texture.Height;
|
||||||
|
image.mipmaps = 1;
|
||||||
|
image.format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||||
|
|
||||||
|
Texture2D rayTexture;
|
||||||
|
|
||||||
rayTexture = Raylib.LoadTextureFromImage(image);
|
rayTexture = Raylib.LoadTextureFromImage(image);
|
||||||
Raylib.UnloadImage(image);
|
|
||||||
|
|
||||||
_texturePool.Add(rayTexture);
|
_texturePool.Add(rayTexture);
|
||||||
|
|
||||||
return _texturePool.Count - 1;
|
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)
|
public override void DrawTexture(int id, System.Drawing.Color tint)
|
||||||
{
|
{
|
||||||
Raylib.DrawTextureV(_texturePool[id], _position, SystemColorToRaylibColor(tint));
|
Raylib.DrawTextureV(_texturePool[id], _position, SystemColorToRaylibColor(tint));
|
||||||
|
|||||||
@@ -22,12 +22,6 @@ namespace DaggerFramework.Rendering
|
|||||||
public abstract void ClearBackground(Color color);
|
public abstract void ClearBackground(Color color);
|
||||||
public abstract double GetFrameTime();
|
public abstract double GetFrameTime();
|
||||||
public abstract int LoadTexture(Texture2d texture);
|
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(Vector2 position, float rotation = 0.0f);
|
||||||
public abstract void SetTransform(Matrix4x4 transform);
|
public abstract void SetTransform(Matrix4x4 transform);
|
||||||
public abstract void DrawCircle(float radius, Color color);
|
public abstract void DrawCircle(float radius, Color color);
|
||||||
|
|||||||
9
Source/Resources/Font.cs
Normal file
9
Source/Resources/Font.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace DaggerFramework;
|
||||||
|
|
||||||
|
public class Font : Resource
|
||||||
|
{
|
||||||
|
public int Size = 16;
|
||||||
|
public Font(string path, byte[] buffer) : base(path, buffer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Source/Resources/Loaders/FontLoader.cs
Normal file
9
Source/Resources/Loaders/FontLoader.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace DaggerFramework;
|
||||||
|
|
||||||
|
public class FontLoader : ResourceLoader<Font>
|
||||||
|
{
|
||||||
|
public override Font Load(string path)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
Source/Resources/Loaders/ResourceLoader.cs
Executable file
8
Source/Resources/Loaders/ResourceLoader.cs
Executable 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
namespace DaggerFramework
|
namespace DaggerFramework
|
||||||
{
|
{
|
||||||
public class SoundLoader : ResourceLoader
|
public class SoundLoader : ResourceLoader<Sound>
|
||||||
{
|
{
|
||||||
public override Resource Load(string path)
|
public override Sound Load(string path)
|
||||||
{
|
{
|
||||||
// TODO
|
// TODO
|
||||||
// var data = File.ReadAllBytes(path);
|
// var data = File.ReadAllBytes(path);
|
||||||
var sound = new Sound(Array.Empty<byte>());
|
var sound = new Sound(path, new byte[] { });
|
||||||
sound.Path = path;
|
sound.Path = path;
|
||||||
|
|
||||||
return sound;
|
return sound;
|
||||||
22
Source/Resources/Loaders/Texture2dLoader.cs
Executable file
22
Source/Resources/Loaders/Texture2dLoader.cs
Executable 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,22 @@ namespace DaggerFramework
|
|||||||
{
|
{
|
||||||
public abstract class Resource : IDisposable
|
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()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
}
|
Buffer = null;
|
||||||
|
Path = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string? _path;
|
||||||
|
private byte[]? _buffer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace DaggerFramework
|
|
||||||
{
|
|
||||||
public class ResourceLoader
|
|
||||||
{
|
|
||||||
public virtual Resource Load(string path) { return default; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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()}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,10 +4,9 @@ namespace DaggerFramework
|
|||||||
{
|
{
|
||||||
public float PitchScale { get; set; } = 1.0f;
|
public float PitchScale { get; set; } = 1.0f;
|
||||||
public float Volume { 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,10 @@ namespace DaggerFramework
|
|||||||
{
|
{
|
||||||
public class Texture2d : Resource
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BIN
TestGame/Resources/icon.png
Executable file
BIN
TestGame/Resources/icon.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 3.2 KiB |
@@ -4,17 +4,31 @@ using DaggerFramework.Rendering;
|
|||||||
|
|
||||||
public class TestGame : Game
|
public class TestGame : Game
|
||||||
{
|
{
|
||||||
|
public override string ResourceRoot => "Resources/";
|
||||||
public override void Shutdown() => scene.Renderer.CloseWindow();
|
public override void Shutdown() => scene.Renderer.CloseWindow();
|
||||||
protected override void OnStart()
|
protected override void OnStart()
|
||||||
{
|
{
|
||||||
_renderer = new GlRenderer();
|
_renderer = new RaylibRenderer();
|
||||||
_inputHandler = new RaylibInputHandler();
|
_inputHandler = new RaylibInputHandler();
|
||||||
|
|
||||||
scene = new Scene(_renderer, _inputHandler, new DummyAudioBackend());
|
scene = new Scene(_renderer, _inputHandler, new DummyAudioBackend());
|
||||||
|
|
||||||
var mainGameLayer = new EntityLayer();
|
var mainGameLayer = new EntityLayer();
|
||||||
|
|
||||||
scene.AddLayer("World", mainGameLayer);
|
scene.AddLayer("World", mainGameLayer);
|
||||||
|
|
||||||
|
var text = new Text2d();
|
||||||
|
|
||||||
|
text.Contents = "Hello World!";
|
||||||
|
text.FontSize = 32;
|
||||||
|
|
||||||
|
var sprite = new Sprite2d();
|
||||||
|
|
||||||
|
sprite.Texture = _funnyTexture;
|
||||||
|
|
||||||
|
mainGameLayer.AddEntity(text);
|
||||||
|
mainGameLayer.AddEntity(sprite);
|
||||||
|
|
||||||
scene.Init();
|
scene.Init();
|
||||||
scene.Start();
|
scene.Start();
|
||||||
|
|
||||||
@@ -23,13 +37,16 @@ public class TestGame : Game
|
|||||||
|
|
||||||
protected override void LoadResources()
|
protected override void LoadResources()
|
||||||
{
|
{
|
||||||
|
_funnyTexture = _textureLoader.Load($"{ResourceRoot}icon.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void MainLoop()
|
protected override void MainLoop()
|
||||||
{
|
{
|
||||||
while (!scene.ShouldStop()) scene.Update();
|
while (!scene.ShouldStop()) scene.Update();
|
||||||
}
|
}
|
||||||
private GlRenderer _renderer;
|
private Renderer _renderer;
|
||||||
private RaylibInputHandler _inputHandler;
|
private RaylibInputHandler _inputHandler;
|
||||||
|
|
||||||
|
private Texture2dLoader _textureLoader = new();
|
||||||
|
private Texture2d? _funnyTexture;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user