Datasheet

THE HEARTBEAT TIMER 47
The solution is to use a single timer object to provide a game ‘heartbeat.’
All elements of the game that require a particular time-based activity use
the heartbeat to synchronize themselves. The timer fires as many times
per second as necessary to achieve the desired frame rate. Each time the
timer fires, it runs the game loop once, and increments a stored value
known as the tick count. The tick count is used to provide the time
elapsed to the rest of the game.
The resulting pseudocode for the game loop looks as follows, where
TimerEventHandler() is the method called once every heartbeat to
handle the timer event:
TimerEventHandler()
{
calculate the time elapsed and retrieve user input
update game internals
update graphics (prepare frame, draw frame)
synchronize sound
}
One of the benefits of using a single heartbeat timer is that the beat
rate can easily be slowed down for debugging purposes, stopped entirely
when the game needs to pause, or tweaked to modify the frame rate when
the graphics performance differs across a range of devices with different
capabilities.
The CPeriodic timer is used most commonly to drive a game loop.
It uses a callback function to execute the game loop on each heartbeat.
CPeriodic is the simplest class to use to provide a timer without coming
into direct contact with active objects because it hides the details of the
active object framework (it derives from CTimer an active object-
based timer class and implements the active object methods RunL()
and DoCancel() for its callers). The
Skeleton
example uses this class,
as shown below. However, if you already use a number of active objects
in your game, you may simply prefer to use the CTimer class directly.
void CSkeletonAppView::ConstructL( const TRect& aRect )
{
...
// Create the heartbeat timer for the game loop.
iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
// Activate the window (calls StartHeartbeat())
ActivateL();
}
void CSkeletonAppView::StartHeartbeat()
{
// Start the CPeriodic timer, passing the Tick() method for callback
if (!iPeriodicTimer->IsActive())
iPeriodicTimer->Start(KFramesPerSecond, KFramesPerSecond,
TCallBack(CSkeletonAppView::Tick, this));
}