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 | 829 | _lastUpdate = usecTimestampNow(); |
| 830 | 830 | |
| 831 | + qint64 totalSleepFor = 0; | |
| 832 | + std::chrono::microseconds totalUpdates; | |
| 833 | + auto lastLoopStart = clock::now(); | |
| 834 | + | |
| 831 | 835 | // TODO: Integrate this with signals/slots instead of reimplementing throttling for ScriptEngine |
| 832 | 836 | while (!_isFinished) { |
| 837 | + auto thisLoopStart = clock::now(); | |
| 838 | + | |
| 833 | 839 | // Throttle to SCRIPT_FPS |
| 834 | 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 | 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 | 852 | std::this_thread::sleep_until(sleepTime); |
| 837 | 853 | |
| 838 | 854 | #ifdef SCRIPT_DELAY_DEBUG |
| 839 | 855 | { |
| 840 | − auto now = clock::now(); | |
| 841 | − uint64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(now - startTime).count(); | |
| 856 | + auto sleptTill = clock::now(); | |
| 857 | + uint64_t seconds = std::chrono::duration_cast<std::chrono::seconds>(sleptTill - startTime).count(); | |
| 842 | 858 | if (seconds > 0) { // avoid division by zero and time travel |
| 843 | 859 | uint64_t fps = thisFrame / seconds; |
| 844 | 860 | // Overreporting artificially reduces the reported rate |
| 845 | 861 | if (thisFrame % SCRIPT_FPS == 0) { |
| 846 | 862 | qCDebug(scriptengine) << |
| 847 | 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 | 867 | "FPS:" << fps; |
| 850 | 868 | } |
| 851 | 869 | } |
| @@ -874,16 +892,21 @@ void ScriptEngine::run() { · 22 unmodified lines | ||
| 874 | 892 | qint64 now = usecTimestampNow(); |
| 875 | 893 | |
| 876 | 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 | 896 | float deltaTime = (float) (now - _lastUpdate) / (float) USECS_PER_SECOND; |
| 879 | 897 | if (!_isFinished) { |
| 898 | + auto preUpdate = clock::now(); | |
| 880 | 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 | 905 | _lastUpdate = now; |
| 884 | 906 | |
| 885 | 907 | // Debug and clear exceptions |
| 886 | 908 | hadUncaughtExceptions(*this, _fileNameString); |
| 909 | + lastLoopStart = thisLoopStart; | |
| 887 | 910 | } |
| 888 | 911 | |
| 889 | 912 | qCDebug(scriptengine) << "Script Engine stopping:" << getFilename(); |