R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::ServerLoop Class Reference

Deterministic fixed-timestep game loop. More...

#include <ServerLoop.hpp>

Inheritance diagram for server::ServerLoop:
Inheritance graph
Collaboration diagram for server::ServerLoop:
Collaboration graph

Public Member Functions

 ServerLoop (std::unique_ptr< IGameLogic > gameLogic, std::shared_ptr< EventBus > eventBus, float gameSpeedMultiplier=1.0f)
 Constructor.
 
 ~ServerLoop () override
 Destructor - ensures clean shutdown.
 
bool initialize ()
 Initialize the game loop.
 
void start () override
 Start the game loop (IServerLoop implementation) Runs in background thread.
 
void stop () override
 Stop the game loop (IServerLoop implementation)
 
bool isRunning () const override
 Check if game loop is running (IServerLoop implementation)
 
uint32_t getCurrentTick () const
 Get the current server tick.
 
IGameLogicgetGameLogic ()
 Get reference to game logic.
 
std::shared_ptr< ecs::wrapper::ECSWorldgetECSWorld ()
 Get reference to ECS world from GameLogic.
 
float getGameSpeedMultiplier () const
 Get the game speed multiplier.
 
- Public Member Functions inherited from server::IServerLoop
virtual ~IServerLoop ()=default
 

Private Member Functions

void _gameLoopThread (std::stop_token stopToken)
 Main game loop function (runs in thread)
 
void _fixedUpdate ()
 Process a single fixed timestep update.
 

Private Attributes

std::unique_ptr< IGameLogic_gameLogic
 
std::shared_ptr< EventBus_eventBus
 
FrameTimer _frameTimer
 
std::jthread _loopThread
 
std::atomic< bool > _initialized {false}
 
float _gameSpeedMultiplier {1.0f}
 
float _scaledTimestep
 
double _timeAccumulator {0.0}
 
uint32_t _frameCount {0}
 
uint32_t _skippedFrames {0}
 
std::mutex _stateMutex
 

Static Private Attributes

static constexpr float BASE_FIXED_TIMESTEP = 1.0f / 60.0f
 
static constexpr float MAX_FRAME_ACCUMULATOR = 0.2f
 

Detailed Description

Deterministic fixed-timestep game loop.

Ensures all clients see the same game state by running at a fixed 60 Hz with deterministic updates. Implements time accumulator pattern. Implements IServerLoop interface.

Features:

  • Fixed 60 Hz update rate (1/60 = 0.0166667 seconds per frame)
  • Deterministic state progression
  • Separate update/render timing
  • Real-time network synchronization hooks
  • Thread-safe operation
  • Frame skipping if lag exceeds threshold

Definition at line 39 of file ServerLoop.hpp.

Constructor & Destructor Documentation

◆ ServerLoop()

server::ServerLoop::ServerLoop ( std::unique_ptr< IGameLogic gameLogic,
std::shared_ptr< EventBus eventBus,
float  gameSpeedMultiplier = 1.0f 
)
explicit

Constructor.

Parameters
gameLogicThe game logic to run
eventBusEvent bus for publishing game events
gameSpeedMultiplierGame speed multiplier (0.25 to 1.0, default 1.0)

Definition at line 15 of file ServerLoop.cpp.

◆ ~ServerLoop()

server::ServerLoop::~ServerLoop ( )
override

Destructor - ensures clean shutdown.

Definition at line 22 of file ServerLoop.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ _fixedUpdate()

void server::ServerLoop::_fixedUpdate ( )
private

Process a single fixed timestep update.

Definition at line 130 of file ServerLoop.cpp.

References _frameCount, _gameLogic, _scaledTimestep, _stateMutex, and LOG_ERROR.

Referenced by _gameLoopThread().

◆ _gameLoopThread()

void server::ServerLoop::_gameLoopThread ( std::stop_token  stopToken)
private

Main game loop function (runs in thread)

Parameters
stopTokenToken to check for stop requests

Definition at line 94 of file ServerLoop.cpp.

References _fixedUpdate(), _frameCount, _frameTimer, _skippedFrames, _timeAccumulator, BASE_FIXED_TIMESTEP, LOG_DEBUG, LOG_ERROR, LOG_WARNING, server::FrameTimer::sleepMilliseconds(), and server::FrameTimer::tick().

Referenced by start().

Here is the call graph for this function:

◆ getCurrentTick()

uint32_t server::ServerLoop::getCurrentTick ( ) const

Get the current server tick.

Returns
uint32_t Current tick number

Definition at line 83 of file ServerLoop.cpp.

References _frameCount.

Referenced by Server::_broadcastGameState(), Server::_sendGameStartToRoom(), and Server::_sendGameStartToSpectator().

◆ getECSWorld()

std::shared_ptr< ecs::wrapper::ECSWorld > server::ServerLoop::getECSWorld ( )

Get reference to ECS world from GameLogic.

Returns
ECSWorld shared pointer

Definition at line 87 of file ServerLoop.cpp.

