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.Presentation.cs
1using System.Drawing;
4using SkiaSharp;
5
6namespace Gondwana.Rendering;
7
8public sealed partial class RenderSurfaceHost<TBackbuffer>
9 where TBackbuffer : BackbufferBase
10{
11 private int _presentationInvalidated = 1;
12
26 public bool RedrawDirtyRectangleOnly { get; set; } = true;
27
36 internal override void PresentBackbufferToAdapter()
37 {
38 if (RenderSurfaceAdapter is null)
39 return;
40
41 Backbuffer.EndFrame();
42
43 if (RedrawDirtyRectangleOnly && Interlocked.Exchange(ref _presentationInvalidated, 0) == 0)
44 PresentBackbufferRect();
45 else
46 PresentBackbufferAll();
47
48 Backbuffer.ClearDirtyRectangle();
49 Backbuffer.BeginFrame();
50 }
51
52 internal override void InvalidatePresentation() => Interlocked.Exchange(ref _presentationInvalidated, 1);
53
54 private void PresentBackbufferAll()
55 {
56 if (Backbuffer == null)
57 return;
58
59 var img = Backbuffer.Snapshot();
60
61 var src = new SKRectI(
62 0,
63 0,
64 img.Width,
65 img.Height);
66
67 var dst = RenderSurfaceAdapter.Presentation.DestinationRect;
68
69 // Post to UI thread
70 Engine.Instance.UiDispatcher!.Post(() => RenderSurfaceAdapter.Present(img, src, dst));
71 }
72
73 private void PresentBackbufferRect()
74 {
75 if (Backbuffer == null)
76 return;
77
78 var dirty = Backbuffer.DirtyRectangle;
79
80 if (dirty.IsEmpty)
81 return;
82
83 var bounds = new Rectangle(
84 0,
85 0,
86 Backbuffer.Width,
87 Backbuffer.Height);
88
89 var clamped = Rectangle.Intersect(dirty, bounds);
90
91 if (clamped.IsEmpty)
92 return;
93
94 var img = Backbuffer.Snapshot();
95
96 // Post to UI thread
98 RenderSurfaceAdapter!.Present(
99 img,
100 clamped.ToSKRectI(),
102 img.Width,
103 img.Height,
104 RenderSurfaceAdapter.Width,
105 RenderSurfaceAdapter.Height)
106 .ScreenRectToAdapterRect(clamped).ToSKRect()));
107 }
108}
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static Engine Instance
Gets the singleton instance of the Engine.
Definition Engine.cs:49
IUiDispatcher? UiDispatcher
Gets the UI dispatcher used for marshalling operations to the UI thread.
Definition Engine.cs:815
bool RedrawDirtyRectangleOnly
Gets or sets a value indicating whether only the dirty (changed) regions of the backbuffer are presen...
void Post(Action action)
Asynchronously posts an action to be executed on the UI thread. This method queues the action and ret...
readonly record struct PresentationTransform(float Scale, SKRect DestinationRect)
Aspect-preserving mapping between logical Backbuffer ScreenPx and adapter pixels.