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
BitmapBackbuffer.cs
1using System.Drawing;
4using SkiaSharp;
5
7
13public sealed class BitmapBackbuffer : BackbufferBase
14{
15 private readonly object _gate = new(); // guards _buffer/_surface/_disposed
16 private SKBitmap? _buffer;
17 private SKSurface? _surface;
18 private bool _disposed;
19
20 private sealed record Resolution(int Width, int Height);
21
22 private Resolution? _requestedResolution;
23
29 public BitmapBackbuffer(int width, int height) : base(width, height)
30 {
31 CreateSurface(width, height);
32 }
33
41 public static BitmapBackbuffer Create(int width, int height) => new BitmapBackbuffer(width, height);
42
53 protected internal override void RequestResize(int width, int height)
54 {
55 Interlocked.Exchange(ref _requestedResolution, new(width, height));
56 }
57
67 public override SKCanvas Canvas
68 {
69 get
70 {
71 lock (_gate)
72 return _surface!.Canvas;
73 }
74 }
75
76 private readonly SKPaint _bitmapPaint = new SKPaint
77 {
78 FilterQuality = SKFilterQuality.None,
79 BlendMode = SKBlendMode.SrcOver
80 };
81
89 public SKFilterQuality FilterQuality
90 {
91 get => _bitmapPaint.FilterQuality;
92 set => _bitmapPaint.FilterQuality = value;
93 }
94
108 protected internal override void BeginFrame()
109 {
110 // if a resize was requested, do it now (render thread only)...
111 if (Interlocked.Exchange(ref _requestedResolution, null) is { } request)
112 {
113 var w = request.Width;
114 var h = request.Height;
115
116 if (w > 0 && h > 0 && (w != Width || h != Height))
117 {
118 lock (_gate)
119 {
120 if (_disposed)
121 return;
122
123 DisposeSurface_NoLock();
124 CreateSurface(w, h);
125 UpdateSize(w, h); // tell base about new logical size
126 }
127 }
128 }
129
130 // Prepare for drawing.
131 lock (_gate)
132 {
133 if (_disposed)
134 return;
135
136 var c = _surface!.Canvas;
137 c.RestoreToCount(1);
138 c.Save();
139 c.ResetMatrix();
140
141 c.ClipRect(
142 new SKRect(
143 0,
144 0,
145 Width,
146 Height));
147 }
148 }
149
156 protected internal override void EndFrame()
157 {
158 lock (_gate)
159 {
160 if (_disposed || _surface is null)
161 return;
162
163 _surface.Flush();
164 }
165 }
166
176 protected internal override void DrawTileFrame(Tile tile, RectangleF destRectScreen)
177 {
178 var bmp = tile.CurrentFrame.SkBitmap;
179
180 if (bmp is null)
181 return;
182
183 Canvas.DrawBitmap(bmp, destRectScreen.ToSKRect(), _bitmapPaint);
184 }
185
200 protected internal override SKImage Snapshot()
201 {
202 lock (_gate)
203 {
204 if (_disposed || _surface is null)
205 throw new ObjectDisposedException(nameof(BitmapBackbuffer));
206
207 return _surface.Snapshot(); // immutable; safe to use on UI thread
208 }
209 }
210
211 private void CreateSurface(int width, int height)
212 {
213 var info = new SKImageInfo(
214 width,
215 height,
216 SKColorType.Bgra8888,
217 SKAlphaType.Premul);
218
219 _buffer = new SKBitmap(info);
220 _surface = SKSurface.Create(info, _buffer.GetPixels(), _buffer.Info.RowBytes);
221 }
222
223 private void DisposeSurface_NoLock()
224 {
225 _surface?.Dispose();
226 _surface = null;
227 _buffer?.Dispose();
228 _buffer = null;
229 }
230
238 public override void Dispose()
239 {
240 lock (_gate)
241 {
242 if (_disposed)
243 return;
244
245 _disposed = true;
246 base.Dispose();
247 DisposeSurface_NoLock();
248 }
249 }
250}
virtual Frame CurrentFrame
Gets or sets the current frame. The initial frame supplies the tile's default collision adjustment an...
Definition Tile.cs:185
int Width
Gets the current Backbuffer width in a thread-safe manner.
void UpdateSize(int width, int height)
Updates the stored backbuffer dimensions and raises the SizeChanged event.
int Height
Gets the current Backbuffer height in a thread-safe manner.
static BitmapBackbuffer Create(int width, int height)
Creates a new instance of BitmapBackbuffer with the specified dimensions. This method exists to suppo...
BitmapBackbuffer(int width, int height)
Initializes a new instance of the BitmapBackbuffer class with the specified dimensions.
SKFilterQuality FilterQuality
Gets or sets the filter quality used when drawing bitmaps to the backbuffer.
override void Dispose()
Releases all resources used by the BitmapBackbuffer.
override SKCanvas Canvas
Gets the SKCanvas for drawing operations on this backbuffer.
readonly? SKBitmap SkBitmap
Gets the SkiaSharp bitmap for this frame.
Definition Frame.cs:69