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
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 if (!Engine.IsInitialized)
47 throw new InvalidOperationException("Logical resolution can only be configured after engine initialization.");
48 if (_logicalResolution is not null)
49 throw new InvalidOperationException("Logical resolution is already configured for this host.");
50
51 _logicalResolution = new BlazorLogicalResolutionBinding(
52 RenderSurfaceHost.RenderSurfaceAdapter,
53 logicalWidth,
54 logicalHeight,
55 scale => Engine.Configuration.RenderScale = scale);
56 }
57
63 protected BlazorGameHostBase(IJSRuntime jsRuntime)
64 {
65 _jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
66 }
67
70
72 protected abstract RenderSurfaceHostBase RenderSurfaceHost { get; }
73
84 protected virtual bool RenderSurfaceDrivesBrowserFrames => false;
85
88 protected abstract void BindSceneCore(Scene scene);
89
91 protected sealed override void ConfigurePlatform()
92 {
93 if (OperatingSystem.IsBrowser())
94 BrowserConsoleLogging.AttachToEngineLogger();
95
97 }
98
100 protected sealed override void ConfigureKeyboard()
101 {
102 Engine.InitializeBlazorKeyboardAdapter(RenderSurfaceComponent);
104 }
105
107 protected sealed override void ConfigureMouse()
108 {
109 Engine.InitializeBlazorMouseAdapter(RenderSurfaceComponent);
111 }
112
114 protected sealed override void ConfigureGamepads()
115 {
117 }
118
120 protected sealed override void ConfigureTouch()
121 {
122 Engine.InitializeBlazorTouchAdapter(RenderSurfaceComponent);
124 }
125
127 protected sealed override void BindScene()
128 {
129 var scene = Scene
130 ?? throw new InvalidOperationException(
131 $"{nameof(BindScene)} cannot be called before {nameof(Scene)} has been created.");
132
133 BindSceneCore(scene);
134 }
135
137 protected sealed override SynchronizationContext? GetSynchronizationContext()
138 {
139 var syncContext = base.GetSynchronizationContext();
140
141 if (syncContext is not null)
142 return syncContext;
143
144 if (!OperatingSystem.IsBrowser())
145 return null;
146
147 syncContext = new SynchronizationContext();
148 SynchronizationContext.SetSynchronizationContext(syncContext);
149
150 return syncContext;
151 }
152
154 protected sealed override void StartEngineCore(SynchronizationContext syncContext)
155 {
156 if (OperatingSystem.IsBrowser())
157 {
158 Engine.StartTimerDriven(syncContext);
159
161 _ = StartBrowserRenderLoopAsync();
162
163 return;
164 }
165
166 base.StartEngineCore(syncContext);
167 }
168
169 private async Task StartBrowserRenderLoopAsync()
170 {
171 var module = await _jsRuntime.InvokeAsync<IJSObjectReference>(
172 "import",
173 "./_content/Gondwana.Blazor/gondwana-blazor.js");
174
175 _module = module;
176 _dotNetRef = DotNetObjectReference.Create(this);
177
178 await module.InvokeVoidAsync("startRenderLoop", _dotNetRef);
179 }
180
182 [JSInvokable]
183 public void OnAnimationFrame()
184 {
185 if (Engine.IsRunning)
186 Engine.Tick();
187 }
188
190 protected sealed override void StopEngineCore()
191 {
192 if (_module is not null)
193 _ = _module.InvokeVoidAsync("stopRenderLoop");
194
195 base.StopEngineCore();
196 }
197
199 protected sealed override void OnInputConfigured()
200 {
201 base.OnInputConfigured();
203 }
204
206 protected sealed override void OnDisposed()
207 {
208 _logicalResolution?.Dispose();
209 _logicalResolution = null;
210
211 _dotNetRef?.Dispose();
212 _dotNetRef = null;
213
214 if (_module is not null)
215 {
216 _ = _module.DisposeAsync();
217 _module = null;
218 }
219
221 }
222
224 protected virtual void OnConfigurePlatform()
225 {
226 }
227
229 protected virtual void OnKeyboardAdapterInitialized()
230 {
231 }
232
234 protected virtual void OnMouseAdapterInitialized()
235 {
236 }
237
239 protected virtual void OnConfigureGamepads()
240 {
241 }
242
244 protected virtual void OnTouchAdapterInitialized()
245 {
246 }
247
249 protected virtual void OnBlazorDisposed()
250 {
251 }
252}
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 ...