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
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
26 {
27 lock (_syncRoot)
28 return _worldRects.Count > 0;
29 }
30 }
31
43 internal IReadOnlyList<Rectangle> WorldRects => _worldRects;
44
50 internal Rectangle[] SnapshotWorldRects()
51 {
52 lock (_syncRoot)
53 return _worldRects.ToArray();
54 }
55
62 internal void AddWorldRect(Rectangle worldPixelRange)
63 {
64 if (!_isEnabled() || worldPixelRange.IsEmpty)
65 return;
66
67 // ensure we're on the engine thread
68 var engine = Engine.Instance;
69
70 if (!engine.EngineDispatcher.IsOnEngineThread)
71 {
72 engine.EngineDispatcher.Post(() => AddWorldRect(worldPixelRange));
73 return;
74 }
75
76 lock (_syncRoot)
77 {
78 // Fast containment check: if any existing rect already fully contains this one, skip storing it.
79 for (int i = 0; i < _worldRects.Count; i++)
80 {
81 if (_worldRects[i].Contains(worldPixelRange))
82 return;
83 }
84
85 _worldRects.Add(worldPixelRange);
86 }
87 }
88
89 internal void AddViewScreenRect(View view, SceneLayer sceneLayer, Rectangle screenPixelRange)
90 {
91 if (!_isEnabled() || screenPixelRange.IsEmpty)
92 return;
93
94 if (view is null)
95 throw new ArgumentNullException(nameof(view));
96
97 if (sceneLayer is null)
98 throw new ArgumentNullException(nameof(sceneLayer));
99
100 // ensure we're on the engine thread
101 var engine = Engine.Instance;
102
103 if (!engine.EngineDispatcher.IsOnEngineThread)
104 {
105 engine.EngineDispatcher.Post(() => AddViewScreenRect(view, sceneLayer, screenPixelRange));
106 return;
107 }
108
109 // clamp screenPixelRange to view's viewport
110 screenPixelRange.Intersect(view.Viewport.TargetRectPx);
111
112 var worldRect = view.ScreenRectToWorldRect(sceneLayer, screenPixelRange);
113 worldRect.Inflate(3, 3); // Expand by 1 pixel in all directions to account for rounding errors.
114 AddWorldRect(worldRect.ToPixelAlignedRect());
115 }
116
117 internal void ClearRefreshQueue()
118 {
119 // ensure we're on the engine thread
120 var engine = Engine.Instance;
121
122 if (!engine.EngineDispatcher.IsOnEngineThread)
123 {
124 engine.EngineDispatcher.Post(() => ClearRefreshQueue());
125 return;
126 }
127
128 lock (_syncRoot)
129 _worldRects.Clear();
130 }
131}
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:395
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:40
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.