diff options
author | Vittorio Romeo <vittorio.romeo@outlook.com> | 2020-07-15 14:10:30 +0100 |
---|---|---|
committer | Vittorio Romeo <vittorio.romeo@outlook.com> | 2020-07-15 14:10:30 +0100 |
commit | 5ec369c27be9aa8a3e6460be1335ca13a1168d90 (patch) | |
tree | d9d073664bf170ae95d066beef5b0245516e5b39 | |
parent | fb0bfdf241405333e1cfc232ffdae09d1935ac15 (diff) |
Frametime-based timerframetime_fixes
-rw-r--r-- | _RELEASE/Packs/cube/Scripts/Levels/babysteps.lua | 2 | ||||
-rw-r--r-- | include/SSVOpenHexagon/Core/HGStatus.hpp | 22 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/HGGraphics.cpp | 4 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/HGStatus.cpp | 79 | ||||
-rw-r--r-- | src/SSVOpenHexagon/Core/HGUpdate.cpp | 5 |
5 files changed, 15 insertions, 97 deletions
diff --git a/_RELEASE/Packs/cube/Scripts/Levels/babysteps.lua b/_RELEASE/Packs/cube/Scripts/Levels/babysteps.lua index 9452f7ae..9e8f864e 100644 --- a/_RELEASE/Packs/cube/Scripts/Levels/babysteps.lua +++ b/_RELEASE/Packs/cube/Scripts/Levels/babysteps.lua @@ -54,7 +54,7 @@ function onLoad() m_messageAddImportant("well done!", 130) m_messageAddImportant("now play some real levels!", 138) - e_eventWaitUntilS(45) + e_eventWaitUntilS(44) -- not 45, to avoid level up u_eventKill() end diff --git a/include/SSVOpenHexagon/Core/HGStatus.hpp b/include/SSVOpenHexagon/Core/HGStatus.hpp index cb1bc8f2..7913d6f7 100644 --- a/include/SSVOpenHexagon/Core/HGStatus.hpp +++ b/include/SSVOpenHexagon/Core/HGStatus.hpp @@ -20,26 +20,11 @@ public: using TimePoint = std::chrono::time_point<Clock>; private: - // Current time of the level - TimePoint currentTp; - - // When we started playing the level - TimePoint levelStartTp; - - // When the last increment happened - TimePoint lastIncrementTp; - - // When the timer was last paused, and for how long - TimePoint lastTimerPauseTp; - - // Duration of the current timer pause - std::chrono::milliseconds pauseDuration{100}; - - // Frametime accumulators double totalFrametimeAccumulator{}; // Total time (including pauses) double playedFrametimeAccumulator{}; // Played time (no pauses) double pausedFrametimeAccumulator{}; // Paused time (only pauses) - double currentPause{}; // Current pause time + double currentPause{0.1 * 60}; // Current pause time + double currentIncrementTime{}; // Time since last increment public: float pulse{75}; @@ -86,9 +71,6 @@ public: // Reset the increment time to the last non-pause time point void resetIncrementTime() noexcept; - // Update the timer (called every frame) - void updateTime() noexcept; - // Accumulate the time spent in a frame into the total void accumulateFrametime(const double ft) noexcept; diff --git a/src/SSVOpenHexagon/Core/HGGraphics.cpp b/src/SSVOpenHexagon/Core/HGGraphics.cpp index 56bd9b82..99637768 100644 --- a/src/SSVOpenHexagon/Core/HGGraphics.cpp +++ b/src/SSVOpenHexagon/Core/HGGraphics.cpp @@ -183,10 +183,6 @@ void HexagonGame::updateText() if(Config::getDebug()) { os << "DEBUG MODE\n"; - os << toStr( - std::floor(status.getPlayedAccumulatedFrametimeInSeconds() * 1000) / - 1000.f) - << '\n'; } if(status.started) diff --git a/src/SSVOpenHexagon/Core/HGStatus.cpp b/src/SSVOpenHexagon/Core/HGStatus.cpp index 9750201e..75892336 100644 --- a/src/SSVOpenHexagon/Core/HGStatus.cpp +++ b/src/SSVOpenHexagon/Core/HGStatus.cpp @@ -9,19 +9,14 @@ namespace hg { -template <typename Duration> -[[nodiscard]] static auto toMilliseconds(const Duration d) noexcept -{ - return std::chrono::duration_cast<std::chrono::milliseconds>(d); -} - void HexagonGameStatus::start() noexcept { // Reset everything to the current time: - currentTp = Clock::now(); - levelStartTp = currentTp; - lastIncrementTp = currentTp; - lastTimerPauseTp = currentTp; + totalFrametimeAccumulator = 0.0; + playedFrametimeAccumulator = 0.0; + pausedFrametimeAccumulator = 0.0; + currentPause = 0.1 * 60; + currentIncrementTime = 0.0; // Signal that we started: started = true; @@ -29,22 +24,12 @@ void HexagonGameStatus::start() noexcept [[nodiscard]] double HexagonGameStatus::getIncrementTimeSeconds() noexcept { - // If we are paused, do not count passing time towards an increment: - const auto ms = - toMilliseconds(isTimePaused() ? lastTimerPauseTp - lastIncrementTp - : currentTp - lastIncrementTp); - - return static_cast<double>(ms.count()) / 1000.0; + return currentIncrementTime / 60.0; } [[nodiscard]] double HexagonGameStatus::getTimeSeconds() noexcept { - // If we are paused, do not count passing time as significant: - const auto ms = - toMilliseconds(isTimePaused() ? lastTimerPauseTp - levelStartTp - : currentTp - levelStartTp); - - return static_cast<double>(ms.count()) / 1000.0; + return getPlayedAccumulatedFrametimeInSeconds(); } [[nodiscard]] HexagonGameStatus::TimePoint @@ -52,8 +37,6 @@ HexagonGameStatus::getCurrentTP() noexcept { return HexagonGameStatus::TimePoint{std::chrono::milliseconds{ (int64_t)(getTotalAccumulatedFrametimeInSeconds() * 1000.0)}}; - - return currentTp; } [[nodiscard]] HexagonGameStatus::TimePoint @@ -61,72 +44,33 @@ HexagonGameStatus::getTimeTP() noexcept { return HexagonGameStatus::TimePoint{std::chrono::milliseconds{ (int64_t)(getPlayedAccumulatedFrametimeInSeconds() * 1000.0)}}; - - // If we are paused, do not count passing time as significant: - return isTimePaused() ? lastTimerPauseTp : currentTp; } [[nodiscard]] HexagonGameStatus::TimePoint HexagonGameStatus::getLevelStartTP() noexcept { return HexagonGameStatus::TimePoint{}; - - // If we are paused, do not count passing time as significant: - return levelStartTp; } [[nodiscard]] bool HexagonGameStatus::isTimePaused() noexcept { - return pauseDuration > (currentTp - lastTimerPauseTp); + return currentPause > 0.0; } void HexagonGameStatus::pauseTime(const double seconds) noexcept { currentPause += seconds * 60.0; - - const auto ms = - std::chrono::milliseconds(static_cast<int>(seconds * 1000.0)); - - // If we are paused, add to the current (in progress) pause duration: - if(isTimePaused()) - { - pauseDuration += ms; - return; - } - - // Otherwise, start a new pause: - lastTimerPauseTp = currentTp; - pauseDuration = ms; } void HexagonGameStatus::resetIncrementTime() noexcept { - // If we are paused, use the last time point before pausing as our next - // starting point for an increment. Otherwise, use the current time: - lastIncrementTp = isTimePaused() ? lastTimerPauseTp : currentTp; -} - -void HexagonGameStatus::updateTime() noexcept -{ - const bool wasPaused = isTimePaused(); - - currentTp = HexagonGameStatus::Clock::now(); - - // If we were paused on last frame, but we just stopped being paused... - if(wasPaused && !isTimePaused()) - { - // ...subtract the pause duration from the total time, so that the time - // returned by `getTimeSeconds()` is still correct: - levelStartTp += pauseDuration; - - // ...signal that we are not in a pause anymore: - lastTimerPauseTp = levelStartTp; - pauseDuration = std::chrono::milliseconds{0}; - } + currentIncrementTime = 0.0; } void HexagonGameStatus::accumulateFrametime(const double ft) noexcept { + // TODO: double-check what to do with remainder + totalFrametimeAccumulator += ft; // double pauseRemainder = 0.0; @@ -143,6 +87,7 @@ void HexagonGameStatus::accumulateFrametime(const double ft) noexcept else { playedFrametimeAccumulator += ft; + currentIncrementTime += ft; // playedFrametimeAccumulator += pauseRemainder; } } diff --git a/src/SSVOpenHexagon/Core/HGUpdate.cpp b/src/SSVOpenHexagon/Core/HGUpdate.cpp index a57b098f..ed3f8daa 100644 --- a/src/SSVOpenHexagon/Core/HGUpdate.cpp +++ b/src/SSVOpenHexagon/Core/HGUpdate.cpp @@ -161,12 +161,7 @@ void HexagonGame::update(ssvu::FT mFT) cwManager.cleanup(); updateEvents(mFT); - status.updateTime(); - - // ---------------------------------------------------------------- - // Accumulate played time into status: status.accumulateFrametime(mFT); - updateIncrement(); if(mustChangeSides && walls.empty()) |