run method
Run this game.
You can set the frames per second with the framesPerSecond
parameter.
Note: Once set, FPS cannot currently be changed.
If you want to run some code when the game has started, but before the
main loop starts, use the onStart
parameter.
The onStart
function will be called after the window has been created,
so it's perfectly fine to start pushing levels at that point.
Before this parameter was introduced, it was necessary to use registerTask, which wasn't quite as reliable, due to how fast windows are created on some machines.
Implementation
@mustCallSuper
Future<void> run({
final int framesPerSecond = 60,
final TaskFunction? onStart,
}) async {
final tickEvery = 1000 ~/ framesPerSecond;
_window = sdl.createWindow(title);
var lastTick = 0;
_isRunning = true;
if (onStart != null) {
onStart();
}
while (_isRunning == true) {
final int timeDelta;
final ticks = sdl.ticks;
if (lastTick == 0) {
timeDelta = 0;
lastTick = 0;
} else {
timeDelta = ticks - lastTick;
}
await tick(timeDelta);
lastTick = sdl.ticks;
final tickTime = lastTick - ticks;
if (tickEvery > tickTime) {
await Future<void>.delayed(
Duration(milliseconds: tickEvery - tickTime),
);
}
}
destroy();
}