mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-29 20:42:56 +02:00
fix unix warning, added comments
This commit is contained in:
parent
83dc9ea6bb
commit
a12034cb45
1 changed files with 18 additions and 17 deletions
|
@ -828,42 +828,44 @@ void ScriptEngine::run() {
|
||||||
|
|
||||||
_lastUpdate = usecTimestampNow();
|
_lastUpdate = usecTimestampNow();
|
||||||
|
|
||||||
qint64 totalSleepFor = 0;
|
|
||||||
std::chrono::microseconds totalUpdates;
|
std::chrono::microseconds totalUpdates;
|
||||||
auto lastLoopStart = clock::now();
|
|
||||||
|
|
||||||
// TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine
|
// TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine
|
||||||
while (!_isFinished) {
|
while (!_isFinished) {
|
||||||
auto thisLoopStart = clock::now();
|
auto beforeSleep = clock::now();
|
||||||
|
|
||||||
// Throttle to SCRIPT_FPS
|
// Throttle to SCRIPT_FPS
|
||||||
|
// We'd like to try to keep the script at a solid SCRIPT_FPS update rate. And so we will
|
||||||
|
// calculate a sleepUntil to be the time from our start time until the original target
|
||||||
|
// sleepUntil for this frame.
|
||||||
const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1);
|
const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1);
|
||||||
const std::chrono::microseconds MINIMUM_SLEEP { FRAME_DURATION / 2 };
|
clock::time_point sleepUntil(startTime + thisFrame++ * FRAME_DURATION);
|
||||||
|
|
||||||
auto beforeSleep = clock::now();
|
// However, if our sleepUntil is not at least our average update time into the future
|
||||||
clock::time_point sleepTime(startTime + thisFrame++ * FRAME_DURATION);
|
// it means our script is taking too long in it's updates, and we want to punish the
|
||||||
auto wouldSleep = (sleepTime - clock::now());
|
// script a little bit. So we will force the sleepUntil to be at least our averageUpdate
|
||||||
auto avgUpdates = totalUpdates / thisFrame;
|
// time into the future.
|
||||||
|
auto wouldSleep = (sleepUntil - clock::now());
|
||||||
|
auto avgerageUpdate = totalUpdates / thisFrame;
|
||||||
|
|
||||||
if (wouldSleep < avgUpdates) {
|
if (wouldSleep < avgerageUpdate) {
|
||||||
sleepTime = beforeSleep + avgUpdates;
|
sleepUntil = beforeSleep + avgerageUpdate;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::this_thread::sleep_until(sleepTime);
|
std::this_thread::sleep_until(sleepUntil);
|
||||||
|
|
||||||
#ifdef SCRIPT_DELAY_DEBUG
|
#ifdef SCRIPT_DELAY_DEBUG
|
||||||
{
|
{
|
||||||
auto sleptTill = clock::now();
|
auto actuallySleptUntil = clock::now();
|
||||||
uint64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(sleptTill - startTime).count();
|
uint64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(actuallySleptUntil - startTime).count();
|
||||||
if (seconds > 0) { // avoid division by zero and time travel
|
if (seconds > 0) { // avoid division by zero and time travel
|
||||||
uint64_t fps = thisFrame / seconds;
|
uint64_t fps = thisFrame / seconds;
|
||||||
// Overreporting artificially reduces the reported rate
|
// Overreporting artificially reduces the reported rate
|
||||||
if (thisFrame % SCRIPT_FPS == 0) {
|
if (thisFrame % SCRIPT_FPS == 0) {
|
||||||
qCDebug(scriptengine) <<
|
qCDebug(scriptengine) <<
|
||||||
"Frame:" << thisFrame <<
|
"Frame:" << thisFrame <<
|
||||||
"Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(sleptTill - beforeSleep).count() <<
|
"Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(actuallySleptUntil - beforeSleep).count() <<
|
||||||
"Avg Updates (us):" << avgUpdates.count() <<
|
"Avg Updates (us):" << avgerageUpdate.count() <<
|
||||||
"Last loop time (us):" << std::chrono::duration_cast<std::chrono::microseconds>(thisLoopStart - lastLoopStart).count() <<
|
|
||||||
"FPS:" << fps;
|
"FPS:" << fps;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -906,7 +908,6 @@ void ScriptEngine::run() {
|
||||||
|
|
||||||
// Debug and clear exceptions
|
// Debug and clear exceptions
|
||||||
hadUncaughtExceptions(*this, _fileNameString);
|
hadUncaughtExceptions(*this, _fileNameString);
|
||||||
lastLoopStart = thisLoopStart;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(scriptengine) << "Script Engine stopping:" << getFilename();
|
qCDebug(scriptengine) << "Script Engine stopping:" << getFilename();
|
||||||
|
|
Loading…
Reference in a new issue