mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
refactor(gameplay): use steady clock and sleep_until for tick loop timing
This commit is contained in:
@@ -289,17 +289,17 @@ void ClientWorld::hot_reload() {
|
||||
|
||||
void ClientWorld::client_run(std::stop_token stoken) {
|
||||
Logger::info("Client Thread Started");
|
||||
while (!stoken.stop_requested()) {
|
||||
auto t1 = system_clock::now();
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
constexpr auto TICK = std::chrono::milliseconds(DEFAULT_PER_TICK_TIME);
|
||||
|
||||
auto next = Clock::now();
|
||||
while (!stoken.stop_requested()) {
|
||||
next += TICK;
|
||||
for (auto& x : m_timers) {
|
||||
x.second.update();
|
||||
}
|
||||
|
||||
auto t2 = system_clock::now();
|
||||
auto dt = duration_cast<microseconds>(t2 - t1);
|
||||
auto st = std::max(dt, milliseconds(DEFAULT_PER_TICK_TIME) - dt);
|
||||
std::this_thread::sleep_for(st);
|
||||
std::this_thread::sleep_until(next);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -299,18 +299,19 @@ void ServerWorld::stop_thread_pool() {
|
||||
|
||||
void ServerWorld::serever_run(std::stop_token stoken) {
|
||||
Logger::info("Server Thread Started!");
|
||||
while (!stoken.stop_requested()) {
|
||||
auto t1 = system_clock::now();
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
constexpr auto TICK = std::chrono::milliseconds(DEFAULT_PER_TICK_TIME);
|
||||
|
||||
auto next = Clock::now();
|
||||
while (!stoken.stop_requested()) {
|
||||
next += TICK;
|
||||
if (m_tick_running) {
|
||||
++m_game_ticks;
|
||||
m_day_tick = (m_day_tick + 1) % DAY_TIME;
|
||||
}
|
||||
update();
|
||||
auto t2 = system_clock::now();
|
||||
auto dt = duration_cast<microseconds>(t2 - t1);
|
||||
auto st = std::max(dt, milliseconds(m_per_tick_time) - dt);
|
||||
std::this_thread::sleep_for(st);
|
||||
std::this_thread::sleep_until(next);
|
||||
}
|
||||
Logger::info("Server Thread Stopped!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user