|
R-Type
Distributed multiplayer game engine in C++
|
Deterministic fixed-timestep game loop. More...
#include <ServerLoop.hpp>


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. | |
| IGameLogic & | getGameLogic () |
| Get reference to game logic. | |
| std::shared_ptr< ecs::wrapper::ECSWorld > | getECSWorld () |
| 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 |
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:
Definition at line 39 of file ServerLoop.hpp.
|
explicit |
Constructor.
| gameLogic | The game logic to run |
| eventBus | Event bus for publishing game events |
| gameSpeedMultiplier | Game speed multiplier (0.25 to 1.0, default 1.0) |
Definition at line 15 of file ServerLoop.cpp.
|
override |
Destructor - ensures clean shutdown.
Definition at line 22 of file ServerLoop.cpp.
References stop().

|
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().
|
private |
Main game loop function (runs in thread)
| stopToken | Token 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().

| uint32_t server::ServerLoop::getCurrentTick | ( | ) | const |
Get the current server tick.
Definition at line 83 of file ServerLoop.cpp.
References _frameCount.
Referenced by Server::_broadcastGameState(), Server::_sendGameStartToRoom(), and Server::_sendGameStartToSpectator().
| std::shared_ptr< ecs::wrapper::ECSWorld > server::ServerLoop::getECSWorld | ( | ) |
Get reference to ECS world from GameLogic.
Definition at line 87 of file ServerLoop.cpp.
References _gameLogic.
Referenced by Server::_broadcastGameState(), Server::_processPendingDestructions(), Server::_sendGameStartToRoom(), and Server::_sendGameStartToSpectator().
|
inline |
Get reference to game logic.
Definition at line 88 of file ServerLoop.hpp.
References _gameLogic.
|
inline |
Get the game speed multiplier.
Definition at line 100 of file ServerLoop.hpp.
References _gameSpeedMultiplier.
| bool server::ServerLoop::initialize | ( | ) |
Initialize the game loop.
Definition at line 26 of file ServerLoop.cpp.
References _gameLogic, _gameSpeedMultiplier, _initialized, _scaledTimestep, BASE_FIXED_TIMESTEP, LOG_ERROR, and LOG_INFO.
|
inlineoverridevirtual |
Check if game loop is running (IServerLoop implementation)
Implements server::IServerLoop.
Definition at line 76 of file ServerLoop.hpp.
References _loopThread.
|
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().

|
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().
|
private |
Definition at line 116 of file ServerLoop.hpp.
|
private |
Definition at line 131 of file ServerLoop.hpp.
Referenced by _fixedUpdate(), _gameLoopThread(), getCurrentTick(), and start().
|
private |
Definition at line 117 of file ServerLoop.hpp.
Referenced by _gameLoopThread(), and start().
|
private |
Definition at line 115 of file ServerLoop.hpp.
Referenced by _fixedUpdate(), getECSWorld(), getGameLogic(), and initialize().
|
private |
Definition at line 127 of file ServerLoop.hpp.
Referenced by getGameSpeedMultiplier(), and initialize().
|
private |
Definition at line 121 of file ServerLoop.hpp.
Referenced by initialize().
|
private |
Definition at line 120 of file ServerLoop.hpp.
Referenced by isRunning(), start(), and stop().
|
private |
Definition at line 128 of file ServerLoop.hpp.
Referenced by _fixedUpdate(), and initialize().
|
private |
Definition at line 132 of file ServerLoop.hpp.
Referenced by _gameLoopThread(), and start().
|
mutableprivate |
Definition at line 135 of file ServerLoop.hpp.
Referenced by _fixedUpdate().
|
private |
Definition at line 130 of file ServerLoop.hpp.
Referenced by _gameLoopThread(), and start().
|
staticconstexprprivate |
Definition at line 124 of file ServerLoop.hpp.
Referenced by _gameLoopThread(), and initialize().
|
staticconstexprprivate |
Definition at line 125 of file ServerLoop.hpp.