Gondwana Game Engine Development (master) - 2.6.1
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
22 public static SvgResourceManager Instance => _instance.Value;
23
30 public SvgResource LoadFromFile(string key, string path)
31 {
32 ArgumentException.ThrowIfNullOrWhiteSpace(key);
33 ArgumentException.ThrowIfNullOrWhiteSpace(path);
34
35 var resource = SvgResource.Load(path);
36 return AddOrReplace(key, resource);
37 }
38
44 public List<SvgResource> LoadFromEngineAssetsFile(AssetsFile resourceFile)
45 {
46 ArgumentNullException.ThrowIfNull(resourceFile);
47 var loaded = new List<SvgResource>();
48
49 foreach (var entry in resourceFile.GetAllEntries())
50 {
51 if (entry.AssetType != AssetTypes.Svg || _svgResources.ContainsKey(entry.AssetName))
52 continue;
53
54 using var stream = resourceFile.Get(entry.AssetType, entry.AssetName);
55 if (stream is null)
56 {
57 Engine.Logger.LogWarning("Failed to retrieve stream for SVG resource: {Key}", entry.AssetName);
58 continue;
59 }
60
61 try
62 {
63 var svg = SvgResource.Load(stream);
64 loaded.Add(AddOrReplace(entry.AssetName, svg));
65 }
66 catch (Exception ex)
67 {
68 Engine.Logger.LogError(ex, "Error loading SVG resource from asset file for key: {Key}", entry.AssetName);
69 throw;
70 }
71 }
72
73 return loaded;
74 }
75
81 public bool Contains(string key) => _svgResources.ContainsKey(key);
82
88 public SvgResource? Get(string key) => _svgResources.TryGetValue(key, out var resource) ? resource : null;
89
94 public Dictionary<string, SvgResource> GetAll() => _svgResources.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
95
100 public void Unload(string key)
101 {
102 if (_svgResources.TryRemove(key, out var resource))
103 resource.Dispose();
104 }
105
109 public void Clear()
110 {
111 foreach (var resource in _svgResources.Values)
112 resource.Dispose();
113
114 _svgResources.Clear();
115 }
116
118 public void Dispose()
119 {
120 if (_disposed)
121 return;
122
123 Clear();
124 _disposed = true;
125 }
126
127 private SvgResource AddOrReplace(string key, SvgResource resource)
128 {
129 if (_svgResources.TryGetValue(key, out var existing))
130 existing.Dispose();
131
132 _svgResources[key] = resource;
133 return resource;
134 }
135}
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