beetlebug▾
/

ztk::game loop

Starts the loop of the game at a fixed step, or changes its options.

ztk::game loop ?-update SCRIPT? ?-draw SCRIPT? ?-rate N? ?-budget UNITS? ?-maxsteps N? ?-pauseonblur BOOLEAN? ?-scale F? ?-time N?

Description

With options, sets them and starts the loop when it does not run. The steps start at the time 0. With no option, gives the options as a dict.

Each frame of the display adds the time since the last frame, and runs -update once for each whole step of that time, with the step in seconds appended. A late frame runs at most -maxsteps steps, and drops the rest. Then -draw runs once with the fraction of a step that is left (0 to 1) appended. The time comes from the frames of the backend. The loop uses no after timer. On the headless backend the clock moves one step at a time, so each run gives the same steps.

An error of a script, or a step over its budget, stops the loop and goes to bgerror. The app in the background, and with -pauseonblur 1 a focus loss of the view, pause the loop and send <<GamePause>> to the view. The return sends <<GameResume>>, and the loop stays paused until ztk::game resume.

Options

OptionTypeDefaultDescription
-updatescriptemptyThe script of each step, with the step in seconds appended.
-drawscriptemptyThe script of each frame, with the fraction of a step that is left appended.
-rateinteger60The steps in each second, from 1 to 1000.
-budgetinteger0The compute units of one run of the update script, or 0 for no budget.
-maxstepsinteger5The most steps in one frame, from 1 to 100.
-pauseonblurboolean0With 1, a focus loss of the view pauses the loop.
-scaledouble1.0The seconds of the game in each second of the clock, from 0 to 16.
-timeintegernoneSets the count of steps of ztk::game time.

-update

The script of each step, with the step in seconds appended.

Runs at the global level at each step. The game reads its actions and moves its items here.

% proc tick {dt} { set ::dt $dt }
> ztk::game loop -update tick -rate 50
> ztk::game step
> set ::dt
0.02

Any text is a script. An error of the script at a step goes to bgerror and stops the loop.

-draw

The script of each frame, with the fraction of a step that is left appended.

Runs once for each frame of the display, after the steps of the frame. A game that moves its items in -update needs no -draw.

% proc show {f} { set ::f $f }
> ztk::game loop -draw show
> ztk::game step
> set ::f
0.0

Any text is a script. An error of the script goes to bgerror and stops the loop.

-rate

The steps in each second, from 1 to 1000.

A step is 1/N seconds of the clock of the frames.

% ztk::game loop -rate 30
> dict get [ztk::game loop] -rate
30
% ztk::game loop -rate 0
expected a rate from 1 to 1000 but got "0"
-errorcode ZTK GAME RATE

-budget

The compute units of one run of the update script, or 0 for no budget.

A step that uses more stops the loop. bgerror gets the message the update step used its budget of N compute units with the error code ZTK GAME BUDGET, and the app stays live.

% proc bgerror {m} { set ::err $::errorCode }
> proc spin {dt} { while 1 {} }
> ztk::game loop -update spin -budget 1000
> ztk::game step
> list $::err [dict get [ztk::game info] running]
{ZTK GAME BUDGET} 0
% ztk::game loop -budget -1
expected a count of 0 or more but got "-1"
-errorcode ZTK GAME BUDGET

-maxsteps

The most steps in one frame, from 1 to 100.

A frame that comes late runs this many steps and drops the rest, so a long pause starts no run of catch-up steps. ztk::game info counts the dropped steps.

% ztk::game loop -update {} -rate 60 -maxsteps 2 -time 0
> ztk::clock set [expr {[ztk::clock get] + 1000}]
> update
> list [ztk::game time] [dict get [ztk::game info] dropped]
2 58
% ztk::game loop -maxsteps 0
expected a count from 1 to 100 but got "0"
-errorcode ZTK GAME MAXSTEPS

-pauseonblur

With 1, a focus loss of the view pauses the loop.

With 0, a focus loss lets go of each key that the actions hold, and the loop runs on. A hidden window pauses the loop with either value.

% ztk::game loop -pauseonblur 1
> dict get [ztk::game loop] -pauseonblur
1
% ztk::game loop -pauseonblur maybe
expected boolean value but got "maybe"
-errorcode TCL VALUE NUMBER

-scale

The seconds of the game in each second of the clock, from 0 to 16.

With 0.5 the game runs at half speed, and with 2 at twice the speed. The step that the update script gets stays 1/rate, so the game gives the same result for the same input at each scale: the scale changes how many steps run in a second. With 0, no step runs and the draw script runs at each frame, so a game can freeze its time and still draw.

% ztk::game loop -update {} -rate 100 -scale 0.5 -time 0
> ztk::clock set [expr {[ztk::clock get] + 40}]
> update
> list [ztk::game time] [dict get [ztk::game info] scale]
2 0.5
% ztk::game loop -scale 17
expected a scale from 0 to 16 but got "17"
-errorcode ZTK GAME SCALE

-time

Sets the count of steps of ztk::game time.

The count goes on over ztk::game stop and a new ztk::game loop, so a page that runs the loop only around a motion keeps one clock. -time 0 starts it again.

% ztk::game loop -update {} -scale 1 -time 0
> ztk::game step 3
> ztk::game stop
> ztk::game loop -update {}
> ztk::game step 2
> set a [ztk::game time]
> ztk::game loop -time 0
> list $a [ztk::game time]
5 0
% ztk::game loop -time -1
expected a count of 0 or more but got "-1"
-errorcode ZTK GAME TIME

Returns

An empty string, or the options as a dict.

Examples

% ztk::game loop -update {} -rate 50
> ztk::game loop
-update {} -draw {} -rate 50 -budget 0 -maxsteps 5 -pauseonblur 0 -scale 1.0

Errors

% ztk::game loop -speed 2
bad option "-speed": must be -budget, -draw, -maxsteps, -pauseonblur, -rate, -scale, -time, or -update
-errorcode TCL LOOKUP OPTION -speed

% ztk::game loop -rate
wrong # args: should be "ztk::game loop ?-option value ...?"
-errorcode TCL WRONGARGS

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztk 1.0.0-beta.7

See also