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
BlazorBitmapRenderSurfaceComponent.razor.cs
3using Microsoft.AspNetCore.Components;
4using Microsoft.JSInterop;
5
7
27{
28 private ElementReference _canvasRef;
29 private IJSObjectReference? _module;
30 private DotNetObjectReference<BlazorBitmapRenderSurfaceComponent>? _dotNetRef;
31 private bool _moduleLoaded;
32
34 public BlazorBitmapRenderSurfaceAdapter Adapter { get; private set; } = null!;
35
37 public RenderSurfaceHost<BitmapBackbuffer> Host { get; private set; } = null!;
38
39 internal override ValueTask<CanvasOffset> GetCanvasOffsetAsync()
40 => _module is null
41 ? ValueTask.FromResult(default(CanvasOffset))
42 : _module.InvokeAsync<CanvasOffset>("getCanvasOffset", _canvasRef);
43
44 internal override RenderSurfaceAdapterBase? InputSurfaceAdapter => Adapter;
45
47 protected override void OnInitialized()
48 {
50 Host = new RenderSurfaceHost<BitmapBackbuffer>(Adapter);
51 }
52
54 protected override async Task OnAfterRenderAsync(bool firstRender)
55 {
56 if (!firstRender)
57 return;
58
59 _module = await JS.InvokeAsync<IJSObjectReference>(
60 "import",
61 "./_content/Gondwana.Blazor/gondwana-blazor.js");
62
63 _moduleLoaded = true;
64 _dotNetRef = DotNetObjectReference.Create(this);
65
66 await _module.InvokeVoidAsync("observeCanvasSize", _canvasRef, _dotNetRef);
67
68 // Get the actual canvas size and update the adapter
69 await UpdateCanvasSizeAsync();
70
71 // Request focus so keyboard events are received without requiring a click.
72 try
73 {
74 await _canvasRef.FocusAsync();
75 }
76 catch
77 {
78 // Focus request is best-effort; ignore failures (e.g. server-side pre-render).
79 }
80 }
81
86 internal async Task UpdateCanvasSizeAsync()
87 {
88 if (!_moduleLoaded || _module is null)
89 return;
90
91 try
92 {
93 var size = await _module.InvokeAsync<CanvasSize>("getCanvasSize", _canvasRef);
94
95 if (size.Width > 0 && size.Height > 0)
96 {
97 Adapter.UpdateSize(size.Width, size.Height);
98 }
99 }
100 catch
101 {
102 // Size query failed; adapter remains at default 1x1
103 }
104 }
105
111 [JSInvokable]
112 public void OnCanvasSizeChanged(int width, int height)
113 {
114 if (width > 0 && height > 0)
115 {
116 Adapter.UpdateSize(width, height);
117 PresentRetainedFrame();
118 }
119 }
120
131 internal void EnqueueFrame(
132 byte[] rgbaPixels,
133 int width,
134 int height,
135 int x,
136 int y,
137 int canvasWidth,
138 int canvasHeight)
139 {
140 if (!_moduleLoaded || _module is null)
141 return;
142
143 if (_module is IJSInProcessObjectReference inProcessModule)
144 {
145 try
146 {
147 inProcessModule.InvokeVoid(
148 "putImageData",
149 _canvasRef,
150 canvasWidth,
151 canvasHeight,
152 width,
153 height,
154 x,
155 y,
156 rgbaPixels,
159 Adapter.Presentation.DestinationRect.Left,
160 Adapter.Presentation.DestinationRect.Top,
161 Adapter.Presentation.DestinationRect.Width,
162 Adapter.Presentation.DestinationRect.Height,
163 Engine.Instance.Configuration.RenderScalingFilter == RenderScalingFilter.NearestNeighbor);
164
165 return;
166 }
167 catch
168 {
169 // Fall back to the async path if sync interop is unavailable or fails.
170 }
171 }
172
173 _ = InvokeAsync(async () =>
174 {
175 if (!_moduleLoaded || _module is null)
176 return;
177
178 await _module.InvokeVoidAsync(
179 "putImageData",
180 _canvasRef,
181 canvasWidth,
182 canvasHeight,
183 width,
184 height,
185 x,
186 y,
187 rgbaPixels,
190 Adapter.Presentation.DestinationRect.Left,
191 Adapter.Presentation.DestinationRect.Top,
192 Adapter.Presentation.DestinationRect.Width,
193 Adapter.Presentation.DestinationRect.Height,
194 Engine.Instance.Configuration.RenderScalingFilter == RenderScalingFilter.NearestNeighbor);
195 });
196 }
197
198 private void PresentRetainedFrame()
199 {
200 if (_module is null)
201 return;
202
203 var rect = Adapter.Presentation.DestinationRect;
204
205 _ = _module.InvokeVoidAsync(
206 "presentBitmap",
207 _canvasRef,
208 Adapter.Width,
209 Adapter.Height,
210 rect.Left,
211 rect.Top,
212 rect.Width,
213 rect.Height,
214 Engine.Instance.Configuration.RenderScalingFilter == RenderScalingFilter.NearestNeighbor);
215 }
216
218 public override void Dispose()
219 {
220 if (_moduleLoaded && _module is not null)
221 {
222 try
223 {
224 if (_module is IJSInProcessObjectReference inProcessModule)
225 {
226 inProcessModule.InvokeVoid("unobserveCanvasSize", _canvasRef);
227 }
228 else
229 {
230 _ = _module.InvokeVoidAsync("unobserveCanvasSize", _canvasRef);
231 }
232 }
233 catch
234 {
235 // Observer cleanup is best-effort during disposal.
236 }
237 }
238
239 _dotNetRef?.Dispose();
240 _dotNetRef = null;
241 _ = _module?.DisposeAsync();
242 _module = null;
243 _moduleLoaded = false;
244
245 Adapter?.Dispose();
246 Host?.Dispose();
247 }
248
249 private sealed class CanvasSize
250 {
254 public int Width { get; set; }
255
259 public int Height { get; set; }
260 }
261}
Provides a bitmap (CPU) render surface adapter for Blazor that presents each frame by converting pixe...
A Blazor component that presents Gondwana game frames on an HTML <canvas> element and forwards keyboa...
void OnCanvasSizeChanged(int width, int height)
Updates the adapter after JavaScript observes a canvas client-size change.
BlazorBitmapRenderSurfaceAdapter Adapter
Gets the render surface adapter that drives this component.
RenderSurfaceHost< BitmapBackbuffer > Host
Gets the RenderSurfaceHost<T> bound to this component.
Provides the shared DOM input surface used by Gondwana's Blazor bitmap and GPU render components.
Represents the core game engine singleton responsible for managing the main update loop,...
Definition Engine.cs:36
static Engine Instance
Gets the singleton instance of the Engine.
Definition Engine.cs:49
EngineConfiguration Configuration
Gets the current engine configuration settings.
Definition Engine.cs:952
Represents the abstract base class for render surface adapters that present backbuffer output to a de...
int Width
Gets the current width of the render surface in pixels.
PresentationTransform Presentation
Current immutable aspect-preserving mapping of Backbuffer ScreenPx to adapter pixels.
int Height
Gets the current height of the render surface in pixels.
RenderScalingFilter
Filtering used only when presenting the finished Backbuffer.