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
DirectDrawingManager.cs
1using System.Collections.Concurrent;
2using System.Collections.ObjectModel;
3using System.Drawing;
6
8
41public sealed class DirectDrawingManager : IDisposable
42{
43 // ---- Singleton ----
44 private static readonly Lazy<DirectDrawingManager> _instance =
45 new(() => new DirectDrawingManager());
46
51 public static DirectDrawingManager Instance => _instance.Value;
52
53 // ---- Storage (instance-level) ----
54 private readonly ConcurrentDictionary<string, IDirectDrawable> _directDrawings =
55 new(StringComparer.Ordinal);
56
57 private DirectDrawingManager() { }
58
77 public ReadOnlyCollection<IDirectDrawable> DirectDrawings =>
78 new ReadOnlyCollection<IDirectDrawable>([.. _directDrawings.Values]);
79
91 public int Count => _directDrawings.Count;
92
113 {
114 return name is null ? null : _directDrawings.TryGetValue(name, out IDirectDrawable? d) ? d : null;
115 }
116
135 public void ClearAll()
136 {
137 var toDispose = _directDrawings.Values.ToArray();
138
139 foreach (var d in toDispose)
140 d.Dispose(); // removal happens via event handler
141 }
142
158 public void Clear(string name)
159 {
160 if (string.IsNullOrEmpty(name))
161 return;
162
163 if (_directDrawings.TryGetValue(name, out var d))
164 d.Dispose();
165 }
166
185 internal void UpdateAll(long tick)
186 {
187 // Snapshot to avoid races while iterating (consistent with RenderAll).
188 var snapshot = _directDrawings.Values.ToArray();
189
190 foreach (var drawing in snapshot)
191 drawing.Update(tick);
192 }
193
215 internal void AddOrReplace(IDirectDrawable drawing)
216 {
217 if (drawing is null)
218 throw new ArgumentNullException(nameof(drawing));
219
220 var name = drawing.Nickname!.ToString();
221
222 _directDrawings.AddOrUpdate(
223 name,
224 // Add case
225 key =>
226 {
227 drawing.Disposing += OnDrawingDisposing;
228 return drawing;
229 },
230 // Update case
231 (key, existing) =>
232 {
233 // Unsubscribe old instance
234 existing.Disposing -= OnDrawingDisposing;
235
236 // Dispose it
237 existing.Dispose();
238
239 // Wire up new one
240 drawing.Disposing += OnDrawingDisposing;
241 return drawing;
242 });
243 }
244
245 private static readonly IComparer<DirectDrawingBase> _defaultComparer =
246 Comparer<DirectDrawingBase>.Create(
247 (Comparison<DirectDrawingBase>)((a, b) =>
248 {
249 if (a == null && b == null)
250 return 0;
251
252 if (a == null)
253 return -1;
254
255 if (b == null)
256 return 1;
257
258 // First compare ZOrder
259 int z = a.ZOrder.CompareTo(b.ZOrder);
260
261 if (z != 0)
262 return z;
263
264 // If ZOrder equal, fall back to Name
265 return string.Compare(a.Nickname, b.Nickname, StringComparison.Ordinal);
266 }));
267
268 private void OnDrawingDisposing(object? sender, IDirectDrawable drawing)
269 {
270 _directDrawings.TryRemove(drawing.Nickname!, out _);
271 drawing.Disposing -= OnDrawingDisposing;
272 }
273
274 #region helper methods
275
276 internal bool HasVisibleDrawingForLayer(
277 SceneLayer layer,
278 Rectangle worldRect)
279 {
280 foreach (var value in _directDrawings.Values)
281 {
282 if (value is DirectDrawingBase drawing &&
283 drawing.Visible &&
284 drawing.Mode == DirectDrawingMode.SceneLayer &&
285 ReferenceEquals(drawing.SceneLayer, layer) &&
286 drawing.WorldBounds.IntersectsWith(worldRect))
287 {
288 return true;
289 }
290 }
291
292 return false;
293 }
294
320 public IReadOnlyList<DirectDrawingBase> GetDrawingsForLayer(SceneLayer layer)
321 {
322 if (layer is null)
323 return Array.Empty<DirectDrawingBase>();
324
325 // Snapshot to avoid races while iterating (consistent with UpdateAll / Render patterns).
326 var snapshot = _directDrawings.Values.ToArray();
327
328 var result = snapshot
329 .OfType<DirectDrawingBase>()
330 .Where(d =>
331 d.Mode == DirectDrawingMode.SceneLayer &&
332 ReferenceEquals(d.SceneLayer, layer))
333 .ToList();
334
335 result.Sort(_defaultComparer);
336 return result;
337 }
338
365 public IReadOnlyList<DirectDrawingBase> GetDrawingsForView(View view)
366 {
367 if (view is null)
368 return Array.Empty<DirectDrawingBase>();
369
370 var snapshot = _directDrawings.Values.ToArray();
371
372 var result = snapshot
373 .OfType<DirectDrawingBase>()
374 .Where(d =>
375 d.Mode == DirectDrawingMode.View &&
376 ReferenceEquals(d.View, view))
377 .ToList();
378
379 result.Sort(_defaultComparer);
380 return result;
381 }
382
386 public void Dispose()
387 {
388 ClearAll();
389 }
390
391 #endregion helper methods
392}
Provides the base implementation for direct drawing objects that can be rendered to the backbuffer wi...
int Count
Gets the current number of registered direct drawings.
static DirectDrawingManager Instance
Gets the singleton instance of the direct drawing manager.
void Clear(string name)
Disposes and removes a specific direct drawing by its nickname.
IReadOnlyList< DirectDrawingBase > GetDrawingsForView(View view)
Returns all direct drawings associated with a specific view, ordered by Z-order then nickname.
IReadOnlyList< DirectDrawingBase > GetDrawingsForLayer(SceneLayer layer)
Returns all direct drawings associated with a specific scene layer, ordered by Z-order then nickname.
IDirectDrawable? GetDirectDrawing(string name)
Retrieves a direct drawing by its nickname.
ReadOnlyCollection< IDirectDrawable > DirectDrawings
Gets a read-only snapshot of all currently registered direct drawings.
void ClearAll()
Disposes and removes all registered direct drawings.
void Dispose()
Releases all registered direct drawings managed by this instance.
Represents any drawable object (movable or static) that can render directly to a RenderSurfaceHost,...
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.