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
RefreshQueue.cs
1using System.Drawing;
5
6namespace Gondwana.Rendering;
7
8internal sealed class RefreshQueue
9{
10 private readonly Func<bool> _isEnabled;
11 private readonly List<Rectangle> _worldRects; // World-space dirty regions (pixels)
12 private readonly object _syncRoot = new(); // Guards _worldRects for cross-thread access
13
14 internal RefreshQueue(Func<bool> isEnabled)
15 {
16 _isEnabled = isEnabled ?? throw new ArgumentNullException(nameof(isEnabled));
17 _worldRects = new List<Rectangle>(64);
18 }
19
23 internal bool IsDirty
24 {
25 get { lock (_syncRoot) return _worldRects.Count > 0; }
26 }
27
39 internal IReadOnlyList<Rectangle> WorldRects => _worldRects;
40
46 internal Rectangle[] SnapshotWorldRects()
47 {
48 lock (_syncRoot) return _worldRects.ToArray();
49 }
50
57 internal void AddWorldRect(Rectangle worldPixelRange)
58 {
59 if (!_isEnabled() || worldPixelRange.IsEmpty)
60 return;
61
62 // ensure we're on the engine thread
63 var engine = Engine.Instance;
64 if (!engine.EngineDispatcher.IsOnEngineThread)
65 {
66 engine.EngineDispatcher.Post(() => AddWorldRect(worldPixelRange));
67 return;
68 }
69
70 lock (_syncRoot)
71 {
72 // Fast containment check: if any existing rect already fully contains this one, skip storing it.
73 for (int i = 0; i < _worldRects.Count; i++)
74 {
75 if (_worldRects[i].Contains(worldPixelRange))
76 return;
77 }
78
79 _worldRects.Add(worldPixelRange);
80 }
81 }
82
83 internal void AddViewScreenRect(View view, SceneLayer sceneLayer, Rectangle screenPixelRange)
84 {
85 if (!_isEnabled() || screenPixelRange.IsEmpty)
86 return;
87
88 if (view is null)
89 throw new ArgumentNullException(nameof(view));
90
91 if (sceneLayer is null)
92 throw new ArgumentNullException(nameof(sceneLayer));
93
94 // ensure we're on the engine thread
95 var engine = Engine.Instance;
96 if (!engine.EngineDispatcher.IsOnEngineThread)
97 {
98 engine.EngineDispatcher.Post(() => AddViewScreenRect(view, sceneLayer, screenPixelRange));
99 return;
100 }
101
102 // clamp screenPixelRange to view's viewport
103 screenPixelRange.Intersect(view.Viewport.TargetRectPx);
104
105 var worldRect = view.ScreenRectToWorldRect(sceneLayer, screenPixelRange);
106 worldRect.Inflate(3, 3); // Expand by 1 pixel in all directions to account for rounding errors.
107 AddWorldRect(worldRect.ToPixelAlignedRect());
108 }
109
110 internal void ClearRefreshQueue()
111 {
112 // ensure we're on the engine thread
113 var engine = Engine.Instance;
114 if (!engine.EngineDispatcher.IsOnEngineThread)
115 {
116 engine.EngineDispatcher.Post(() => ClearRefreshQueue());
117 return;
118 }
119
120 lock (_syncRoot)
121 _worldRects.Clear();
122 }
123}
RectangleF ScreenRectToWorldRect(SceneLayer layer, RectangleF screenRect)
Converts a screen-space rectangle (on the logical Backbuffer) into a world-space rectangle for the gi...
Definition View.cs:371
Viewport Viewport
Gets the viewport that defines the screen-space rectangle, zoom level, and other rendering parameters...
Definition View.cs:44
Rectangle TargetRectPx
Screen-space rectangle (in SCREEN / RenderSurface pixels) where this view is drawn....
Definition Viewport.cs:35
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.