| 142 | } |
| 143 | |
| 144 | void |
| 145 | Timer::waitUntilNextFrameIsDue () |
| 146 | { |
| 147 | if (playState != ePlayStateRunning) { |
| 148 | // |
| 149 | // If we are not running, reset all timing state |
| 150 | // variables and return without waiting. |
| 151 | // |
| 152 | |
| 153 | gettimeofday (&_lastFrameTime, 0); |
| 154 | _timingError = 0; |
| 155 | _lastFpsFrameTime = _lastFrameTime; |
| 156 | _framesSinceLastFpsFrame = 0; |
| 157 | |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | double spf; |
| 163 | { |
| 164 | QMutexLocker l(&_mutex); |
| 165 | spf = _spf; |
| 166 | } |
| 167 | // |
| 168 | // If less than _spf seconds have passed since the last frame |
| 169 | // was displayed, sleep until exactly _spf seconds have gone by. |
| 170 | // |
| 171 | timeval now; |
| 172 | gettimeofday (&now, 0); |
| 173 | |
| 174 | double timeSinceLastFrame = now.tv_sec - _lastFrameTime.tv_sec + |
| 175 | (now.tv_usec - _lastFrameTime.tv_usec) * 1e-6f; |
| 176 | if (timeSinceLastFrame < 0) { |
| 177 | timeSinceLastFrame = 0; |
| 178 | } |
| 179 | double timeToSleep = spf - timeSinceLastFrame - _timingError; |
| 180 | |
| 181 | #ifdef _WIN32 |
| 182 | |
| 183 | if (timeToSleep > 0) { |
| 184 | Sleep ( int (timeToSleep * 1000.0f) ); |
| 185 | } |
| 186 | |
| 187 | #else |
| 188 | |
| 189 | if (timeToSleep > 0) { |
| 190 | timespec ts; |
| 191 | ts.tv_sec = (time_t) timeToSleep; |
| 192 | ts.tv_nsec = (long) ( (timeToSleep - ts.tv_sec) * 1e9f ); |
| 193 | nanosleep (&ts, 0); |
| 194 | } |
| 195 | |
| 196 | #endif |
| 197 | |
| 198 | // |
| 199 | // If we slept, it is possible that we woke up a little too early |
| 200 | // or a little too late. Keep track of the difference between |
| 201 | // now and the exact time when we wanted to wake up; next time |
no test coverage detected