mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 09:56:01 +01:00
Merge pull request #270 from MaSzyna-EU07/fix/fpslimit-frame-pacing
fpslimit: pace frames in integer clock ticks, show the real frame rate (eu07/maszyna#416)
This commit is contained in:
@@ -67,6 +67,7 @@ struct subsystem_stopwatches {
|
||||
stopwatch sim_events;
|
||||
stopwatch sim_ai;
|
||||
stopwatch mainloop_total;
|
||||
stopwatch mainloop_period; // start to start, the wait for fpslimit included
|
||||
};
|
||||
|
||||
extern subsystem_stopwatches subsystem;
|
||||
|
||||
59
utilities/framepacer.h
Normal file
59
utilities/framepacer.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <optional>
|
||||
|
||||
// paces the main loop to the frame time set by fpslimit in eu07.ini.
|
||||
// the frames are scheduled on a fixed cadence rather than by sleeping off what is left of each frame,
|
||||
// so a sleep that overshoots (the windows timer wakes up in steps of up to ~15.6 ms) is made up by
|
||||
// a shorter wait in the next frame, and the frame rate averages to the limit.
|
||||
// all the time arithmetic is done in the clock's integer ticks: a sleep_for() given a float duration
|
||||
// adds it to now() as float nanoseconds, which after a few hours of system uptime rounds the wake-up
|
||||
// time to tens of milliseconds and lets the limit fall through (eu07/maszyna#416)
|
||||
class frame_pacer {
|
||||
|
||||
public:
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
// called at the end of each frame. returns the point in time the next frame may start at,
|
||||
// or nothing when it may start right away
|
||||
[[nodiscard]] std::optional<clock::time_point> next_frame(clock::time_point const Now, std::chrono::duration<float> const Frametime) {
|
||||
return next_frame(Now, std::chrono::duration_cast<clock::duration>(Frametime));
|
||||
}
|
||||
[[nodiscard]] std::optional<clock::time_point> next_frame(clock::time_point const Now, clock::duration const Minframetime) {
|
||||
if (Minframetime <= clock::duration::zero()) {
|
||||
// no limit
|
||||
m_framestart.reset();
|
||||
return std::nullopt;
|
||||
}
|
||||
if (!m_framestart) {
|
||||
// first paced frame
|
||||
m_framestart = Now;
|
||||
return std::nullopt;
|
||||
}
|
||||
auto const nextframe{*m_framestart + Minframetime};
|
||||
if (Now >= nextframe + Minframetime) {
|
||||
// a whole frame or more behind: start the cadence anew rather than run frames back to back to catch up
|
||||
m_framestart = Now;
|
||||
return std::nullopt;
|
||||
}
|
||||
m_framestart = nextframe;
|
||||
if (Now >= nextframe) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return nextframe;
|
||||
}
|
||||
|
||||
private:
|
||||
// the scheduled start of the current frame
|
||||
std::optional<clock::time_point> m_framestart;
|
||||
};
|
||||
Reference in New Issue
Block a user