File indexing completed on 2024-10-13 03:43:43

0001 /*
0002     SPDX-FileCopyrightText: 2009 Ian Wadham <iandw.au@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef KGRTIMER_H
0008 #define KGRTIMER_H
0009 
0010 #include <QElapsedTimer>
0011 #include <QTimer>
0012 
0013 class KGrTimer : public QObject
0014 {
0015     Q_OBJECT
0016 public:
0017     explicit KGrTimer (QObject * parent, int pTick = 20, float pScale = 1.0);
0018     ~KGrTimer() override;
0019 
0020     void pause();
0021     void resume();
0022     void step();
0023     inline void setScale (const float pScale)
0024                          { scaledTime = (pScale * tickTime) + 0.5; }
0025 
0026 Q_SIGNALS:
0027     /**
0028      * This signal powers the whole game. KGrLevelPlayer connects it to its
0029      * tick() slot.
0030      *
0031      * @param missed       If true, the QTimer has missed one or more ticks, due
0032      *                     to overheads elsewhere in Qt or the O/S. The game
0033      *                     catches up on the missed signal(s) and the graphics
0034      *                     view avoids painting any sprites until the catchup
0035      *                     is complete, thus saving further overheads. The
0036      *                     sprites may "jump" a little when this happens, but
0037      *                     at least the game stays on-time in wall-clock time.
0038      * @param pScaledTime  The number of milliseconds per tick. Usually this is
0039      *                     tickTime (= 20 msec), but it is less when the game is
0040      *                     slowed down or more when it is speeded up. If the
0041      *                     scaled time is 10 (beginner speed), the game will
0042      *                     take 2 ticks of 20 msec (i.e. 40 msec) to do what it
0043      *                     normally does in 20 msec.
0044      */
0045     void tick (bool missed, int pScaledTime);
0046 
0047 private Q_SLOTS:
0048     void internalSlot();
0049 
0050 private:
0051     QElapsedTimer    t;
0052     QTimer * ticker;
0053     int      tickTime;
0054     int      scaledTime;
0055     int      tickCount;
0056     int      halfTick;
0057     int      expectedTime;
0058 };
0059 
0060 #endif // KGRTIMER_H