From 06da2c3f7ff54ea7f1e3b256341120101adfb7d0 Mon Sep 17 00:00:00 2001 From: dnesov Date: Wed, 14 Jun 2023 23:48:26 +0200 Subject: [PATCH] Refactor ResourceLoader, use only Texture2d in Renderer. --- DaggerFramework.csproj | 4 +- Source/Entities/Sprite2d.cs | 2 +- Source/Entities/Text2d.cs | 21 +++++ Source/Game.cs | 1 + Source/Rendering/GlRenderer.cs | 6 -- Source/Rendering/RaylibRenderer.cs | 35 +++---- Source/Rendering/Renderer.cs | 6 -- Source/Resources/Font.cs | 9 ++ Source/Resources/Loaders/FontLoader.cs | 9 ++ Source/Resources/Loaders/ResourceLoader.cs | 8 ++ Source/Resources/{ => Loaders}/SoundLoader.cs | 6 +- Source/Resources/Loaders/Texture2dLoader.cs | 22 +++++ Source/Resources/Resource.cs | 15 ++- Source/Resources/ResourceLoader.cs | 7 -- Source/Resources/ResourceManager.cs | 87 ------------------ Source/Resources/Sound.cs | 5 +- Source/Resources/Texture2d.cs | 6 +- Source/Resources/Texture2dLoader.cs | 14 --- TestGame/Resources/icon.png | Bin 0 -> 3305 bytes TestGame/TestGame.cs | 23 ++++- 20 files changed, 128 insertions(+), 158 deletions(-) create mode 100644 Source/Entities/Text2d.cs create mode 100644 Source/Resources/Font.cs create mode 100644 Source/Resources/Loaders/FontLoader.cs create mode 100755 Source/Resources/Loaders/ResourceLoader.cs rename Source/Resources/{ => Loaders}/SoundLoader.cs (54%) create mode 100755 Source/Resources/Loaders/Texture2dLoader.cs delete mode 100755 Source/Resources/ResourceLoader.cs delete mode 100755 Source/Resources/ResourceManager.cs delete mode 100755 Source/Resources/Texture2dLoader.cs create mode 100755 TestGame/Resources/icon.png 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 0000000000000000000000000000000000000000..c98fbb601c83c81ec8c22b1dba7d1d57c62b323c GIT binary patch literal 3305 zcmVNc=P)Px>qe(&U$es`gSqKCHF-lq>v1vga#%UF>TTrLR zW%{UNJKZi|Pj@Rc9GyPBD1CamMMf6SL~V^ag9~Vzut^L^0!Tv0LK0FTdnJ`x->EF(MZIP5kY*1-@^egP~7mH>({qi7{6 zQF;bN-XMq~+RzA8lI9AtJuz@PY*+{SP-Gbd@mZ(r*eE&`XO5!C>w#-pcmS28K^qzY zfTGCjor*I@ltgKb03nh#Fh$KpDL=o}gj-g4v6{}ZR1*mvXv?|gEA&Yr#r;Zw*d zUabIx8iHf+WoIO_c11Ba&!34XihSMF&C#YFDjU0)mmbXz3ex!D&t9UYp>;&R%(O(_ z*z^;&A84SWzKiQpqsdQ+Vs?rFS(f?R;c8xg_ft;Roec_~1KsVww}wzq5D}*5x6k|& zf~2A3@L4|ix|Q=L>rnmKE;B3UB=OMQxAK$Ce;LvDp?hwn-{Rn}Uo~U4IXTs4V%MQY zCWULcZFU0R%gbU;_Ef(A#76r1%|YWis0t`9$R{cyjFnsV(POrI)SGQi-l{mu{e?5R zepcp?AQ54D3g_mswd@RLn{z~;^Cl}>%j@}TWixL+audY``MmSV{-E(3R0Ws^U9%mk zmAond;N8k*{(f!}e^~d(i1Hq@jdv@XN2ML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9LHOFIo{G2 zG}l$9R*{<+L2hCsOJ~Lt6Q-rRub*8X{*4{)e}>%=_&DxOFeq1LRia4Yyj*Tyynw>F zxkKf(MiaG0*L|V-^Zhtvg-(-|F0&1rU8bqab*n5TT8~C860O$|6Rt%P1=1(EjIQZ% z;Y^PU2VC*~^2!sG?mbBPS0~0yd-+086)+rHjhfk6>CB$t`o%;=kdYF9NwiKkwbIpN z;_FlOuHQHHSZ&@fUuSI-S*t`DjsiIB z{=1M@JKVC$a8z{2;xCPfRb{~T>uo#5rL4L+z9n`rSUt3Tt nAZ`TZm+q1gPVN84&*%Ra7her>#-hHS00000NkvXXu0mjf|6N@O literal 0 HcmV?d00001 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