diff --git a/DaggerFramework.csproj b/DaggerFramework.csproj
index 1099bdc..8fe5972 100644
--- a/DaggerFramework.csproj
+++ b/DaggerFramework.csproj
@@ -8,6 +8,8 @@
-
+
+
+
\ No newline at end of file
diff --git a/Source/Entities/Sprite2d.cs b/Source/Entities/Sprite2d.cs
index 2d5d3e7..a728cf8 100755
--- a/Source/Entities/Sprite2d.cs
+++ b/Source/Entities/Sprite2d.cs
@@ -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)
diff --git a/Source/Entities/Text2d.cs b/Source/Entities/Text2d.cs
new file mode 100644
index 0000000..3f9cc3d
--- /dev/null
+++ b/Source/Entities/Text2d.cs
@@ -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;
+ }
+}
\ No newline at end of file
diff --git a/Source/Game.cs b/Source/Game.cs
index f95a3bc..4bc903e 100755
--- a/Source/Game.cs
+++ b/Source/Game.cs
@@ -2,6 +2,7 @@ namespace DaggerFramework
{
public abstract class Game
{
+ public abstract string ResourceRoot { get; }
public void Start()
{
LoadResources();
diff --git a/Source/Rendering/GlRenderer.cs b/Source/Rendering/GlRenderer.cs
index 54a8734..d4ecbeb 100644
--- a/Source/Rendering/GlRenderer.cs
+++ b/Source/Rendering/GlRenderer.cs
@@ -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;
diff --git a/Source/Rendering/RaylibRenderer.cs b/Source/Rendering/RaylibRenderer.cs
index d596eb4..4499687 100755
--- a/Source/Rendering/RaylibRenderer.cs
+++ b/Source/Rendering/RaylibRenderer.cs
@@ -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));
diff --git a/Source/Rendering/Renderer.cs b/Source/Rendering/Renderer.cs
index b1a1cf0..9798d61 100755
--- a/Source/Rendering/Renderer.cs
+++ b/Source/Rendering/Renderer.cs
@@ -22,12 +22,6 @@ namespace DaggerFramework.Rendering
public abstract void ClearBackground(Color color);
public abstract double GetFrameTime();
public abstract int LoadTexture(Texture2d texture);
- ///
- /// Temporary workaround for Raylib's LoadImageFromMemory.
- ///
- ///
- ///
- 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);
diff --git a/Source/Resources/Font.cs b/Source/Resources/Font.cs
new file mode 100644
index 0000000..0ffc1e7
--- /dev/null
+++ b/Source/Resources/Font.cs
@@ -0,0 +1,9 @@
+namespace DaggerFramework;
+
+public class Font : Resource
+{
+ public int Size = 16;
+ public Font(string path, byte[] buffer) : base(path, buffer)
+ {
+ }
+}
\ No newline at end of file
diff --git a/Source/Resources/Loaders/FontLoader.cs b/Source/Resources/Loaders/FontLoader.cs
new file mode 100644
index 0000000..cddaf54
--- /dev/null
+++ b/Source/Resources/Loaders/FontLoader.cs
@@ -0,0 +1,9 @@
+namespace DaggerFramework;
+
+public class FontLoader : ResourceLoader
+{
+ public override Font Load(string path)
+ {
+ throw new NotImplementedException();
+ }
+}
\ No newline at end of file
diff --git a/Source/Resources/Loaders/ResourceLoader.cs b/Source/Resources/Loaders/ResourceLoader.cs
new file mode 100755
index 0000000..5fef0d2
--- /dev/null
+++ b/Source/Resources/Loaders/ResourceLoader.cs
@@ -0,0 +1,8 @@
+namespace DaggerFramework
+{
+ public abstract class ResourceLoader : IDisposable where T : Resource
+ {
+ public void Dispose() { }
+ public abstract T Load(string path);
+ }
+}
\ No newline at end of file
diff --git a/Source/Resources/SoundLoader.cs b/Source/Resources/Loaders/SoundLoader.cs
similarity index 54%
rename from Source/Resources/SoundLoader.cs
rename to Source/Resources/Loaders/SoundLoader.cs
index 780f82e..f5d564b 100644
--- a/Source/Resources/SoundLoader.cs
+++ b/Source/Resources/Loaders/SoundLoader.cs
@@ -1,12 +1,12 @@
namespace DaggerFramework
{
- public class SoundLoader : ResourceLoader
+ public class SoundLoader : ResourceLoader
{
- public override Resource Load(string path)
+ public override Sound Load(string path)
{
// TODO
// var data = File.ReadAllBytes(path);
- var sound = new Sound(Array.Empty());
+ var sound = new Sound(path, new byte[] { });
sound.Path = path;
return sound;
diff --git a/Source/Resources/Loaders/Texture2dLoader.cs b/Source/Resources/Loaders/Texture2dLoader.cs
new file mode 100755
index 0000000..a8d5121
--- /dev/null
+++ b/Source/Resources/Loaders/Texture2dLoader.cs
@@ -0,0 +1,22 @@
+using StbImageSharp;
+
+namespace DaggerFramework
+{
+ public class Texture2dLoader : ResourceLoader
+ {
+ 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;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Source/Resources/Resource.cs b/Source/Resources/Resource.cs
index 03fddc0..427fda0 100755
--- a/Source/Resources/Resource.cs
+++ b/Source/Resources/Resource.cs
@@ -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;
}
}
\ No newline at end of file
diff --git a/Source/Resources/ResourceLoader.cs b/Source/Resources/ResourceLoader.cs
deleted file mode 100755
index bcc9ff1..0000000
--- a/Source/Resources/ResourceLoader.cs
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace DaggerFramework
-{
- public class ResourceLoader
- {
- public virtual Resource Load(string path) { return default; }
- }
-}
\ No newline at end of file
diff --git a/Source/Resources/ResourceManager.cs b/Source/Resources/ResourceManager.cs
deleted file mode 100755
index 617ed71..0000000
--- a/Source/Resources/ResourceManager.cs
+++ /dev/null
@@ -1,87 +0,0 @@
-using System.IO;
-
-namespace DaggerFramework
-{
- public static class ResourceManager
- {
- public static string ResourceRoot { get; set; } = "Resources/";
- public static T Load(string relativePath) where T : Resource
- {
- var path = GlobalizePath(relativePath);
- var loader = GetAssociatedResourceLoader();
- var res = loader.Load(path);
- res.Path = path;
- return res as T;
- }
-
- public static T Load(string name, string relativePath) where T : Resource
- {
- T res = null;
- if (TryGetResourceFromCache(name, out res)) return res as T;
-
- res = Load(relativePath);
- AddResourceToCache(name, res);
- return res as T;
- }
-
- ///
- /// Loads a list of resources from a folder. Their names will be derived from the actual file name, excluding the extension.
- ///
- /// Resource type for all of the resources.
- /// Path to the folder, relative to the ResourceRoot.
- ///
- public static void LoadFolder(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(resName, resRelativePath);
- }
- }
-
- public static T Get(string name) where T : Resource
- {
- T res = null;
- if (TryGetResourceFromCache(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(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() where T : Resource
- {
- return _resourceLoaderAssociations[typeof(T)];
- }
-
- private static KeyValuePair CreateResourceAssociation(Type type, ResourceLoader loader)
- {
- return new KeyValuePair(type, loader);
- }
-
- private static Dictionary _resourceCache = new Dictionary();
- private static readonly Dictionary _resourceLoaderAssociations = new Dictionary()
- {
- {typeof(Texture2d), new Texture2dLoader()},
- {typeof(Sound), new SoundLoader()}
- };
- }
-}
\ No newline at end of file
diff --git a/Source/Resources/Sound.cs b/Source/Resources/Sound.cs
index 580fe72..725f3bf 100644
--- a/Source/Resources/Sound.cs
+++ b/Source/Resources/Sound.cs
@@ -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;
}
}
\ No newline at end of file
diff --git a/Source/Resources/Texture2d.cs b/Source/Resources/Texture2d.cs
index 10997c9..3af1127 100755
--- a/Source/Resources/Texture2d.cs
+++ b/Source/Resources/Texture2d.cs
@@ -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;
}
}
\ No newline at end of file
diff --git a/Source/Resources/Texture2dLoader.cs b/Source/Resources/Texture2dLoader.cs
deleted file mode 100755
index 72cdf8f..0000000
--- a/Source/Resources/Texture2dLoader.cs
+++ /dev/null
@@ -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;
- }
- }
-}
\ No newline at end of file
diff --git a/TestGame/Resources/icon.png b/TestGame/Resources/icon.png
new file mode 100755
index 0000000..c98fbb6
Binary files /dev/null and b/TestGame/Resources/icon.png differ
diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs
index 8e46f37..022f343 100644
--- a/TestGame/TestGame.cs
+++ b/TestGame/TestGame.cs
@@ -4,17 +4,31 @@ using DaggerFramework.Rendering;
public class TestGame : Game
{
+ public override string ResourceRoot => "Resources/";
public override void Shutdown() => scene.Renderer.CloseWindow();
protected override void OnStart()
{
- _renderer = new GlRenderer();
+ _renderer = new RaylibRenderer();
_inputHandler = new RaylibInputHandler();
scene = new Scene(_renderer, _inputHandler, new DummyAudioBackend());
var mainGameLayer = new EntityLayer();
+
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.Start();
@@ -23,13 +37,16 @@ public class TestGame : Game
protected override void LoadResources()
{
-
+ _funnyTexture = _textureLoader.Load($"{ResourceRoot}icon.png");
}
protected override void MainLoop()
{
while (!scene.ShouldStop()) scene.Update();
}
- private GlRenderer _renderer;
+ private Renderer _renderer;
private RaylibInputHandler _inputHandler;
+
+ private Texture2dLoader _textureLoader = new();
+ private Texture2d? _funnyTexture;
}
\ No newline at end of file