Skip to content

GameApplication.RunAsync Method

Definition

Namespace: Brine2D.Hosting

Overloads
RunAsync<TScene>(Func<IServiceProvider,TScene>, CancellationToken) Starts the game application with a custom scene factory. Use this when you need to pass runtime data to the initial scene that DI alone cannot provide. Pass null for sceneFactory to use standard DI resolution.
RunAsync<TScene>(CancellationToken) Starts the game application with the specified initial scene. Runs the game on a dedicated thread and returns a task that completes when the game exits.

GameApplication.RunAsync\<TScene>(Func\<IServiceProvider,TScene>, CancellationToken) Method

Starts the game application with a custom scene factory. Use this when you need to pass runtime data to the initial scene that DI alone cannot provide. Pass null for sceneFactory to use standard DI resolution.

public System.Threading.Tasks.Task RunAsync<TScene>(System.Func<System.IServiceProvider,TScene>? sceneFactory, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken))
    where TScene : Brine2D.Engine.Scene;

Type parameters

TScene

The initial scene type to load.

Parameters

sceneFactory System.Func<System.IServiceProvider,TScene>

Factory function to create the scene, or null to resolve via DI.

cancellationToken System.Threading.CancellationToken

Token to cancel the game execution.

Returns

System.Threading.Tasks.Task
A task that completes when the game exits. The task will fault if engine initialization or the initial scene load fails on the game thread.

Exceptions

System.InvalidOperationException
Thrown if the game is already running.

System.ObjectDisposedException
Thrown if the application has been disposed.

Example

await game.RunAsync<GameScene>(sp =>
    new GameScene(sp.GetRequiredService<IRenderer>(), levelNumber: 5));

Remarks

The initial scene is loaded synchronously before the game loop starts, so a LoadingScene cannot be displayed during this first load. To show a loading screen from startup, load a lightweight placeholder scene first and transition to the real scene (with a loading screen) from its OnEnter.

GameApplication.RunAsync\<TScene>(CancellationToken) Method

Starts the game application with the specified initial scene. Runs the game on a dedicated thread and returns a task that completes when the game exits.

public System.Threading.Tasks.Task RunAsync<TScene>(System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken))
    where TScene : Brine2D.Engine.Scene;

Type parameters

TScene

The initial scene type to load.

Parameters

cancellationToken System.Threading.CancellationToken

Token to cancel the game execution.

Returns

System.Threading.Tasks.Task
A task that completes when the game exits. The task will fault if engine initialization or the initial scene load fails on the game thread.

Exceptions

System.InvalidOperationException
Thrown if the game is already running.

System.ObjectDisposedException
Thrown if the application has been disposed.

Remarks

The game runs on a dedicated thread; SDL3 window events are posted to and must be polled from the thread that created the window.

The initial scene is loaded synchronously before the game loop starts, so a LoadingScene cannot be displayed during this first load. To show a loading screen from startup, load a lightweight placeholder scene first and transition to the real scene (with a loading screen) from its OnEnter.