Gondwana Game Engine Development (master) - 2.6.1
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 private Resolution? _requestedResolution;
22
28 public BitmapBackbuffer(int width, int height) : base(width, height)
29 {
30 CreateSurface(width, height);
31 }
32
40 public static BitmapBackbuffer Create(int width, int height) => new BitmapBackbuffer(width, height);
41
52 protected internal override void RequestResize(int width, int height)
53 {
54 Interlocked.Exchange(ref _requestedResolution, new(width, height));
55 }
56
66 public override SKCanvas Canvas
67 {
68 get { lock (_gate) return _surface!.Canvas; }
69 }
70
71 private readonly SKPaint _bitmapPaint = new SKPaint
72 {
73 FilterQuality = SKFilterQuality.None,
74 BlendMode = SKBlendMode.SrcOver
75 };
76
84 public SKFilterQuality FilterQuality
85 {
86 get => _bitmapPaint.FilterQuality;
87 set => _bitmapPaint.FilterQuality = value;
88 }
89
103 protected internal override void BeginFrame()
104 {
105 // if a resize was requested, do it now (render thread only)...
106 if (Interlocked.Exchange(ref _requestedResolution, null) is { } request)
107 {
108 var w = request.Width;
109 var h = request.Height;
110
111 if (w > 0 && h > 0 && (w != Width || h != Height))
112 {
113 lock (_gate)
114 {
115 if (_disposed) return;
116 DisposeSurface_NoLock();
117 CreateSurface(w, h);
118 UpdateSize(w, h); // tell base about new logical size
119 }
120 }
121 }
122
123 // Prepare for drawing.
124 lock (_gate)
125 {
126 if (_disposed) return;
127 var c = _surface!.Canvas;
128 c.RestoreToCount(1);
129 c.Save();
130 c.ResetMatrix();
131 c.ClipRect(new SKRect(0, 0, Width, Height));
132 }
133 }
134
141 protected internal override void EndFrame()
142 {
143 lock (_gate)
144 {
145 if (_disposed || _surface is null) return;
146 _surface.Flush();
147 }
148 }
149
159 protected internal override void DrawTileFrame(Tile tile, RectangleF destRectScreen)
160 {
161 var bmp = tile.CurrentFrame.SkBitmap;
162
163 if (bmp is null)
164 return;
165
166 Canvas.DrawBitmap(bmp, destRectScreen.ToSKRect(), _bitmapPaint);
167 }
168
183 protected internal override SKImage Snapshot()
184 {
185 lock (_gate)
186 {
187 if (_disposed || _surface is null)
188 throw new ObjectDisposedException(nameof(BitmapBackbuffer));
189
190 return _surface.Snapshot(); // immutable; safe to use on UI thread
191 }
192 }
193
194 private void CreateSurface(int width, int height)
195 {
196 var info = new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
197 _buffer = new SKBitmap(info);
198 _surface = SKSurface.Create(info, _buffer.GetPixels(), _buffer.Info.RowBytes);
199 }
200
201 private void DisposeSurface_NoLock()
202 {
203 _surface?.Dispose(); _surface = null;
204 _buffer?.Dispose(); _buffer = null;
205 }
206
214 public override void Dispose()
215 {
216 lock (_gate)
217 {
218 if (_disposed) return;
219
220 _disposed = true;
221 base.Dispose();
222 DisposeSurface_NoLock();
223 }
224 }
225}
virtual Frame CurrentFrame
Gets or sets the current frame. The initial frame supplies the tile's default collision adjustment an...
Definition Tile.cs:176
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:65