Gondwana Game Engine Version 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
31 protected BlazorGameHostBase(IJSRuntime jsRuntime)
32 {
33 _jsRuntime = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime));
34 }
35
38
40 protected abstract RenderSurfaceHostBase RenderSurfaceHost { get; }
41
52 protected virtual bool RenderSurfaceDrivesBrowserFrames => false;
53
56 protected abstract void BindSceneCore(Scene scene);
57
59 protected sealed override void ConfigurePlatform()
60 {
61 if (OperatingSystem.IsBrowser())
62 BrowserConsoleLogging.AttachToEngineLogger();
63
65 }
66
68 protected sealed override void ConfigureKeyboard()
69 {
70 Engine.InitializeBlazorKeyboardAdapter(RenderSurfaceComponent);
72 }
73
75 protected sealed override void ConfigureMouse()
76 {
77 Engine.InitializeBlazorMouseAdapter(RenderSurfaceComponent);
79 }
80
82 protected sealed override void ConfigureGamepads()
83 {
85 }
86
88 protected sealed override void ConfigureTouch()
89 {
90 Engine.InitializeBlazorTouchAdapter(RenderSurfaceComponent);
92 }
93
95 protected sealed override void BindScene()
96 {
97 var scene = Scene
98 ?? throw new InvalidOperationException(
99 $"{nameof(BindScene)} cannot be called before {nameof(Scene)} has been created.");
100
101 BindSceneCore(scene);
102 }
103
105 protected sealed override SynchronizationContext? GetSynchronizationContext()
106 {
107 var syncContext = base.GetSynchronizationContext();
108
109 if (syncContext is not null)
110 return syncContext;
111
112 if (!OperatingSystem.IsBrowser())
113 return null;
114
115 syncContext = new SynchronizationContext();
116 SynchronizationContext.SetSynchronizationContext(syncContext);
117
118 return syncContext;
119 }
120
122 protected sealed override void StartEngineCore(SynchronizationContext syncContext)
123 {
124 if (OperatingSystem.IsBrowser())
125 {
126 Engine.StartTimerDriven(syncContext);
127
129 _ = StartBrowserRenderLoopAsync();
130
131 return;
132 }
133
134 base.StartEngineCore(syncContext);
135 }
136
137 private async Task StartBrowserRenderLoopAsync()
138 {
139 var module = await _jsRuntime.InvokeAsync<IJSObjectReference>(
140 "import",
141 "./_content/Gondwana.Blazor/gondwana-blazor.js");
142
143 _module = module;
144 _dotNetRef = DotNetObjectReference.Create(this);
145
146 await module.InvokeVoidAsync("startRenderLoop", _dotNetRef);
147 }
148
150 [JSInvokable]
151 public void OnAnimationFrame()
152 {
153 if (Engine.IsRunning)
154 Engine.Tick();
155 }
156
158 protected sealed override void StopEngineCore()
159 {
160 if (_module is not null)
161 _ = _module.InvokeVoidAsync("stopRenderLoop");
162
163 base.StopEngineCore();
164 }
165
167 protected sealed override void OnInputConfigured()
168 {
169 base.OnInputConfigured();
171 }
172
174 protected sealed override void OnDisposed()
175 {
176 _dotNetRef?.Dispose();
177 _dotNetRef = null;
178
179 if (_module is not null)
180 {
181 _ = _module.DisposeAsync();
182 _module = null;
183 }
184
186 }
187
189 protected virtual void OnConfigurePlatform()
190 {
191 }
192
194 protected virtual void OnKeyboardAdapterInitialized()
195 {
196 }
197
199 protected virtual void OnMouseAdapterInitialized()
200 {
201 }
202
204 protected virtual void OnConfigureGamepads()
205 {
206 }
207
209 protected virtual void OnTouchAdapterInitialized()
210 {
211 }
212
214 protected virtual void OnBlazorDisposed()
215 {
216 }
217}
BlazorRenderSurfaceComponentBase RenderSurfaceComponent
Gets the concrete Blazor component that supplies DOM input events.
virtual void OnKeyboardAdapterInitialized()
Called after the Blazor keyboard adapter is initialized.
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.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...