History

CR feedback

Brad Hefta-Gaub committed 10 years ago

Commit 74a38351 parent 22619a61 file · +6 −18Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+6−18
@@ -839,34 +839,22 @@ void ScriptEngine::run() { · 838 unmodified lines
839839 // calculate a sleepUntil to be the time from our start time until the original target
840840 // sleepUntil for this frame.
841841 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);
843843
844844 // However, if our sleepUntil is not at least our average update time into the future
845845 // it means our script is taking too long in it's updates, and we want to punish the
846846 // script a little bit. So we will force the sleepUntil to be at least our averageUpdate
847847 // 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);
854850
855851 // We don't want to actually sleep for too long, because it causes our scripts to hang
856852 // on shutdown and stop... so we want to loop and sleep until we've spent our time in
857853 // purgatory, constantly checking to see if our script was asked to end
858− bool firstTime = true;
859854 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);
868857 std::this_thread::sleep_until(thisSleepUntil);
869− firstTime = false;
870858 }
871859
872860 #ifdef SCRIPT_DELAY_DEBUG
@@ -880,7 +868,7 @@ void ScriptEngine::run() { · 7 unmodified lines
880868 qCDebug(scriptengine) <<
881869 "Frame:" << thisFrame <<
882870 "Slept (us):" << std::chrono::duration_cast<std::chrono::microseconds>(actuallySleptUntil - beforeSleep).count() <<
883− "Avg Updates (us):" << avgerageUpdate.count() <<
871+ "Avg Updates (us):" << averageUpdate.count() <<
884872 "FPS:" << fps;
885873 }
886874 }