Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 4 files). Show all
Unified · Split
| @@ -828,24 +828,42 @@ void ScriptEngine::run() { · 827 unmodified lines | |||
| 828 | 828 | ||
| 829 | _lastUpdate = usecTimestampNow(); | 829 | _lastUpdate = usecTimestampNow(); |
| 830 | 830 | ||
| 831 | qint64 totalSleepFor = 0; | ||
| 832 | std::chrono::microseconds totalUpdates; | ||
| 833 | auto lastLoopStart = clock::now(); | ||
| 834 | |||
| 831 | // TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine | 835 | // TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine |
| 832 | while (!_isFinished) { | 836 | while (!_isFinished) { |
| 837 | auto thisLoopStart = clock::now(); | ||
| 838 | |||
| 833 | // Throttle to SCRIPT_FPS | 839 | // Throttle to SCRIPT_FPS |
| 834 | const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1); | 840 | const std::chrono::microseconds FRAME_DURATION(USECS_PER_SECOND / SCRIPT_FPS + 1); |
| 841 | const std::chrono::microseconds MINIMUM_SLEEP { FRAME_DURATION / 2 }; | ||
| 842 | |||
| 843 | auto beforeSleep = clock::now(); | ||
| 835 | clock::time_point sleepTime(startTime + thisFrame++ * FRAME_DURATION); | 844 | clock::time_point sleepTime(startTime + thisFrame++ * FRAME_DURATION); |
| 845 | auto wouldSleep = (sleepTime - clock::now()); | ||
| 846 | auto avgUpdates = totalUpdates / thisFrame; | ||
| 847 | |||
| 848 | if (wouldSleep < avgUpdates) { | ||
| 849 | sleepTime = beforeSleep + avgUpdates; | ||
| 850 | } | ||
| 851 | |||
| 836 | std::this_thread::sleep_until(sleepTime); | 852 | std::this_thread::sleep_until(sleepTime); |
| 837 | 853 | ||
| 838 | #ifdef SCRIPT_DELAY_DEBUG | 854 | #ifdef SCRIPT_DELAY_DEBUG |
| 839 | { | 855 | { |
| 840 | auto now = clock::now(); | 856 | auto sleptTill = clock::now(); |
| 841 | uint64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(now - startTime).count(); | 857 | uint64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(sleptTill - startTime).count(); |
| 842 | if (seconds > 0) { // avoid division by zero and time travel | 858 | if (seconds > 0) { // avoid division by zero and time travel |
| 843 | uint64_t fps = thisFrame / seconds; | 859 | uint64_t fps = thisFrame / seconds; |
| 844 | // Overreporting artificially reduces the reported rate | 860 | // Overreporting artificially reduces the reported rate |
| 845 | if (thisFrame % SCRIPT_FPS == 0) { | 861 | if (thisFrame % SCRIPT_FPS == 0) { |
| 846 | qCDebug(scriptengine) << | 862 | qCDebug(scriptengine) << |
| 847 | "Frame:" << thisFrame << | 863 | "Frame:" << thisFrame << |
| 848 | "Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(now - sleepTime).count() << | 864 | "Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(sleptTill - beforeSleep).count() << |
| 865 | "Avg Updates (us):" << avgUpdates.count() << | ||
| 866 | "Last loop time (us):" << std::chrono::duration_cast<std::chrono::microseconds>(thisLoopStart - lastLoopStart).count() << | ||
| 849 | "FPS:" << fps; | 867 | "FPS:" << fps; |
| 850 | } | 868 | } |
| 851 | } | 869 | } |
| @@ -874,16 +892,21 @@ void ScriptEngine::run() { · 22 unmodified lines | |||
| 874 | qint64 now = usecTimestampNow(); | 892 | qint64 now = usecTimestampNow(); |
| 875 | 893 | ||
| 876 | // we check for 'now' in the past in case people set their clock back | 894 | // we check for 'now' in the past in case people set their clock back |
| 877 | if (_lastUpdate < now) { | 895 | if (_isPhysicsEnabledFunc() && _lastUpdate < now) { |
| 878 | float deltaTime = (float) (now - _lastUpdate) / (float) USECS_PER_SECOND; | 896 | float deltaTime = (float) (now - _lastUpdate) / (float) USECS_PER_SECOND; |
| 879 | if (!_isFinished) { | 897 | if (!_isFinished) { |
| 898 | auto preUpdate = clock::now(); | ||
| 880 | emit update(deltaTime); | 899 | emit update(deltaTime); |
| 900 | auto postUpdate = clock::now(); | ||
| 901 | auto elapsed = (postUpdate - preUpdate); | ||
| 902 | totalUpdates += std::chrono::duration_cast<std::chrono::microseconds>(elapsed); | ||
| 881 | } | 903 | } |
| 882 | } | 904 | } |
| 883 | _lastUpdate = now; | 905 | _lastUpdate = now; |
| 884 | 906 | ||
| 885 | // Debug and clear exceptions | 907 | // Debug and clear exceptions |
| 886 | hadUncaughtExceptions(*this, _fileNameString); | 908 | hadUncaughtExceptions(*this, _fileNameString); |
| 909 | lastLoopStart = thisLoopStart; | ||
| 887 | } | 910 | } |
| 888 | 911 | ||
| 889 | qCDebug(scriptengine) << "Script Engine stopping:" << getFilename(); | 912 | qCDebug(scriptengine) << "Script Engine stopping:" << getFilename(); |