Add network timer in main loop

This commit is contained in:
Benau 2018-08-23 14:40:17 +08:00
parent 9e500751a1
commit 1992177205
2 changed files with 29 additions and 0 deletions

View File

@ -66,6 +66,8 @@ LRESULT CALLBACK separateProcessProc(_In_ HWND hwnd, _In_ UINT uMsg,
MainLoop::MainLoop(unsigned parent_pid)
: m_abort(false), m_ticks_adjustment(0), m_parent_pid(parent_pid)
{
m_network_timer.store(StkTime::getRealTimeMs());
m_start_game_ticks.store(0);
m_curr_time = 0;
m_prev_time = 0;
m_throttle_fps = true;
@ -493,4 +495,18 @@ void MainLoop::abort()
m_abort = true;
} // abort
//-----------------------------------------------------------------------------
/** Set game start ticks told by server somewhere in the future.
*/
void MainLoop::setStartNetworkGameTimer(uint64_t ticks)
{
uint64_t ticks_now = getNetworkTimer();
if (ticks < ticks_now)
{
Log::warn("MainLoop", "Network timer is too slow to catch up");
ticks = ticks_now;
}
m_start_game_ticks.store(ticks);
} // setStartNetworkGameTimer
/* EOF */

View File

@ -21,6 +21,7 @@
#define HEADER_MAIN_LOOP_HPP
#include "utils/synchronised.hpp"
#include "utils/time.hpp"
#include "utils/types.hpp"
#include <atomic>
@ -39,6 +40,8 @@ private:
Synchronised<int> m_ticks_adjustment;
std::atomic<uint64_t> m_network_timer, m_start_game_ticks;
uint32_t m_curr_time;
uint32_t m_prev_time;
unsigned m_parent_pid;
@ -62,6 +65,16 @@ public:
m_ticks_adjustment.getData() += ticks;
m_ticks_adjustment.unlock();
}
// ------------------------------------------------------------------------
uint64_t getNetworkTimer() const
{ return StkTime::getRealTimeMs() - m_network_timer.load(); }
// ------------------------------------------------------------------------
void setNetworkTimer(uint64_t ticks)
{ m_network_timer.store(StkTime::getRealTimeMs() - ticks); }
// ------------------------------------------------------------------------
void resetStartNetworkGameTimer() { m_start_game_ticks.store(0); }
// ------------------------------------------------------------------------
void setStartNetworkGameTimer(uint64_t ticks);
}; // MainLoop