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
BlazorGameHostBase.cs
6using Microsoft.JSInterop;
7
9
20public abstract class BlazorGameHostBase : GameHostBase
21{
22 private readonly IJSRuntime _jsRuntime;
23 private DotNetObjectReference<BlazorGameHostBase>? _dotNetRef;
24 private IJSObjectReference? _module;
25 private BlazorLogicalResolutionBinding? _logicalResolution;
26
43 public void ConfigureLogicalResolution(int logicalWidth, int logicalHeight)
44 {
46
47 if (!Engine.IsInitialized)
48 throw new InvalidOperationException(
49 "Logical resolution can only be configured after engine initialization.");
50
51 if (_logicalResolution is not null)
52 throw new InvalidOperationException("Logical resolution is already configured for this host.");
53
54 _logicalResolution = new BlazorLogicalResolutionBinding(
55 RenderSurfaceHost.RenderSurfaceAdapter,
56 logicalWidth,
57 logicalHeight,
58 scale => Engine.Configuration.RenderScale = scale);
59 }
60
66 protected BlazorGameHostBase(IJSRuntime jsRuntime)
67 {
68 _jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
69 }
70
73
75 protected abstract RenderSurfaceHostBase RenderSurfaceHost { get; }
76
87 protected virtual bool RenderSurfaceDrivesBrowserFrames => false;
88
91 protected abstract void BindSceneCore(Scene scene);
92
94 protected sealed override void ConfigurePlatform()
95 {
96 if (OperatingSystem.IsBrowser())
97 BrowserConsoleLogging.AttachToEngineLogger();
98
100 }
101
103 protected sealed override void ConfigureKeyboard()
104 {
105 Engine.InitializeBlazorKeyboardAdapter(RenderSurfaceComponent);
107 }
108
110 protected sealed override void ConfigureMouse()
111 {
112 Engine.InitializeBlazorMouseAdapter(RenderSurfaceComponent);
114 }
115
117 protected sealed override void ConfigureGamepads()
118 {
120 }
121
123 protected sealed override void ConfigureTouch()
124 {
125 Engine.InitializeBlazorTouchAdapter(RenderSurfaceComponent);
127 }
128
130 protected sealed override void BindScene()
131 {
132 var scene = Scene
133 ?? throw new InvalidOperationException(
134 $"{nameof(BindScene)} cannot be called before {nameof(Scene)} has been created.");
135
136 BindSceneCore(scene);
137 }
138
140 protected sealed override SynchronizationContext? GetSynchronizationContext()
141 {
142 var syncContext = base.GetSynchronizationContext();
143
144 if (syncContext is not null)
145 return syncContext;
146
147 if (!OperatingSystem.IsBrowser())
148 return null;
149
150 syncContext = new SynchronizationContext();
151 SynchronizationContext.SetSynchronizationContext(syncContext);
152
153 return syncContext;
154 }
155
157 protected sealed override void StartEngineCore(SynchronizationContext syncContext)
158 {
159 if (OperatingSystem.IsBrowser())
160 {
161 Engine.StartTimerDriven(syncContext);
162
164 _ = StartBrowserRenderLoopAsync();
165
166 return;
167 }
168
169 base.StartEngineCore(syncContext);
170 }
171
172 private async Task StartBrowserRenderLoopAsync()
173 {
174 var module = await _jsRuntime.InvokeAsync<IJSObjectReference>(
175 "import",
176 "./_content/Gondwana.Blazor/gondwana-blazor.js");
177
178 _module = module;
179 _dotNetRef = DotNetObjectReference.Create(this);
180
181 await module.InvokeVoidAsync("startRenderLoop", _dotNetRef);
182 }
183
185 [JSInvokable]
186 public void OnAnimationFrame()
187 {
188 if (Engine.IsRunning)
189 Engine.Tick();
190 }
191
193 protected sealed override void StopEngineCore()
194 {
195 if (_module is not null)
196 _ = _module.InvokeVoidAsync("stopRenderLoop");
197
198 base.StopEngineCore();
199 }
200
202 protected sealed override void OnInputConfigured()
203 {
204 base.OnInputConfigured();
206 }
207
209 protected sealed override void OnDisposed()
210 {
211 _logicalResolution?.Dispose();
212 _logicalResolution = null;
213
214 _dotNetRef?.Dispose();
215 _dotNetRef = null;
216
217 if (_module is not null)
218 {
219 _ = _module.DisposeAsync();
220 _module = null;
221 }
222
224 }
225
227 protected virtual void OnConfigurePlatform()
228 {
229 }
230
232 protected virtual void OnKeyboardAdapterInitialized()
233 {
234 }
235
237 protected virtual void OnMouseAdapterInitialized()
238 {
239 }
240
242 protected virtual void OnConfigureGamepads()
243 {
244 }
245
247 protected virtual void OnTouchAdapterInitialized()
248 {
249 }
250
252 protected virtual void OnBlazorDisposed()
253 {
254 }
255}
BlazorRenderSurfaceComponentBase RenderSurfaceComponent
Gets the concrete Blazor component that supplies DOM input events.
virtual void OnKeyboardAdapterInitialized()
Called after the Blazor keyboard adapter is initialized.
void ConfigureLogicalResolution(int logicalWidth, int logicalHeight)
Establishes a minimum logical game size from the first usable Blazor canvas dimensions.
override void ConfigureGamepads()
Configures the gamepad manager.
virtual void OnMouseAdapterInitialized()
Called after the Blazor mouse adapter is initialized.
override void ConfigureKeyboard()
Configures the keyboard input adapter.
override void ConfigureTouch()
Configures the touch input adapter.
virtual void OnBlazorDisposed()
Runs after Blazor interop resources have been released.
BlazorGameHostBase(IJSRuntime jsRuntime)
Initializes a new instance of BlazorGameHostBase.
void BindSceneCore(Scene scene)
Binds a scene to the concrete typed render-surface host.
override void ConfigureMouse()
Configures the mouse input adapter.
RenderSurfaceHostBase RenderSurfaceHost
Gets the render-surface host used for scene binding and widget input.
override void OnInputConfigured()
Runs after all input devices have been configured.Platform hosts may override this hook to call Initi...
virtual void OnConfigureGamepads()
Provides a hook for configuring browser gamepad support.
virtual void OnTouchAdapterInitialized()
Called after the Blazor touch adapter is initialized.
override void StopEngineCore()
Performs platform-specific engine shutdown work.
virtual void OnConfigurePlatform()
Provides a hook for configuring additional Blazor platform settings.
override void BindScene()
Binds the current scene to the platform render surface.
virtual bool RenderSurfaceDrivesBrowserFrames
Gets a value indicating whether the render surface itself supplies the browser animation callback tha...
override? SynchronizationContext GetSynchronizationContext()
Gets the synchronization context used to start the engine.The synchronization context,...
void OnAnimationFrame()
Advances the timer-driven bitmap path from Gondwana's browser animation loop.
override void StartEngineCore(SynchronizationContext syncContext)
Starts the engine using the supplied synchronization context.
override void OnDisposed()
Runs after managed resources have been disposed.
Provides the shared DOM input surface used by Gondwana's Blazor bitmap and GPU render components.
Provides a base class for hosting and managing the lifecycle of a Gondwana game application.
Scene? Scene
Gets the current active scene.
void InitializeWidgetInput(RenderSurfaceHostBase renderSurfaceHost)
Initializes widget input routing for the supplied render surface host.
Engine Engine
Gets the singleton instance of the Gondwana engine.
void EnsureNotDisposed()
Ensures that the host has not been disposed.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...