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
BlazorBitmapRenderSurfaceAdapter.cs
1using System.Runtime.InteropServices;
3using SkiaSharp;
4
6
18{
19 private readonly BlazorBitmapRenderSurfaceComponent _component;
20 private byte[]? _pixelBuffer;
21 private bool _disposed;
22
29 : base(1, 1, initialSizeAvailable: false)
30 {
31 _component = component ?? throw new ArgumentNullException(nameof(component));
32 }
33
39 internal void UpdateSize(int width, int height)
40 {
41 SetDestinationSize(width, height);
42 }
43
51 public override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
52 {
53 if (_disposed)
54 {
55 bufferImage.Dispose();
56 return;
57 }
58
59 var srcRect = SKRectI.Intersect(bufferRect, new SKRectI(0, 0, bufferImage.Width, bufferImage.Height));
60 var w = srcRect.Width;
61 var h = srcRect.Height;
62 if (w <= 0 || h <= 0)
63 {
64 bufferImage.Dispose();
65 return;
66 }
67
68 var needed = w * h * 4;
69 if (_pixelBuffer == null || _pixelBuffer.Length != needed)
70 _pixelBuffer = new byte[needed];
71
72 var info = new SKImageInfo(w, h, SKColorType.Rgba8888, SKAlphaType.Unpremul);
73
74 // Pin the managed array so we can pass its address to native SkiaSharp code.
75 var gcHandle = GCHandle.Alloc(_pixelBuffer, GCHandleType.Pinned);
76 bool success;
77 try
78 {
79 success = bufferImage.ReadPixels(info, gcHandle.AddrOfPinnedObject(), w * 4, srcRect.Left, srcRect.Top);
80 }
81 finally
82 {
83 gcHandle.Free();
84 }
85
86 if (!success)
87 {
88 bufferImage.Dispose();
89 return;
90 }
91
92 // In Blazor WASM (single-threaded) no copy is needed: the frame cannot be overwritten
93 // before the queued InvokeAsync action consumes it. For Blazor Server the host is
94 // expected to configure a separate loop, but the copy is omitted here for performance.
95 _component.EnqueueFrame(_pixelBuffer, w, h, srcRect.Left, srcRect.Top, bufferImage.Width, bufferImage.Height);
96
97 bufferImage.Dispose();
98 }
99
101 public void Dispose()
102 {
103 if (_disposed) return;
104 _disposed = true;
105 _pixelBuffer = null;
106 }
107}
BlazorBitmapRenderSurfaceAdapter(BlazorBitmapRenderSurfaceComponent component)
Initializes a new instance of BlazorBitmapRenderSurfaceAdapter.
override void Present(SKImage bufferImage, SKRectI bufferRect, SKRect destRect)
Presents the specified buffer image to the render surface by reading pixels into an RGBA byte array a...
A Blazor component that presents Gondwana game frames on an HTML <canvas> element and forwards keyboa...
void SetDestinationSize(int destWidth, int destHeight)
Sets the destination size of the render surface and raises the Resized event if the dimensions have c...
RenderSurfaceAdapterBase(int destWidth, int destHeight)
Initializes a new instance of the RenderSurfaceAdapterBase class with the specified dimensions.