diff --git a/Client/Rendering/Texture/AtlasLoader.cs b/Client/Rendering/Texture/AtlasLoader.cs index dc70996..d9d082d 100644 --- a/Client/Rendering/Texture/AtlasLoader.cs +++ b/Client/Rendering/Texture/AtlasLoader.cs @@ -9,12 +9,13 @@ namespace Voxel.Client.Rendering.Texture; public class AtlasLoader { - private static JsonSerializer Serializer = new JsonSerializer(); - + private static JsonSerializer Serializer = new(); + public static void LoadAtlas(AssetReader reader, Atlas target, RenderSystem renderSystem) { - - reader.LoadAll(s => s.StartsWith(Path.Combine("textures", "atlases", target.Name.ToLower())) && s.EndsWith(".json"), (path, stream, length) => { - + bool FilenameMatches(string s) + => s.StartsWith(Path.Combine("textures", "atlases", target.Name.ToLower())) && s.EndsWith(".json"); + + foreach ((string path, var stream, int length) in reader.LoadAll(FilenameMatches)) { using var sr = new StreamReader(stream); using var jsonTextReader = new JsonTextReader(sr); @@ -40,7 +41,7 @@ public static void LoadAtlas(AssetReader reader, Atlas target, RenderSystem rend if (jsonObject.Explicit != null && jsonObject.Explicit != null) foreach (var entry in jsonObject.Explicit) target.StitchTexture($"{target.Name.ToLower()}/{entry.Name}", texture, set, new ivec2(entry.X, entry.Y), new ivec2(entry.Width, entry.Height)); - }); + } target.GenerateMipmaps(); renderSystem.MainCommandList.SetFramebuffer(renderSystem.GraphicsDevice.SwapchainFramebuffer); diff --git a/Common/Util/Iteration.cs b/Common/Util/Iteration.cs index 424bc9e..e645baa 100644 --- a/Common/Util/Iteration.cs +++ b/Common/Util/Iteration.cs @@ -5,36 +5,16 @@ namespace Voxel.Common.Util; public static class Iteration { - - public static IEnumerable Cubic(ivec3 min, ivec3 max) => new PositionIteratorVec3 { Min = min, Max = max }; - public static IEnumerable Cubic(int min, int max) => new PositionIteratorInt { Min = min, Max = max }; - - public static IEnumerable Cubic(int max) => Cubic(0, max); - - private class PositionIteratorVec3 : IEnumerable { - public ivec3 Min; - public ivec3 Max; - - public IEnumerator GetEnumerator() { - for (var x = Min.x; x < Max.x; x++) - for (var y = Min.y; y < Max.y; y++) - for (var z = Min.z; z < Max.z; z++) + public static IEnumerable Cubic(ivec3 min, ivec3 max) { + for (int x = min.x; x < max.x; x++) + for (int y = min.y; y < max.y; y++) + for (int z = min.z; z < max.z; z++) yield return new(x, y, z); - } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } - private class PositionIteratorInt : IEnumerable { + public static IEnumerable Cubic(int min, int max) + => Cubic(new ivec3(min, min, min), new ivec3(max, max, max)); - public int Min; - public int Max; - - public IEnumerator GetEnumerator() { - for (var x = Min; x < Max; x++) - for (var y = Min; y < Max; y++) - for (var z = Min; z < Max; z++) - yield return new(x, y, z); - } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - } + public static IEnumerable Cubic(int max) + => Cubic(0, max); } diff --git a/Core/Assets/AssetReader.cs b/Core/Assets/AssetReader.cs index 1ef6f61..ef4a97f 100644 --- a/Core/Assets/AssetReader.cs +++ b/Core/Assets/AssetReader.cs @@ -3,7 +3,7 @@ namespace RenderSurface.Assets; -public class AssetReader : IDisposable { +public sealed class AssetReader : IDisposable { public delegate bool ConditionDelegate(string path); public delegate void LoadDelegate(string path, Stream stream, int length); @@ -25,16 +25,21 @@ public bool TryGetStream(string path, [NotNullWhen(true)] out Stream? assetStrea return true; } - public void LoadAll(ConditionDelegate condition, LoadDelegate loader) { + public IEnumerable<(string path, Stream stream, int length)> LoadAll(ConditionDelegate condition) { foreach (var entry in File.Entries) { if (!condition(entry.FullName)) continue; using var str = entry.Open(); - loader(entry.FullName, str, (int)entry.Length); + yield return (entry.FullName, str, (int)entry.Length); } } + public void LoadAll(ConditionDelegate condition, LoadDelegate loader) { + foreach ((string path, var stream, int length) in LoadAll(condition)) + loader(path, stream, length); + } + public void Dispose() { File.Dispose(); } diff --git a/Core/Game.cs b/Core/Game.cs index 9d54c54..e913804 100644 --- a/Core/Game.cs +++ b/Core/Game.cs @@ -1,5 +1,4 @@ -using ImGuiNET; -using RenderSurface.Assets; +using RenderSurface.Assets; using RenderSurface.Input; using RenderSurface.Rendering; using Veldrid; @@ -45,7 +44,7 @@ public void Run(int tps = 20, string windowTitle = "Game") { isOpen = true; - AssetReader = new AssetReader("Content.zip"); + AssetReader = new("Content.zip"); ImGuiRenderer = new(gd, gd.SwapchainFramebuffer.OutputDescription, NativeWindow.Width, NativeWindow.Height); RenderSystem = new(this, AssetReader);