Skip to content

Commit a556886

Browse files
authored
Merge pull request #5238 from myk002/myk_unpaused_ms
add Core API for getting unpaused ms
2 parents ab791e8 + 38e91d5 commit a556886

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Template for new versions:
6565
## Documentation
6666

6767
## API
68+
- ``Core::getUnpausedMs``: new API for getting unpaused ms since load in a fort-mode game
6869

6970
## Lua
7071

library/Core.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,20 @@ bool PerfCounters::getIgnorePauseState() {
145145
return ignore_pause_state;
146146
}
147147

148-
void PerfCounters::registerTick(uint32_t baseline_ms) {
148+
uint32_t PerfCounters::registerTick(uint32_t baseline_ms) {
149149
if (!World::isFortressMode() || World::ReadPauseState()) {
150150
last_tick_baseline_ms = 0;
151-
return;
151+
return 0;
152152
}
153153

154154
// only update when the tick counter has advanced
155155
if (!world || last_frame_counter == world->frame_counter)
156-
return;
156+
return 0;
157157
last_frame_counter = world->frame_counter;
158158

159159
if (last_tick_baseline_ms == 0) {
160160
last_tick_baseline_ms = baseline_ms;
161-
return;
161+
return 0;
162162
}
163163

164164
uint32_t elapsed_ms = baseline_ms - last_tick_baseline_ms;
@@ -173,6 +173,8 @@ void PerfCounters::registerTick(uint32_t baseline_ms) {
173173

174174
recent_ticks.history[recent_ticks.head_idx] = elapsed_ms;
175175
recent_ticks.sum_ms += elapsed_ms;
176+
177+
return elapsed_ms;
176178
}
177179

178180
uint32_t PerfCounters::getUnpausedFps() {
@@ -1705,6 +1707,7 @@ bool Core::InitMainThread() {
17051707
}
17061708

17071709
perf_counters.reset();
1710+
unpaused_ms = 0;
17081711

17091712
return true;
17101713
}
@@ -2141,7 +2144,7 @@ int Core::Update()
21412144
}
21422145

21432146
uint32_t start_ms = p->getTickCount();
2144-
perf_counters.registerTick(start_ms);
2147+
unpaused_ms += perf_counters.registerTick(start_ms);
21452148
doUpdate(out);
21462149
perf_counters.incCounter(perf_counters.total_update_ms, start_ms);
21472150
}
@@ -2337,6 +2340,7 @@ void Core::onStateChange(color_ostream &out, state_change_event event)
23372340
case SC_WORLD_LOADED:
23382341
{
23392342
perf_counters.reset();
2343+
unpaused_ms = 0;
23402344
Persistence::Internal::load(out);
23412345
plug_mgr->doLoadWorldData(out);
23422346
loadModScriptPaths(out);

library/include/Core.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ namespace DFHack
100100
bool getIgnorePauseState();
101101

102102
// noop if game is paused and getIgnorePauseState() returns false
103-
void incCounter(uint32_t &perf_counter, uint32_t baseline_ms);
103+
void incCounter(uint32_t &counter, uint32_t baseline_ms);
104+
105+
// returns number of unpaused ms since last tick
106+
uint32_t registerTick(uint32_t baseline_ms);
104107

105-
void registerTick(uint32_t baseline_ms);
106108
uint32_t getUnpausedFps();
107109

108110
private:
@@ -219,6 +221,7 @@ namespace DFHack
219221
static void cheap_tokenise(std::string const& input, std::vector<std::string> &output);
220222

221223
PerfCounters perf_counters;
224+
uint32_t getUnpausedMs() { return unpaused_ms; }
222225

223226
lua_State* getLuaState(bool bypass_assertion = false) {
224227
assert(bypass_assertion || isSuspended());
@@ -334,6 +337,8 @@ namespace DFHack
334337

335338
lua_State* State;
336339

340+
uint32_t unpaused_ms; // reset to 0 on map load
341+
337342
friend class CoreService;
338343
friend class ServerConnection;
339344
friend class CoreSuspender;

0 commit comments

Comments
 (0)