-
Notifications
You must be signed in to change notification settings - Fork 0
Game Class
The Game class is the heart of any Void application. It manages the window, game loop, timing, and lifecycle. You inherit from it and override the methods you need.
A Void game follows a simple lifecycle:
- OnEnter(): Called once when the game starts. This is where you load assets, initialize systems, and set up your game state.
- OnUpdate(FrameTime): Called every frame. This is where you handle input, update positions, run AI, and process game logic.
- OnDraw(FrameTime): Called every frame after OnUpdate. This is where you draw everything.
- OnExit(): Called once when the game exits. This is where you clean up resources.
OnUpdate and OnDraw receive a FrameTime object that provides timing information:
- DeltaTime: Time since the last frame. Use this for smooth movement.
- UnscaledDeltaTime: Real time since the last frame, unaffected by time scale.
- TotalTime: Total time the game has been running.
- FPS: Current frames per second.
Never assume a fixed frame rate. Always use DeltaTime for movement and animations.
The Game class requires a GameSettings instance created with the fluent builder. See the Getting Started page for a full example.
using Void.Engine;
public class MyGame : Game
{
private Texture _logo;
private SpriteBatcher _batcher;
private float _rotation;
public MyGame(GameSettings settings) : base(settings) { }
protected override void OnEnter()
{
_logo = AssetManager.Instance.Load<Texture>("logo.png");
_batcher = new SpriteBatcher();
}
protected override void OnUpdate(FrameTime frameTime)
{
_rotation += frameTime.DeltaTime * 0.5f;
}
protected override void OnDraw(FrameTime frameTime)
{
_batcher.Begin();
_batcher.Draw(_logo, new Vect2(400, 300), Color.White, _rotation, new Vect2(32, 32));
_batcher.End();
}
protected override void OnExit()
{
_batcher?.Dispose();
}
}
In Program.cs:
using Void.Engine;
var settings = GameSettings.Instance
.SetAppCompany("MyStudio")
.SetAppName("MyGame")
.SetWindow(1280, 720)
.Build();
using var game = new MyGame(settings);
game.Run();
The batcher is created once in OnEnter, reused every frame, and disposed in OnExit. Never create a new batcher per draw call.
Use DeltaTime for movement
position += velocity * frameTime.DeltaTime;
Load assets in OnEnter
All asset loading should happen in OnEnter, not in OnUpdate.
Keep Update and Draw separate OnUpdate handles game logic. OnDraw handles rendering. They should not overlap.