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 #include "kgrtimer.h"
0008 
0009 #include "kgoldrunner_debug.h"
0010 
0011 KGrTimer::KGrTimer (QObject * parent, int pTickTime, float pScale)
0012     :
0013     QObject      (parent),
0014     ticker       (new QTimer (parent)),
0015     tickTime     (pTickTime),
0016     tickCount    (0),
0017     halfTick     (pTickTime / 2),
0018     expectedTime (0)
0019 {
0020     setScale (pScale);
0021     connect(ticker, &QTimer::timeout, this, &KGrTimer::internalSlot);
0022     ticker->start (tickTime);
0023     t.start();
0024 }
0025 
0026 KGrTimer::~KGrTimer()
0027 {
0028     ticker->stop();
0029 }
0030 
0031 void KGrTimer::pause()
0032 {
0033     ticker->stop();
0034 }
0035 
0036 void KGrTimer::resume()
0037 {
0038     ticker->start (tickTime);
0039     t.start();
0040     expectedTime = 0;
0041 }
0042 
0043 void KGrTimer::step()
0044 {
0045     tickCount++;
0046     expectedTime = expectedTime + tickTime;
0047     Q_EMIT tick (false, scaledTime);
0048 }
0049 
0050 void KGrTimer::internalSlot()
0051 {
0052     // Check whether the QTimer::timeout() signal is on-time.
0053     int timeOnClock = t.elapsed();
0054 
0055     // If the signal is too early, ignore it.  If it is on-time +/-halfTick,
0056     // trigger an internal signal.  If it is late, trigger more internal
0057     // signals, in order to "catch up".
0058     
0059     while (timeOnClock > (expectedTime + halfTick)) {
0060         tickCount++;
0061         expectedTime = expectedTime + tickTime;
0062         Q_EMIT tick ((timeOnClock >= (expectedTime + tickTime)), scaledTime);
0063     }
0064 }
0065 
0066 #include "moc_kgrtimer.cpp"