diff --git a/docs/changelog.txt b/docs/changelog.txt index 2f8e286e13..7571a43be2 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -65,6 +65,7 @@ Template for new versions: ## Documentation ## API +- ``Core::getUnpausedMs``: new API for getting unpaused ms since load in a fort-mode game ## Lua diff --git a/library/Core.cpp b/library/Core.cpp index 94a94d748c..9dc2d2e68a 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -145,20 +145,20 @@ bool PerfCounters::getIgnorePauseState() { return ignore_pause_state; } -void PerfCounters::registerTick(uint32_t baseline_ms) { +uint32_t PerfCounters::registerTick(uint32_t baseline_ms) { if (!World::isFortressMode() || World::ReadPauseState()) { last_tick_baseline_ms = 0; - return; + return 0; } // only update when the tick counter has advanced if (!world || last_frame_counter == world->frame_counter) - return; + return 0; last_frame_counter = world->frame_counter; if (last_tick_baseline_ms == 0) { last_tick_baseline_ms = baseline_ms; - return; + return 0; } uint32_t elapsed_ms = baseline_ms - last_tick_baseline_ms; @@ -173,6 +173,8 @@ void PerfCounters::registerTick(uint32_t baseline_ms) { recent_ticks.history[recent_ticks.head_idx] = elapsed_ms; recent_ticks.sum_ms += elapsed_ms; + + return elapsed_ms; } uint32_t PerfCounters::getUnpausedFps() { @@ -1705,6 +1707,7 @@ bool Core::InitMainThread() { } perf_counters.reset(); + unpaused_ms = 0; return true; } @@ -2141,7 +2144,7 @@ int Core::Update() } uint32_t start_ms = p->getTickCount(); - perf_counters.registerTick(start_ms); + unpaused_ms += perf_counters.registerTick(start_ms); doUpdate(out); perf_counters.incCounter(perf_counters.total_update_ms, start_ms); } @@ -2337,6 +2340,7 @@ void Core::onStateChange(color_ostream &out, state_change_event event) case SC_WORLD_LOADED: { perf_counters.reset(); + unpaused_ms = 0; Persistence::Internal::load(out); plug_mgr->doLoadWorldData(out); loadModScriptPaths(out); diff --git a/library/include/Core.h b/library/include/Core.h index 8725c1faeb..556e5e1b03 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -100,9 +100,11 @@ namespace DFHack bool getIgnorePauseState(); // noop if game is paused and getIgnorePauseState() returns false - void incCounter(uint32_t &perf_counter, uint32_t baseline_ms); + void incCounter(uint32_t &counter, uint32_t baseline_ms); + + // returns number of unpaused ms since last tick + uint32_t registerTick(uint32_t baseline_ms); - void registerTick(uint32_t baseline_ms); uint32_t getUnpausedFps(); private: @@ -219,6 +221,7 @@ namespace DFHack static void cheap_tokenise(std::string const& input, std::vector &output); PerfCounters perf_counters; + uint32_t getUnpausedMs() { return unpaused_ms; } lua_State* getLuaState(bool bypass_assertion = false) { assert(bypass_assertion || isSuspended()); @@ -334,6 +337,8 @@ namespace DFHack lua_State* State; + uint32_t unpaused_ms; // reset to 0 on map load + friend class CoreService; friend class ServerConnection; friend class CoreSuspender;