References _gameLogic.

Referenced by Server::_broadcastGameState(), Server::_processPendingDestructions(), Server::_sendGameStartToRoom(), and Server::_sendGameStartToSpectator().

◆ getGameLogic()

IGameLogic & server::ServerLoop::getGameLogic ( )
inline

Get reference to game logic.

Returns
IGameLogic reference

Definition at line 88 of file ServerLoop.hpp.

References _gameLogic.

◆ getGameSpeedMultiplier()

float server::ServerLoop::getGameSpeedMultiplier ( ) const
inline

Get the game speed multiplier.

Returns
Game speed multiplier (0.25 to 1.0)

Definition at line 100 of file ServerLoop.hpp.

References _gameSpeedMultiplier.

◆ initialize()

bool server::ServerLoop::initialize ( )

Initialize the game loop.

Returns
true if successful

Definition at line 26 of file ServerLoop.cpp.

References _gameLogic, _gameSpeedMultiplier, _initialized, _scaledTimestep, BASE_FIXED_TIMESTEP, LOG_ERROR, and LOG_INFO.

◆ isRunning()

bool server::ServerLoop::isRunning ( ) const
inlineoverridevirtual

Check if game loop is running (IServerLoop implementation)

Returns
true if running

Implements server::IServerLoop.

Definition at line 76 of file ServerLoop.hpp.

References _loopThread.

◆ start()

void server::ServerLoop::start ( )
overridevirtual

Start the game loop (IServerLoop implementation) Runs in background thread.

Implements server::IServerLoop.

Definition at line 55 of file ServerLoop.cpp.

References _frameCount, _frameTimer, _gameLoopThread(), _loopThread, _skippedFrames, _timeAccumulator, LOG_ERROR, LOG_INFO, LOG_WARNING, and server::FrameTimer::reset().

Here is the call graph for this function:

◆ stop()

void server::ServerLoop::stop ( )
overridevirtual

Stop the game loop (IServerLoop implementation)

Implements server::IServerLoop.

Definition at line 78 of file ServerLoop.cpp.

References _loopThread, and LOG_INFO.

Referenced by ~ServerLoop().

Member Data Documentation

◆ _eventBus

std::shared_ptr<EventBus> server::ServerLoop::_eventBus
private

Definition at line 116 of file ServerLoop.hpp.

◆ _frameCount

uint32_t server::ServerLoop::_frameCount {0}
private

Definition at line 131 of file ServerLoop.hpp.

Referenced by _fixedUpdate(), _gameLoopThread(), getCurrentTick(), and start().

◆ _frameTimer

FrameTimer server::ServerLoop::_frameTimer
private

Definition at line 117 of file ServerLoop.hpp.

Referenced by _gameLoopThread(), and start().

◆ _gameLogic

std::unique_ptr<IGameLogic> server::ServerLoop::_gameLogic
private

Definition at line 115 of file ServerLoop.hpp.

Referenced by _fixedUpdate(), getECSWorld(), getGameLogic(), and initialize().

◆ _gameSpeedMultiplier

float server::ServerLoop::_gameSpeedMultiplier {1.0f}
private

Definition at line 127 of file ServerLoop.hpp.

Referenced by getGameSpeedMultiplier(), and initialize().

◆ _initialized

std::atomic<bool> server::ServerLoop::_initialized {false}
private

Definition at line 121 of file ServerLoop.hpp.

Referenced by initialize().

◆ _loopThread

std::jthread server::ServerLoop::_loopThread
private

Definition at line 120 of file ServerLoop.hpp.

Referenced by isRunning(), start(), and stop().

◆ _scaledTimestep

float server::ServerLoop::_scaledTimestep
private
Initial value:
{
static constexpr float BASE_FIXED_TIMESTEP

Definition at line 128 of file ServerLoop.hpp.

Referenced by _fixedUpdate(), and initialize().

◆ _skippedFrames

uint32_t server::ServerLoop::_skippedFrames {0}
private

Definition at line 132 of file ServerLoop.hpp.

Referenced by _gameLoopThread(), and start().

◆ _stateMutex

std::mutex server::ServerLoop::_stateMutex
mutableprivate

Definition at line 135 of file ServerLoop.hpp.

Referenced by _fixedUpdate().

◆ _timeAccumulator

double server::ServerLoop::_timeAccumulator {0.0}
private

Definition at line 130 of file ServerLoop.hpp.

Referenced by _gameLoopThread(), and start().

◆ BASE_FIXED_TIMESTEP

constexpr float server::ServerLoop::BASE_FIXED_TIMESTEP = 1.0f / 60.0f
staticconstexprprivate

Definition at line 124 of file ServerLoop.hpp.

Referenced by _gameLoopThread(), and initialize().

◆ MAX_FRAME_ACCUMULATOR

constexpr float server::ServerLoop::MAX_FRAME_ACCUMULATOR = 0.2f
staticconstexprprivate

Definition at line 125 of file ServerLoop.hpp.


The documentation for this class was generated from the following files: