From 604e34a12a6a9ccdd4e0098ab654834f382e4e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 9 Apr 2022 12:34:09 +0200 Subject: [PATCH] Start of ContentManager --- GlitchyEngine/src/Content/ContentManager.bf | 61 +++++++++++++++++++++ GlitchyEngine/src/Renderer/Texture.bf | 23 +++++--- 2 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 GlitchyEngine/src/Content/ContentManager.bf diff --git a/GlitchyEngine/src/Content/ContentManager.bf b/GlitchyEngine/src/Content/ContentManager.bf new file mode 100644 index 0000000..789abd7 --- /dev/null +++ b/GlitchyEngine/src/Content/ContentManager.bf @@ -0,0 +1,61 @@ +using System; +using System.IO; +using xxHash; + +namespace GlitchyEngine.Content +{ + class ContentId + { + private readonly String _string; + private readonly XXH64_hash _hash; + + public String String => _string; + public XXH64_hash Hash => _hash; + + [AllowAppend] + public this(StringView id) + { + String str = append String(id); + + _string = str; + + _hash = xxHash.ComputeHash(id); + } + } + + interface IContentManager + { + Stream GetFile(String filename); + } + + class ContentManager : IContentManager + { + private String _contentRoot; + + [AllowAppend] + public this(String contentRoot) + { + String cntRoot = append String(contentRoot); + _contentRoot = cntRoot; + + Runtime.Assert(Directory.Exists(contentRoot), "Content root directory doesn't exist."); + } + + public Stream GetFile(String filename) + { + String fullpath = scope .(_contentRoot.Length + 1 + filename.Length); + Path.InternalCombine(fullpath, _contentRoot, filename); + + FileStream stream = new FileStream(); + var result = stream.Open(fullpath, .Read, .Read); + + if (result case .Err(let error)) + { + Log.EngineLogger.Error($"Failed to open file \"{fullpath}\". Error: {error}"); + return null; + } + + return stream; + } + } +} \ No newline at end of file diff --git a/GlitchyEngine/src/Renderer/Texture.bf b/GlitchyEngine/src/Renderer/Texture.bf index 6e57c5b..1694b44 100644 --- a/GlitchyEngine/src/Renderer/Texture.bf +++ b/GlitchyEngine/src/Renderer/Texture.bf @@ -78,12 +78,12 @@ namespace GlitchyEngine.Renderer { Debug.Profiler.ProfileResourceFunction!(); - FileStream fs = new FileStream(); + Stream data = Application.Get().ContentManager.GetFile(_path); + defer delete data; - fs.Open(_path, .Read); - var readResult = fs.Read(); + var readResult = data.Read(); - delete fs; + data.Position = 0; char8[8] magicWord; @@ -93,7 +93,7 @@ namespace GlitchyEngine.Renderer if (strView.StartsWith(PngMagicWord)) { - LoadPng(); + LoadPng(data); } else if (strView.StartsWith(DdsMagicWord)) { @@ -106,14 +106,23 @@ namespace GlitchyEngine.Renderer } } - protected void LoadPng() + protected void LoadPng(Stream stream) { Debug.Profiler.ProfileResourceFunction!(); + + uint8[] pngData = new:ScopedAlloc! uint8[stream.Length]; + + var result = stream.TryRead(pngData); + + if (result case .Err(let err)) + { + Log.EngineLogger.Error($"Failed to read data from stream. Texture: \"{_path}\", Error: {err}"); + } uint8* rawData = ?; uint32 width = 0, height = 0; - uint32 errorCode = LodePng.LodePng.Decode32File(&rawData, &width, &height, _path.CStr()); + uint32 errorCode = LodePng.LodePng.Decode32(&rawData, &width, &height, pngData.Ptr, (.)pngData.Count); Debug.Assert(errorCode == 0, "Failed to load png File");