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
RenderSurfaceAdapterBase.cs
1using System.Drawing;
2using SkiaSharp;
3
4namespace Gondwana.Rendering;
5
10public abstract class RenderSurfaceAdapterBase
11{
12 private sealed class PresentationState(
13 int bufferWidth,
14 int bufferHeight,
15 int adapterWidth,
16 int adapterHeight)
17 {
18 internal readonly int BufferWidth = bufferWidth, BufferHeight = bufferHeight;
19 internal readonly int AdapterWidth = adapterWidth, AdapterHeight = adapterHeight;
20
21 internal readonly PresentationTransform Transform = PresentationTransform.Fit(
22 bufferWidth,
23 bufferHeight,
24 adapterWidth,
25 adapterHeight);
26 }
27
28 private readonly object _presentationSync = new();
29
30 private PresentationState _presentation = new(
31 1,
32 1,
33 0,
34 0);
35
36 internal bool InitialSizeAvailable { get; private set; }
37
39 public PresentationTransform Presentation => Volatile.Read(ref _presentation).Transform;
40
42 public float PresentationScale => Presentation.Scale;
43
44 internal void SetBackbufferSize(int width, int height)
45 {
46 lock (_presentationSync)
47 {
48 var state = _presentation;
49
50 Volatile.Write(
51 ref _presentation,
52 new(
53 width,
54 height,
55 state.AdapterWidth,
56 state.AdapterHeight));
57 }
58 }
59
63 public Point AdapterPxToScreenPx(PointF adapterPx)
64 {
65 Presentation.TryAdapterPxToScreenPx(adapterPx, out var screen);
66 // Floor is essential: a fractional negative margin must not truncate to the edge pixel zero.
67 return new Point((int)Math.Floor(screen.X), (int)Math.Floor(screen.Y));
68 }
69
74 public void DrawImage(SKCanvas canvas, SKImage image, SKColor clearColor)
75 {
76 var state = Volatile.Read(ref _presentation);
77
78 // A queued CPU snapshot may precede an explicit resolution change. Fit that complete
79 // image using the same authoritative calculation until its replacement arrives.
80 var transform = image.Width == state.BufferWidth && image.Height == state.BufferHeight
81 ? state.Transform
83 image.Width,
84 image.Height,
85 state.AdapterWidth,
86 state.AdapterHeight);
87
88 canvas.Clear(clearColor);
89
90 if (transform.Scale <= 0)
91 return;
92
93 canvas.DrawImage(image, transform.DestinationRect, PresentationSampling);
94 }
95
96 internal static SKSamplingOptions PresentationSampling => new(
97 Engine.Instance.Configuration.RenderScalingFilter == RenderScalingFilter.NearestNeighbor
98 ? SKFilterMode.Nearest
99 : SKFilterMode.Linear);
100
108 public event Action<RenderSurfaceAdapterResizedEventArgs>? Resized;
109
114 public int Width
115 {
116 get => Volatile.Read(ref _presentation).AdapterWidth;
117 protected set => SetDestinationSize(value, Height);
118 }
119
124 public int Height
125 {
126 get => Volatile.Read(ref _presentation).AdapterHeight;
127 protected set => SetDestinationSize(Width, value);
128 }
129
135 protected RenderSurfaceAdapterBase(int destWidth, int destHeight)
136 : this(destWidth, destHeight, initialSizeAvailable: true)
137 {
138 }
139
144 protected RenderSurfaceAdapterBase(int destWidth, int destHeight, bool initialSizeAvailable)
145 {
146 SetDestinationSize(destWidth, destHeight);
147 InitialSizeAvailable = initialSizeAvailable;
148 }
149
159 protected void SetDestinationSize(int destWidth, int destHeight)
160 {
161 var sizeAvailable = destWidth > 0 && destHeight > 0;
162
163 if (destWidth == Width && destHeight == Height && InitialSizeAvailable == sizeAvailable)
164 return;
165
166 var oldWidth = Width;
167 var oldHeight = Height;
168
169 InitialSizeAvailable = sizeAvailable;
170
171 lock (_presentationSync)
172 {
173 var state = _presentation;
174
175 Volatile.Write(
176 ref _presentation,
177 new(
178 state.BufferWidth,
179 state.BufferHeight,
180 destWidth,
181 destHeight));
182 }
183
184 Resized?.Invoke(
186 this,
187 oldWidth,
188 oldHeight,
189 Width,
190 Height));
191 }
192
202 public abstract void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect);
203}
int Width
Gets the current width of the render surface in pixels.
void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
Presents the specified portion of the Backbuffer image to the destination rectangle on the RenderSurf...
RenderSurfaceAdapterBase(int destWidth, int destHeight, bool initialSizeAvailable)
Constructs an adapter whose initial layout may still be pending.
float PresentationScale
Current derived presentation scale; adapter resize does not resize the Backbuffer.
void SetDestinationSize(int destWidth, int destHeight)
Sets the destination size of the render surface and raises the Resized event if the dimensions have c...
PresentationTransform Presentation
Current immutable aspect-preserving mapping of Backbuffer ScreenPx to adapter pixels.
RenderSurfaceAdapterBase(int destWidth, int destHeight)
Initializes a new instance of the RenderSurfaceAdapterBase class with the specified dimensions.
Point AdapterPxToScreenPx(PointF adapterPx)
Converts adapter input to logical ScreenPx, retaining outside coordinates for capture/leave.
int Height
Gets the current height of the render surface in pixels.
Action< RenderSurfaceAdapterResizedEventArgs >? Resized
Occurs when the render surface adapter is resized.
void DrawImage(SKCanvas canvas, SKImage image, SKColor clearColor)
Presents a complete image using the same transform as pointer normalization.
Provides data for the RenderSurfaceAdapterBase.Resized event.
readonly record struct PresentationTransform(float Scale, SKRect DestinationRect)
Aspect-preserving mapping between logical Backbuffer ScreenPx and adapter pixels.
@ NearestNeighbor
Unfiltered nearest-pixel presentation for pixel art.