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
GameHostBase.cs
6using Microsoft.Extensions.Logging;
7
9
13public abstract class GameHostBase : IDisposable
14{
15 private bool _disposed;
16 private bool _initialized;
17 private bool _engineInitialized;
18 private bool _engineStarted;
19
20 private WidgetInputRouter? _widgetInputRouter;
21
25 public Engine Engine => Engine.Instance;
26
30 public Scene? Scene { get; protected set; }
31
35 protected WidgetInputRouter? WidgetInputRouter => _widgetInputRouter;
36
54 public void Initialize(
55 string? configPath = null,
56 bool? autoSaveConfig = null,
57 LogLevel logLevel = LogLevel.Warning)
58 => InitializeCore(
59 configPath,
60 autoSaveConfig,
61 logLevel,
62 configurationStore: null);
63
75 IEngineConfigurationStore configurationStore,
76 bool? autoSaveConfig = null,
77 LogLevel logLevel = LogLevel.Warning)
78 {
79 ArgumentNullException.ThrowIfNull(configurationStore);
80
81 InitializeCore(
82 configPath: null,
83 autoSaveConfig,
84 logLevel,
85 configurationStore);
86 }
87
88 private void InitializeCore(
89 string? configPath,
90 bool? autoSaveConfig,
91 LogLevel logLevel,
92 IEngineConfigurationStore? configurationStore)
93 {
96
98
99 ConfigureLogging(logLevel);
103
104 InitializeEngine(configPath, autoSaveConfig, configurationStore);
105 StartEngine();
106
107 _initialized = true;
108
110 }
111
125 protected void InitializeWidgetInput(RenderSurfaceHostBase renderSurfaceHost)
126 {
127 ArgumentNullException.ThrowIfNull(renderSurfaceHost);
128
129 _widgetInputRouter?.Dispose();
130
131 var keyboardPoller = Engine.Input.KeyboardEventPoller;
132 var mousePoller = Engine.Input.MouseEventPoller;
133 var touchPoller = Engine.Input.TouchEventPoller;
134
135 mousePoller?.StartMonitoringMouse(
136 trackMouseMovement: true);
137
138 _widgetInputRouter = new WidgetInputRouter(
139 renderSurfaceHost,
140 keyboardPoller,
141 mousePoller,
142 touchPoller);
143
144 _widgetInputRouter.Start();
145 }
146
150 protected virtual void OnInitializing()
151 {
152 }
153
158 protected void ConfigureLogging(
159 LogLevel logLevel)
160 {
161 EngineLogger.SetLogLevel(logLevel);
162 }
163
167 protected abstract void ConfigurePlatform();
168
172 protected void ConfigureInput()
173 {
178
180 }
181
185 protected virtual void ConfigureKeyboard()
186 {
187 }
188
192 protected virtual void ConfigureMouse()
193 {
194 }
195
199 protected virtual void ConfigureGamepads()
200 {
201 }
202
206 protected virtual void ConfigureTouch()
207 {
208 }
209
217 protected virtual void OnInputConfigured()
218 {
219 }
220
224 protected void InitializeGameContent()
225 {
226 LoadContent();
228 BindScene();
229 OnSceneBound();
231 }
232
236 protected void LoadContent()
237 {
238 LoadAssets();
241 }
242
246 protected virtual void LoadAssets()
247 {
248 }
249
253 protected virtual void LoadTilesheets()
254 {
255 }
256
260 protected virtual void LoadAnimationCycles()
261 {
262 }
263
270 protected void CreateSceneGraph()
271 {
273 ?? throw new InvalidOperationException(
274 $"{nameof(CreateInitialScene)} returned null.");
275
278 }
279
283 protected virtual void OnSceneGraphCreated()
284 {
285 }
286
291 protected virtual Scene CreateInitialScene()
292 {
293 return Scene.Empty;
294 }
295
299 protected virtual void CreateInitialViews()
300 {
301 }
302
306 protected virtual void BindScene()
307 {
308 }
309
313 protected virtual void OnSceneBound()
314 {
315 }
316
320 protected void InitializeSceneObjects()
321 {
324 }
325
329 protected virtual void CreateSprites()
330 {
331 }
332
336 protected virtual void CreateDirectDrawings()
337 {
338 }
339
350 protected void InitializeEngine(
351 string? configPath,
352 bool? autoSaveConfig)
353 => InitializeEngine(configPath, autoSaveConfig, configurationStore: null);
354
365 protected void InitializeEngine(
366 string? configPath,
367 bool? autoSaveConfig,
368 IEngineConfigurationStore? configurationStore)
369 {
370 if (configurationStore is null)
371 {
372 Engine.Instance.Initialize(
373 configFileName: configPath,
374 autoSaveConfig: autoSaveConfig);
375 }
376 else
377 {
378 Engine.Instance.InitializeWithConfigurationStore(
379 configurationStore,
380 autoSaveConfig: autoSaveConfig);
381 }
382
383 _engineInitialized = true;
384
386 }
387
391 protected virtual void OnEngineInitialized()
392 {
393 }
394
402 protected void StartEngine()
403 {
404 var syncContext = GetSynchronizationContext()
405 ?? throw new InvalidOperationException(
406 $"{nameof(Initialize)} must be called on a thread with a current {nameof(SynchronizationContext)}.");
407
408 StartEngineCore(syncContext);
409 _engineStarted = true;
410
412 }
413
418 protected virtual void StartEngineCore(
419 SynchronizationContext syncContext)
420 {
421 Engine.Instance.Start(syncContext);
422 }
423
428 protected virtual SynchronizationContext? GetSynchronizationContext()
429 {
430 return SynchronizationContext.Current;
431 }
432
436 protected virtual void OnEngineStarted()
437 {
438 }
439
443 protected virtual void OnInitialized()
444 {
445 }
446
450 protected virtual void UnhookEvents()
451 {
452 }
453
457 protected void StopEngine()
458 {
460 _engineStarted = false;
461 }
462
466 protected virtual void StopEngineCore()
467 {
468 Engine.Instance.Stop();
469 }
470
474 protected void DisposeEngine()
475 {
476 Engine.Instance.Dispose();
477 _engineInitialized = false;
478 }
479
483 protected virtual void OnDisposing()
484 {
485 }
486
490 protected virtual void OnDisposed()
491 {
492 }
493
500 protected void EnsureNotInitialized()
501 {
502 if (_initialized)
503 {
504 throw new InvalidOperationException(
505 $"{GetType().Name} has already been initialized.");
506 }
507 }
508
515 protected void EnsureNotDisposed()
516 {
517 ObjectDisposedException.ThrowIf(_disposed, this);
518 }
519
527 public void Dispose()
528 {
529 if (_disposed)
530 return;
531
532 // Stop platform scheduling and wait for rendering before any hook can release
533 // native drawing resources. Stop alone only requests that the loop exit.
534 if (_engineStarted)
535 StopEngine();
536
537 if (_engineInitialized)
538 Engine.StopAndWait();
539
540 OnDisposing();
541
542 _widgetInputRouter?.Dispose();
543 _widgetInputRouter = null;
544
545 UnhookEvents();
546
547 if (_engineInitialized)
549
550 _disposed = true;
551
552 OnDisposed();
553
554 GC.SuppressFinalize(this);
555 }
556}
Provides a base class for hosting and managing the lifecycle of a Gondwana game application.
virtual void StopEngineCore()
Performs platform-specific engine shutdown work.
void InitializeEngine(string? configPath, bool? autoSaveConfig)
Initializes the Gondwana engine using the default file-backed configuration store.
virtual void LoadAssets()
Loads textures, sounds, and other game assets.
void ConfigurePlatform()
Configures platform-specific services, adapters, and render infrastructure.
void InitializeEngine(string? configPath, bool? autoSaveConfig, IEngineConfigurationStore? configurationStore)
Initializes the Gondwana engine.
virtual ? SynchronizationContext GetSynchronizationContext()
Gets the synchronization context used to start the engine.
virtual void OnInputConfigured()
Runs after all input devices have been configured.
void InitializeGameContent()
Initializes game-specific content, the scene graph, and scene objects.
void InitializeSceneObjects()
Creates sprites and direct drawings for the current scene.
virtual void ConfigureKeyboard()
Configures the keyboard input adapter.
void LoadContent()
Loads assets, tilesheets, and animation cycles.
virtual void CreateInitialViews()
Creates the initial views and viewports used to render the scene.
virtual void OnInitialized()
Runs after the full host initialization sequence has completed.
virtual void StartEngineCore(SynchronizationContext syncContext)
Starts the engine using the supplied synchronization context.
void DisposeEngine()
Disposes the engine instance.
virtual void OnDisposing()
Runs before managed resources are disposed.
virtual void OnInitializing()
Runs before the host initialization sequence begins.
void EnsureNotInitialized()
Ensures that the host has not already been initialized.
virtual void BindScene()
Binds the current scene to the platform render surface.
virtual void OnEngineInitialized()
Runs after the engine has been initialized but before it has been started.
virtual void OnEngineStarted()
Runs after the engine has been started.
void StopEngine()
Stops the engine during host disposal.
virtual void OnDisposed()
Runs after managed resources have been disposed.
virtual void LoadAnimationCycles()
Loads animation cycle definitions.
virtual void UnhookEvents()
Unhooks custom event handlers during disposal.
virtual void LoadTilesheets()
Loads tilesheet definitions.
virtual void OnSceneBound()
Runs after the current scene has been bound to the render surface host.
virtual void ConfigureGamepads()
Configures the gamepad manager.
virtual void CreateSprites()
Creates sprite objects for the current scene.
void Dispose()
Releases resources owned by the game host.
void CreateSceneGraph()
Creates the initial scene and its views.
Scene? Scene
Gets the current active scene.
virtual Scene CreateInitialScene()
Creates the initial scene for the game.
void Initialize(string? configPath=null, bool? autoSaveConfig=null, LogLevel logLevel=LogLevel.Warning)
Initializes the game host by configuring logging, platform services, input, game content,...
void ConfigureInput()
Configures keyboard, mouse, gamepad, and touch input.
virtual void ConfigureMouse()
Configures the mouse input adapter.
void StartEngine()
Starts the engine using the synchronization context returned by GetSynchronizationContext.
virtual void OnSceneGraphCreated()
Runs after the scene graph has been created but before the scene is bound.
virtual void CreateDirectDrawings()
Creates direct drawings and widgets for the current scene.
void ConfigureLogging(LogLevel logLevel)
Configures the logging level used by Gondwana.
void InitializeWithConfigurationStore(IEngineConfigurationStore configurationStore, bool? autoSaveConfig=null, LogLevel logLevel=LogLevel.Warning)
Initializes the game host using a caller-supplied configuration persistence store.
WidgetInputRouter? WidgetInputRouter
Gets the widget input router, if widget input has been initialized.
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.
virtual void ConfigureTouch()
Configures the touch input adapter.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
Provides a persistence boundary for EngineConfiguration.