Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
SvgResourceManager.cs
1using System.Collections.Concurrent;
3using Microsoft.Extensions.Logging;
4
5namespace Gondwana.Drawing;
6
10public sealed class SvgResourceManager : IDisposable
11{
12 private static readonly Lazy<SvgResourceManager> _instance = new(() => new SvgResourceManager());
13 private readonly ConcurrentDictionary<string, SvgResource> _svgResources = new();
14 private bool _disposed;
15
16 private SvgResourceManager()
17 {
18 }
19
23 public static SvgResourceManager Instance => _instance.Value;
24
31 public SvgResource LoadFromFile(string key, string path)
32 {
33 ArgumentException.ThrowIfNullOrWhiteSpace(key);
34 ArgumentException.ThrowIfNullOrWhiteSpace(path);
35
36 var resource = SvgResource.Load(path);
37 return AddOrReplace(key, resource);
38 }
39
45 public List<SvgResource> LoadFromEngineAssetsFile(AssetsFile resourceFile)
46 {
47 ArgumentNullException.ThrowIfNull(resourceFile);
48 var loaded = new List<SvgResource>();
49
50 foreach (var entry in resourceFile.GetAllEntries())
51 {
52 if (entry.AssetType != AssetTypes.Svg || _svgResources.ContainsKey(entry.AssetName))
53 continue;
54
55 using var stream = resourceFile.Get(entry.AssetType, entry.AssetName);
56
57 if (stream is null)
58 {
59 Engine.Logger.LogWarning("Failed to retrieve stream for SVG resource: {Key}", entry.AssetName);
60 continue;
61 }
62
63 try
64 {
65 var svg = SvgResource.Load(stream);
66 loaded.Add(AddOrReplace(entry.AssetName, svg));
67 }
68 catch (Exception ex)
69 {
70 Engine.Logger.LogError(
71 ex,
72 "Error loading SVG resource from asset file for key: {Key}",
73 entry.AssetName);
74
75 throw;
76 }
77 }
78
79 return loaded;
80 }
81
87 public bool Contains(string key) => _svgResources.ContainsKey(key);
88
94 public SvgResource? Get(string key) => _svgResources.TryGetValue(key, out var resource) ? resource : null;
95
100 public Dictionary<string, SvgResource> GetAll() => _svgResources.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
101
106 public void Unload(string key)
107 {
108 if (_svgResources.TryRemove(key, out var resource))
109 resource.Dispose();
110 }
111
115 public void Clear()
116 {
117 foreach (var resource in _svgResources.Values)
118 resource.Dispose();
119
120 _svgResources.Clear();
121 }
122
124 public void Dispose()
125 {
126 if (_disposed)
127 return;
128
129 Clear();
130 _disposed = true;
131 }
132
133 private SvgResource AddOrReplace(string key, SvgResource resource)
134 {
135 if (_svgResources.TryGetValue(key, out var existing))
136 existing.Dispose();
137
138 _svgResources[key] = resource;
139 return resource;
140 }
141}
Represents an asset file used by the engine, providing functionality to load, manage,...
Definition AssetsFile.cs:18
static SvgResourceManager Instance
Gets the singleton instance of the SvgResourceManager.
void Unload(string key)
Unloads a resource by key.
List< SvgResource > LoadFromEngineAssetsFile(AssetsFile resourceFile)
Loads all SVG resources from an AssetsFile.
SvgResource? Get(string key)
Gets a resource by key or null when not found.
bool Contains(string key)
Determines whether a resource key is loaded.
Dictionary< string, SvgResource > GetAll()
Gets all loaded resources.
SvgResource LoadFromFile(string key, string path)
Loads an SVG resource from a file path.
void Clear()
Clears all loaded resources.
Represents a loaded SVG asset that can be rasterized into cached bitmaps at requested sizes.
static SvgResource Load(string path)
Loads an SVG resource from a file path.
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static ILogger< Engine > Logger
Gets the logger instance used by the engine for diagnostic and informational messages.
Definition Engine.cs:59
AssetTypes
Defines the types of assets that can be stored and managed by the engine.
Definition AssetTypes.cs:8