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