16 float gameSpeedMultiplier)
17 : _gameLogic(std::move(gameLogic)),
19 _gameSpeedMultiplier(std::clamp(gameSpeedMultiplier, 0.25f, 1.0f)),
20 _scaledTimestep(BASE_FIXED_TIMESTEP * _gameSpeedMultiplier) {}
31 LOG_INFO(
"Initializing deterministic game loop...");
35 LOG_ERROR(
"Failed to initialize game logic");
40 LOG_INFO(
"✓ Game logic initialized");
48 }
catch (
const std::exception &e) {
49 LOG_ERROR(
"Initialization failed: ", e.what());
61 LOG_INFO(
"Starting game loop thread...");
71 LOG_INFO(
"✓ Game loop thread started");
72 }
catch (
const std::exception &e) {
73 LOG_ERROR(
"Failed to start game loop: ", e.what());
89 return gameLogic->getECSWorld();
97 while (!stopToken.stop_requested()) {
102 if (frameTime > 0.1) {
103 LOG_WARNING(
"Frame time exceeded 100ms (", frameTime * 1000.0,
"ms)");
117 if (frameSkips > 1) {
123 }
catch (
const std::exception &e) {
124 LOG_ERROR(
"Thread exception: ", e.what());
137 }
catch (
const std::exception &e) {
138 LOG_ERROR(
"Game logic update failed: ", e.what());
double tick()
Get elapsed time and automatically reset (optimized for game loops)
static void sleepMilliseconds(int milliseconds)
Sleep for specified milliseconds (centralized time management)
void reset()
Reset the timer to the current time.
Deterministic, authoritative server game logic.
uint32_t getCurrentTick() const
Get the current server tick.
std::shared_ptr< ecs::wrapper::ECSWorld > getECSWorld()
Get reference to ECS world from GameLogic.
void _fixedUpdate()
Process a single fixed timestep update.
~ServerLoop() override
Destructor - ensures clean shutdown.
void _gameLoopThread(std::stop_token stopToken)
Main game loop function (runs in thread)
void stop() override
Stop the game loop (IServerLoop implementation)
ServerLoop(std::unique_ptr< IGameLogic > gameLogic, std::shared_ptr< EventBus > eventBus, float gameSpeedMultiplier=1.0f)
Constructor.
std::unique_ptr< IGameLogic > _gameLogic
bool initialize()
Initialize the game loop.
void start() override
Start the game loop (IServerLoop implementation) Runs in background thread.
std::atomic< bool > _initialized
static constexpr float BASE_FIXED_TIMESTEP
float _gameSpeedMultiplier