Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all
Unified · Split
| @@ -839,34 +839,22 @@ void ScriptEngine::run() { · 838 unmodified lines | ||
| 839 | 839 | // calculate a sleepUntil to be the time from our start time until the original target |
| 840 | 840 | // sleepUntil for this frame. |
| 841 | 841 | const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1); |
| 842 | − clock::time_point sleepUntil(startTime + thisFrame++ * FRAME_DURATION); | |
| 842 | + clock::time_point targetSleepUntil(startTime + thisFrame++ * FRAME_DURATION); | |
| 843 | 843 | |
| 844 | 844 | // However, if our sleepUntil is not at least our average update time into the future |
| 845 | 845 | // it means our script is taking too long in it's updates, and we want to punish the |
| 846 | 846 | // script a little bit. So we will force the sleepUntil to be at least our averageUpdate |
| 847 | 847 | // time into the future. |
| 848 | − auto wouldSleep = (sleepUntil - beforeSleep); | |
| 849 | − auto avgerageUpdate = totalUpdates / thisFrame; | |
| 850 | − | |
| 851 | − if (wouldSleep < avgerageUpdate) { | |
| 852 | − sleepUntil = beforeSleep + avgerageUpdate; | |
| 853 | − } | |
| 848 | + auto averageUpdate = totalUpdates / thisFrame; | |
| 849 | + auto sleepUntil = std::max(targetSleepUntil, beforeSleep + averageUpdate); | |
| 854 | 850 | |
| 855 | 851 | // We don't want to actually sleep for too long, because it causes our scripts to hang |
| 856 | 852 | // on shutdown and stop... so we want to loop and sleep until we've spent our time in |
| 857 | 853 | // purgatory, constantly checking to see if our script was asked to end |
| 858 | − bool firstTime = true; | |
| 859 | 854 | while (!_isFinished && clock::now() < sleepUntil) { |
| 860 | − if (!firstTime) { | |
| 861 | − QCoreApplication::processEvents(); // before we sleep again, give events a chance to process | |
| 862 | − } | |
| 863 | − auto wouldSleepSlice = (sleepUntil - clock::now()); | |
| 864 | − auto thisSleepUntil = sleepUntil; | |
| 865 | − if (wouldSleepSlice > FRAME_DURATION) { | |
| 866 | − thisSleepUntil = clock::now() + FRAME_DURATION; | |
| 867 | − } | |
| 855 | + QCoreApplication::processEvents(); // before we sleep again, give events a chance to process | |
| 856 | + auto thisSleepUntil = std::min(sleepUntil, clock::now() + FRAME_DURATION); | |
| 868 | 857 | std::this_thread::sleep_until(thisSleepUntil); |
| 869 | − firstTime = false; | |
| 870 | 858 | } |
| 871 | 859 | |
| 872 | 860 | #ifdef SCRIPT_DELAY_DEBUG |
| @@ -880,7 +868,7 @@ void ScriptEngine::run() { · 7 unmodified lines | ||
| 880 | 868 | qCDebug(scriptengine) << |
| 881 | 869 | "Frame:" << thisFrame << |
| 882 | 870 | "Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(actuallySleptUntil - beforeSleep).count() << |
| 883 | − "Avg Updates (us):" << avgerageUpdate.count() << | |
| 871 | + "Avg Updates (us):" << averageUpdate.count() << | |
| 884 | 872 | "FPS:" << fps; |
| 885 | 873 | } |
| 886 | 874 | } |