Skip to content

Game Class

shmellyorc edited this page Sep 14, 2026 · 6 revisions

Game Class

The Game class is the center of a VOID application. It owns the native window, selected renderer, timing, main loop, and application lifecycle.

You can use Game directly, but most games inherit from it and override the lifecycle methods.

Lifecycle

A VOID game follows four main hooks:

Method Purpose
OnEnter() One-time setup before the loop starts
OnUpdate(FrameTime) Game logic and input
OnDraw(FrameTime) Rendering
OnExit() Final game-owned cleanup

Example:

public sealed class MyGame : Game
{
    private Texture _logo;
    private SpriteBatcher _batcher;

    public MyGame(GameSettings settings) : base(settings) { }

    protected override void OnEnter()
    {
        _logo = AssetManager.Instance.Load<Texture>("Graphics/logo.png");
        _batcher = new SpriteBatcher();
    }

    protected override void OnUpdate(FrameTime frameTime)
    {
        // Update game state.
    }

    protected override void OnDraw(FrameTime frameTime)
    {
        _batcher.Begin();
        _batcher.Draw(_logo, new Vect2(100, 100), Color.White);
        _batcher.End();
    }

    protected override void OnExit()
    {
        _batcher?.Dispose();
    }
}

Starting the Game

var settings = GameSettings.Instance
    .SetAppCompany("MyStudio")
    .SetAppName("MyGame")
    .SetWindow(1280, 720)
    .Build();

using var game = new MyGame(settings);
game.Run();

Build() must be called before the Game is constructed.

FrameTime

OnUpdate and OnDraw receive the current FrameTime. FrameTime owns VOID's timing state and keeps the full timing context together instead of passing standalone delta values around the engine.

Use DeltaTime for frame-rate-independent gameplay movement:

position +=
    velocity * frameTime.DeltaTime;

Useful timing values include:

float scaled =
    frameTime.DeltaTime;

float real =
    frameTime.UnscaledDeltaTime;

TimeSpan total =
    frameTime.TotalTime;

float alpha =
    frameTime.Alpha;

TotalTime accumulates the engine's clamped real outer-frame time and is not multiplied by TimeScale.

Timing values follow the current engine phase:

  • In fixed timestep mode, OnUpdate sees the configured fixed interval through ElapsedTime, DeltaTime, and UnscaledDeltaTime.
  • Before rendering, VOID switches FrameTime to the actual elapsed time of the current rendered frame. OnDraw and automatically updated cameras therefore see real rendered-frame timing.
  • In variable timestep mode, update and rendering both use the actual elapsed frame time.

VOID may run more than one fixed update before a draw when the accumulator needs to catch up. Rendering still occurs once per presented frame.

Alpha can be used to interpolate visual state between fixed updates.

Window Access

The active window is available through:

Game.Instance.Window

For example:

if (!Game.Instance.Window.IsFocused)
{
    // Game window is not focused.
}

Runtime window changes are queued and then applied:

Game.Instance.Window
    .SetTitle("New Title")
    .SetSize(1600, 900)
    .ApplyChanges();

See Window & Displays.

Default Font

VOID includes an embedded default SpriteFont:

var font = Game.Instance.Font;

This is useful for debug text and simple UI without loading a custom font first.

Application Folders

VOID exposes the resolved application paths through the game instance:

string root = Game.Instance.ApplicationFolder;
string logs = Game.Instance.ApplicationLogFolder;
string saves = Game.Instance.ApplicationSaveFolder;
string config = Game.Instance.ApplicationConfigFolder;
string temp = Game.Instance.ApplicationTempFolder;

Recommended Pattern

Create long-lived objects such as batchers in OnEnter, reuse them during the game, and dispose objects your game owns in OnExit.

Avoid allocating a new batcher every frame.


Back to Home

Clone this wiki locally