Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Clean up iterator in Iteration.Cubic and use an iterator in AssetRead…
Browse files Browse the repository at this point in the history
…er.LoadAll
  • Loading branch information
Oliver-makes-code committed Oct 12, 2023
1 parent 37e1ddc commit df5c4ff
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
13 changes: 7 additions & 6 deletions Client/Rendering/Texture/AtlasLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
36 changes: 8 additions & 28 deletions Common/Util/Iteration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,16 @@
namespace Voxel.Common.Util;

public static class Iteration {

public static IEnumerable<ivec3> Cubic(ivec3 min, ivec3 max) => new PositionIteratorVec3 { Min = min, Max = max };
public static IEnumerable<ivec3> Cubic(int min, int max) => new PositionIteratorInt { Min = min, Max = max };

public static IEnumerable<ivec3> Cubic(int max) => Cubic(0, max);

private class PositionIteratorVec3 : IEnumerable<ivec3> {
public ivec3 Min;
public ivec3 Max;

public IEnumerator<ivec3> 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<ivec3> 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<ivec3> {
public static IEnumerable<ivec3> Cubic(int min, int max)
=> Cubic(new ivec3(min, min, min), new ivec3(max, max, max));

public int Min;
public int Max;

public IEnumerator<ivec3> 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<ivec3> Cubic(int max)
=> Cubic(0, max);
}
11 changes: 8 additions & 3 deletions Core/Assets/AssetReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
}
Expand Down
5 changes: 2 additions & 3 deletions Core/Game.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ImGuiNET;
using RenderSurface.Assets;
using RenderSurface.Assets;
using RenderSurface.Input;
using RenderSurface.Rendering;
using Veldrid;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit df5c4ff

Please sign in to comment.