[Kde-games-devel] A timer class for games

Ian Wadham ianw2 at optusnet.com.au
Fri Jun 12 09:15:52 CEST 2009


During the re-write of KGoldrunner I have developed a timer
class that gives greater reliability to animation-timing and a
smoother overall animation effect.  I am wondering if it is worth
generalizing it into a KDE Games Library class for KDE 4.4,
i.e. whether other games could find it useful.

Although its accuracy cannot be better than QTimer's, which
is to say QObject's accuracy, it checks its performance against
real time (wall-clock time) and compensates for "beats" that
QTimer misses, i.e. if you ask for a beat every 20 msec, you
get 50 beats/second, no matter what the Qt library decides to
do, although not all beats are exactly on time and some are
rapidly-executed catch-ups.

Other bonuses are that it implements pause(), resume() and
step() methods, to accuracy within one beat, so if you are running
a game off a single timer, it gives you instant pause/resume.
And the step() method is very handy for debugging animation.

There is also a setScale() method which is useful for speeding
up or slowing down the whole game.  The tick() signal emitted
by the timer has parameters that tell you whether a beat has been
missed (by QObject) and what the scaled time of the tick is.

In KGoldrunner, I use the "missed" parameter to skip an animation
frame in the graphics, while keeping the internal model and the
overall motion (distance travelled per second) on time.  That
should relieve the load on the CPU and graphics and, at worst, it
causes an occasional flickering, which is not as bad as it already
was (all the time) in KDE 3 and KDE 4.0 to 4.2.

The code is in kgoldrunner/src/kgrtimer.h and
kgoldrunner/src/kgrtimer.cpp, but it needs polishing.

All the best, Ian W.


More information about the kde-games-devel mailing list