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
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(
60 bufferRect,
61 new SKRectI(
62 0,
63 0,
64 bufferImage.Width,
65 bufferImage.Height));
66
67 var w = srcRect.Width;
68 var h = srcRect.Height;
69
70 if (w <= 0 || h <= 0)
71 {
72 bufferImage.Dispose();
73 return;
74 }
75
76 var needed = w * h * 4;
77
78 if (_pixelBuffer == null || _pixelBuffer.Length != needed)
79 _pixelBuffer = new byte[needed];
80
81 var info = new SKImageInfo(
82 w,
83 h,
84 SKColorType.Rgba8888,
85 SKAlphaType.Unpremul);
86
87 // Pin the managed array so we can pass its address to native SkiaSharp code.
88 var gcHandle = GCHandle.Alloc(_pixelBuffer, GCHandleType.Pinned);
89 bool success;
90
91 try
92 {
93 success = bufferImage.ReadPixels(
94 info,
95 gcHandle.AddrOfPinnedObject(),
96 w * 4,
97 srcRect.Left,
98 srcRect.Top);
99 }
100 finally
101 {
102 gcHandle.Free();
103 }
104
105 if (!success)
106 {
107 bufferImage.Dispose();
108 return;
109 }
110
111 // In Blazor WASM (single-threaded) no copy is needed: the frame cannot be overwritten
112 // before the queued InvokeAsync action consumes it. For Blazor Server the host is
113 // expected to configure a separate loop, but the copy is omitted here for performance.
114 _component.EnqueueFrame(
115 _pixelBuffer,
116 w,
117 h,
118 srcRect.Left,
119 srcRect.Top,
120 bufferImage.Width,
121 bufferImage.Height);
122
123 bufferImage.Dispose();
124 }
125
127 public void Dispose()
128 {
129 if (_disposed)
130 return;
131
132 _disposed = true;
133 _pixelBuffer = null;
134 }
135}
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.