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
RenderSurfaceHost.Bitmap.cs
1using System.Drawing;
7
8namespace Gondwana.Rendering;
9
10public sealed partial class RenderSurfaceHost<TBackbuffer>
11 where TBackbuffer : BackbufferBase
12{
48 private void RenderToBackbufferBitmap(long tick)
49 {
50 if (ViewManager.Views.Count == 0)
51 {
52 Backbuffer.ClearRect(
53 new Rectangle(
54 0,
55 0,
56 Backbuffer.Width,
57 Backbuffer.Height));
58
60 return;
61 }
62
63 bool fullViewComposition = ViewManager.Views.Any(view => view.HasPresentationEffect)
64 || Scene.VisibleSceneLayers.Any(layer =>
65 layer.WrapHorizontally || layer.WrapVertically);
66
67 if (!PrepareBitmapFrame(fullViewComposition))
68 return;
69
70 if (fullViewComposition)
71 Backbuffer.ClearRect(
72 new Rectangle(
73 0,
74 0,
75 Backbuffer.Width,
76 Backbuffer.Height));
77
78 // Render all views to Backbuffer. Draw layers back -> front (ascending Z).
79 foreach (var view in ViewManager.Views)
80 RenderBitmapView(view, tick, fullViewComposition);
81
82 // 3) Clear layer queues now that we've consumed them (avoids re-drawing same tiles next frame)
83 for (int i = 0; i < Scene.CountOfVisibleLayers; i++)
84 Scene.VisibleSceneLayers[i].RefreshQueue.ClearRefreshQueue();
85
87
88 // Notify subscribers that all scene content has been drawn and the canvas is ready for
89 // post-scene effects. Fires before RenderBackbufferEnd so subscribers still have a
90 // chance to draw into the canvas before it is presented to the adapter.
91 InvokePostSceneCanvasHooks();
92 }
93
94 private bool PrepareBitmapFrame(bool fullViewComposition)
95 {
96 // 0) If there are no visible SceneLayers, just clear and publish the full frame.
97 if (Scene.CountOfVisibleLayers == 0)
98 {
99 Backbuffer.ClearRect(
100 new Rectangle(
101 0,
102 0,
103 Backbuffer.Width,
104 Backbuffer.Height));
105
107 }
108 else
109 {
110 // 1) Handle full scene refresh once (camera moved, zoom changed, etc.): clear and mark all layers as dirty.
111 // This already clears the whole backbuffer and enqueues a full rect per layer.
112 if (Scene.FullRefreshNeeded || (fullViewComposition && Scene.IsDirty))
113 {
114 // this will mark the Scene.IsDirty flag as true
115 EnqueueFullSceneRefresh();
116 }
117 else
118 {
119 // scene is not dirty, this frame is done...
120 if (!Scene.IsDirty)
121 {
122 RenderBackbufferNoOp?.Invoke();
123 return false;
124 }
125 }
126 }
127
128 return true;
129 }
130
131 private void RenderBitmapView(View view, long tick, bool fullViewComposition)
132 {
133 RenderContext.Push(view, tick);
134
135 try
136 {
137 // 2.1) Force-refresh all DirectDrawings that overlay this view
139
140 foreach (var overlay in overlays)
141 {
142 overlay.ForceRefresh();
143 }
144
145 // 2.2) identify dirty SCREEN areas across all layers for this view
146 var dirtyScreenRects = CollectDirtyScreenArea(view);
147
148 // 2.3) Clip to this view's viewport
149 var vp = view.GetRenderViewportTargetRectPx();
150
151 BeginViewportClip(view, vp, Backbuffer);
152
153 int viewPresentation = BeginPresentation(
154 view.GetPresentationBoundsPx(),
155 view.EffectOpacity,
156 view.EffectReveal,
157 view.EffectRevealDirection);
158
159 if (viewPresentation > 0)
160 {
161 if (fullViewComposition)
162 {
163 Backbuffer.ClearRect(view.GetPresentationBoundsPx().ToPixelAlignedRect());
164 }
165 else
166 {
167 // 2.4) Pre-clear dirty areas on backbuffer to Backbuffer.ClearColor
168 PreclearScreenAreas(view, dirtyScreenRects);
169 }
170
171 RenderBitmapLayers(view);
172
173 // 2.6) draw all View-based DirectDrawings for this view
174 for (int i = 0; i < overlays.Count; i++)
175 overlays[i].Draw(Backbuffer, overlays[i].GetDrawLocationScreen(view));
176
177 EndPresentation(viewPresentation);
178 }
179
180 // 2.7) Restore from viewport clip
181 Backbuffer.Canvas.Restore();
182 }
183 finally
184 {
185 RenderContext.Pop();
186 }
187 }
188
189 private void RenderBitmapLayers(View view)
190 {
191 // 2.5) Render each visible layer's dirty regions for this view
192 // this will draw SceneLayerTiles, Sprites, and SceneLayer-based DirectDrawings
193 var sceneLayers = Scene.VisibleSceneLayers;
194
195 for (int i = 0; i < sceneLayers.Count; i++)
196 {
197 var layer = sceneLayers[i];
198
199 int layerPresentation = BeginPresentation(
200 GetLayerPresentationBounds(view, layer),
201 layer.EffectOpacity,
202 layer.EffectReveal,
203 layer.EffectRevealDirection);
204
205 if (layerPresentation == 0)
206 continue;
207
208 RenderLayerDirtyRegions(view, layer);
209 EndPresentation(layerPresentation);
210 }
211 }
212
213 private void EnqueueFullSceneRefresh()
214 {
215 foreach (var view in ViewManager.Views)
216 {
217 var viewport = view.Viewport;
218 var screenRect = viewport.TargetRectPx;
219
220 for (int i = 0; i < Scene.CountOfVisibleLayers; i++)
221 {
222 var layer = Scene.VisibleSceneLayers[i];
223
224 // 1) Compute layer-specific visible world rect
225 var layerWorldRect = view.ScreenRectToWorldRect(layer, screenRect);
226
227 // 2) Expand world rect by **one full tile** in all directions.
228 // This compensates for fractional-layer motion (parallax),
229 // camera motion, and tile boundaries.
230 int expandX = layer.TileWidth;
231 int expandY = layer.TileHeight;
232
233 layerWorldRect.Inflate(expandX, expandY);
234
235 // 3) Round to ints and enqueue
236 var worldRectInt = layerWorldRect.ToPixelAlignedRect();
237
238 layer.RefreshQueue.AddWorldRect(worldRectInt);
239 }
240 }
241 }
242
243 private List<Rectangle> CollectDirtyScreenArea(View view)
244 {
245 var dirty = new List<Rectangle>(64);
246 var viewportRect = view.GetRenderViewportTargetRectPx();
247
248 foreach (var sceneLayer in Scene.VisibleSceneLayers)
249 {
250 var refreshQueue = sceneLayer.RefreshQueue;
251
252 if (!refreshQueue.IsDirty)
253 continue;
254
255 foreach (var worldRect in refreshQueue.SnapshotWorldRects())
256 {
257 var screenRectF = view.WorldRectToScreenRect(sceneLayer, worldRect);
258
259 var rect = Rectangle.Intersect(
260 screenRectF.ToPixelAlignedRect(),
261 viewportRect);
262
263 if (rect.IsEmpty)
264 continue;
265
266 dirty.AddDeduped(rect);
267 }
268 }
269
270 return dirty;
271 }
272
273 private void PreclearScreenAreas(View view, List<Rectangle> screenRects)
274 {
275 if (Backbuffer is null || screenRects is null || screenRects.Count == 0)
276 return;
277
278 foreach (var screenRect in screenRects)
279 {
280 var screenRectViewport = Rectangle.Intersect(screenRect, view.GetRenderViewportTargetRectPx());
281
282 if (screenRectViewport.IsEmpty || screenRectViewport.Width <= 0 || screenRectViewport.Height <= 0)
283 continue;
284
285 // Clear just this patch (overwrite with Backbuffer.ClearColor)
286 Backbuffer.ClearRect(screenRectViewport);
287
288 EnqueueForOverlappingSceneLayers(view, screenRectViewport);
289 }
290 }
291
292 private void EnqueueForOverlappingSceneLayers(View sourceView, Rectangle clearedScreenRect)
293 {
294 var overlap = Rectangle.Intersect(clearedScreenRect, sourceView.GetRenderViewportTargetRectPx());
295
296 if (overlap.IsEmpty)
297 return;
298
299 foreach (var sceneLayer in Scene.VisibleSceneLayers)
300 {
301 var worldRectF = sourceView.ScreenRectToWorldRect(sceneLayer, overlap);
302 var worldRect = worldRectF.ToPixelAlignedRect();
303
304 if (!worldRect.IsEmpty)
305 sceneLayer.RefreshQueue.AddWorldRect(worldRect);
306 }
307 }
308
309 private void RenderLayerDirtyRegions(View view, SceneLayer layer)
310 {
311 var refreshQueue = layer.RefreshQueue;
312
313 // if this layer has no dirty regions and we are not forcing a full redraw, skip it.
314 if (!refreshQueue.IsDirty)
315 return;
316
317 foreach (var worldRect in refreshQueue.SnapshotWorldRects())
318 {
319 // draw tiles/sprites/direct drawings in this world rect
320 var drawables = layer.GetDrawablesInWorldRect(worldRect);
321
322 // project world → screen for adapter dirty
323 var screenRect = view.WorldRectToScreenRect(layer, worldRect).ToPixelAlignedRect();
324
325 Backbuffer.DrawDrawables(view, drawables, screenRect);
326 }
327 }
328}
Manages the lifecycle, registration, and retrieval of all direct drawing objects in the Gondwana engi...
static DirectDrawingManager Instance
Gets the singleton instance of the direct drawing manager.
IReadOnlyList< DirectDrawingBase > GetDrawingsForView(View view)
Returns all direct drawings associated with a specific view, ordered by Z-order then nickname.
Represents a base class for managing a graphical backbuffer, and is the in-memory surface where rende...
override ViewManager ViewManager
Gets the view manager that controls camera positions, viewports, and multi-view rendering.
override Scene Scene
Gets the scene currently bound to this render surface host.
RenderSurfaceHost() RenderSurfaceHost(RenderSurfaceAdapterBase renderSurfaceAdapter)
Initializes a new instance of the RenderSurfaceHost<TBackbuffer> class with the specified render surf...
Action? RenderBackbufferNoOp
Occurs when a backbuffer render operation is skipped because the scene is not dirty.
override BackbufferBase Backbuffer
Gets the backbuffer used for off-screen rendering before presentation to the adapter.
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
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
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
bool FullRefreshNeeded
Gets or sets a value indicating whether the entire scene needs to be refreshed on the next render.
Definition Scene.cs:250