|
R-Type
Distributed multiplayer game engine in C++
|
Main game loop orchestrating all subsystems. More...
#include <GameLoop.hpp>

Public Member Functions | |
| GameLoop (EventBus &eventBus, Replicator &replicator, const std::string &playerName) | |
| Constructor with shared EventBus and Replicator. | |
| ~GameLoop () | |
| Destructor. | |
| bool | initialize () |
| Initialize all game subsystems. | |
| void | run () |
| Start the main game loop. | |
| void | shutdown () |
| Stop and clean up all subsystems. | |
| void | stop () |
| Stop the game loop. | |
| void | setReconciliationThreshold (float threshold) |
| Set the reconciliation threshold for client-side prediction. | |
| float | getReconciliationThreshold () const |
| Get the current reconciliation threshold. | |
Private Member Functions | |
| void | update (float deltaTime) |
| Update game logic (variable timestep) | |
| void | fixedUpdate (float fixedDeltaTime) |
| Update physics simulation (fixed timestep) | |
| void | render () |
| Perform rendering of current frame. | |
| void | processInput () |
| Process player inputs. | |
| float | calculateDeltaTime () |
| Calculate time elapsed since last frame. | |
| void | handleNetworkMessage (const NetworkEvent &event) |
| Handle incoming network messages. | |
| void | handleUIEvent (const UIEvent &event) |
| void | handleGameStart (const std::vector< uint8_t > &payload) |
| void | handleGameState (const std::vector< uint8_t > &payload) |
| void | handleGameruleUpdate (const std::vector< uint8_t > &payload) |
| void | handleRoomList (const std::vector< uint8_t > &payload) |
| void | handleRoomState (const std::vector< uint8_t > &payload) |
| void | handleEntityDestroyed (const std::vector< uint8_t > &payload) |
| void | handleChatMessage (const std::vector< uint8_t > &payload) |
| void | handleLeftRoom (const std::vector< uint8_t > &payload) |
| void | handleGameOver (const std::vector< uint8_t > &payload) |
| void | processServerReconciliation (const RType::Messages::S2C::EntityState &entity) |
| void | simulateInputHistory (float &x, float &y) |
Private Attributes | |
| EventBus * | _eventBus |
| std::unique_ptr< InputBuffer > | _inputBuffer |
| Replicator * | _replicator |
| std::unique_ptr< Rendering > | _rendering |
| bool | _running = false |
| bool | _initialized = false |
| float | _fixedTimestep = 1.0f / 60.0f |
| float | _accumulator = 0.0f |
| uint32_t | _currentFrame = 0 |
| uint32_t | _inputSequenceId = 0 |
| std::deque< RType::Messages::C2S::PlayerInput::InputSnapshot > | _inputHistory |
| std::optional< uint32_t > | _myEntityId |
| uint32_t | _myPlayerId = 0 |
| bool | _entityInitialized = false |
| bool | _justCreatedRoom |
| std::string | _playerName |
| bool | _isMoving = false |
| float | _playerSpeed = 300.0f |
| bool | _clientSidePredictionEnabled = true |
| float | _gameSpeedMultiplier = 1.0f |
| std::unordered_set< uint32_t > | _knownEntityIds |
Static Private Attributes | |
| static constexpr size_t | INPUT_HISTORY_SIZE = 12 |
Main game loop orchestrating all subsystems.
GameLoop is the conductor of the R-Type client. It coordinates and synchronizes all components:
Temporal architecture:
Fixed timestep:
Definition at line 57 of file GameLoop.hpp.
| GameLoop::GameLoop | ( | EventBus & | eventBus, |
| Replicator & | replicator, | ||
| const std::string & | playerName | ||
| ) |
Constructor with shared EventBus and Replicator.
Uses the provided EventBus and Replicator from Client. Call initialize() to start other subsystems.
| eventBus | Shared EventBus instance |
| replicator | Shared Replicator instance |
| playerName | Local player's display name (for host detection) |
Definition at line 15 of file GameLoop.cpp.
| GameLoop::~GameLoop | ( | ) |
Destructor.
Properly stops all subsystems and frees resources.
Definition at line 18 of file GameLoop.cpp.
References shutdown().

|
private |
Calculate time elapsed since last frame.
Definition at line 482 of file GameLoop.cpp.
Referenced by run().
|
private |
Update physics simulation (fixed timestep)
Called N times per frame to guarantee 60 FPS simulation.
| fixedDeltaTime | Fixed simulation time (1/60 = 0.0167s) |
Definition at line 338 of file GameLoop.cpp.
Referenced by run().
| float GameLoop::getReconciliationThreshold | ( | ) | const |
Get the current reconciliation threshold.
Definition at line 291 of file GameLoop.cpp.
References _rendering.
|
private |
Definition at line 806 of file GameLoop.cpp.
References _rendering, RType::Messages::S2C::S2CChatMessage::deserialize(), LOG_ERROR, and LOG_INFO.
Referenced by handleNetworkMessage().

|
private |
Definition at line 654 of file GameLoop.cpp.
References _entityInitialized, _myEntityId, _rendering, RType::Messages::S2C::EntityDestroyed::deserialize(), LOG_ERROR, LOG_INFO, and LOG_WARNING.
Referenced by handleNetworkMessage().

|
private |
Definition at line 855 of file GameLoop.cpp.
References _rendering, RType::Messages::S2C::GameOver::deserialize(), LOG_ERROR, and LOG_INFO.
Referenced by handleNetworkMessage().

|
private |
Definition at line 777 of file GameLoop.cpp.
References _gameSpeedMultiplier, _playerSpeed, RType::Messages::S2C::GamerulePacket::deserialize(), GAME_SPEED_MULTIPLIER, client::ClientGameRules::getInstance(), LOG_ERROR, LOG_INFO, and PLAYER_SPEED.
Referenced by handleNetworkMessage().

|
private |
Definition at line 529 of file GameLoop.cpp.
References _entityInitialized, _myEntityId, _rendering, RType::Messages::S2C::GameStart::deserialize(), LOG_ERROR, and LOG_INFO.
Referenced by handleNetworkMessage().

|
private |
Definition at line 678 of file GameLoop.cpp.
References _clientSidePredictionEnabled, _knownEntityIds, _myEntityId, _rendering, RType::Messages::S2C::GameState::deserialize(), LOG_DEBUG, LOG_ERROR, and processServerReconciliation().
Referenced by handleNetworkMessage().

|
private |
Definition at line 822 of file GameLoop.cpp.
References _eventBus, _myPlayerId, _rendering, BACK_TO_ROOM_LIST, LOG_ERROR, LOG_INFO, and EventBus::publish().
Referenced by handleNetworkMessage().

|
private |
Handle incoming network messages.
Processes GameStart and GameState messages to update entity rendering.
| event | Network event containing message data |
Definition at line 492 of file GameLoop.cpp.
References NetworkEvent::getData(), NetworkMessages::getMessageType(), NetworkMessages::getPayload(), handleChatMessage(), handleEntityDestroyed(), handleGameOver(), handleGameruleUpdate(), handleGameStart(), handleGameState(), handleLeftRoom(), handleRoomList(), handleRoomState(), NetworkMessages::S2C_CHAT_MESSAGE, NetworkMessages::S2C_ENTITY_DESTROYED, NetworkMessages::S2C_GAME_OVER, NetworkMessages::S2C_GAME_START, NetworkMessages::S2C_GAME_STATE, NetworkMessages::S2C_GAMERULE_UPDATE, NetworkMessages::S2C_LEFT_ROOM, NetworkMessages::S2C_ROOM_LIST, and NetworkMessages::S2C_ROOM_STATE.
Referenced by initialize().

|
private |
Definition at line 575 of file GameLoop.cpp.
References _rendering, RType::Messages::S2C::RoomList::deserialize(), RoomData::isPrivate, LOG_ERROR, LOG_INFO, RoomData::maxPlayers, RoomData::playerCount, RoomData::roomId, RoomData::roomName, and RoomData::state.
Referenced by handleNetworkMessage().

|
private |
Definition at line 606 of file GameLoop.cpp.
References _myPlayerId, _rendering, RType::Messages::S2C::RoomState::deserialize(), LOG_ERROR, and LOG_INFO.
Referenced by handleNetworkMessage().

|
private |
Definition at line 51 of file GameLoop.cpp.
References _justCreatedRoom, _replicator, AUTO_MATCHMAKING, CREATE_ROOM, UIEvent::getType(), JOIN_GAME, LEAVE_ROOM, LOG_INFO, LOG_WARNING, LOGIN_ACCOUNT, QUIT_GAME, REGISTER_ACCOUNT, REQUEST_ROOM_LIST, Replicator::sendAutoMatchmaking(), Replicator::sendCreateRoom(), Replicator::sendJoinRoom(), Replicator::sendLeaveRoom(), Replicator::sendLoginAccount(), Replicator::sendRegisterAccount(), Replicator::sendRequestRoomList(), Replicator::sendStartGame(), START_GAME_REQUEST, stop(), UPDATE_AUTO_MATCHMAKING_PREF, and Replicator::updateAutoMatchmakingPreference().
Referenced by initialize().

| bool GameLoop::initialize | ( | ) |
Initialize all game subsystems.
Initializes in order:
Definition at line 22 of file GameLoop.cpp.
References _eventBus, _initialized, _inputBuffer, _rendering, handleNetworkMessage(), handleUIEvent(), LOG_INFO, and EventBus::subscribe().

|
private |
Process player inputs.
Reads keyboard/mouse events and publishes them on EventBus.
Definition at line 362 of file GameLoop.cpp.
References _clientSidePredictionEnabled, _entityInitialized, _fixedTimestep, _gameSpeedMultiplier, _inputHistory, _inputSequenceId, _isMoving, _myEntityId, _playerSpeed, _rendering, _replicator, RType::Messages::C2S::PlayerInput::InputSnapshot::actions, Input::BindingToGamepadButton(), NetworkMessages::C2S_PLAYER_INPUT, NetworkMessages::createMessage(), Input::KeyBindings::getInstance(), INPUT_HISTORY_SIZE, Input::IsGamepadBinding(), Replicator::isSpectator(), Input::MOVE_DOWN, Input::MOVE_LEFT, Input::MOVE_RIGHT, Input::MOVE_UP, RType::Messages::Shared::MoveDown, RType::Messages::Shared::MoveLeft, RType::Messages::Shared::MoveRight, RType::Messages::Shared::MoveUp, Replicator::sendPacket(), RType::Messages::C2S::PlayerInput::InputSnapshot::sequenceId, RType::Messages::C2S::PlayerInput::serialize(), Input::SHOOT, and RType::Messages::Shared::Shoot.
Referenced by run().

|
private |
Definition at line 723 of file GameLoop.cpp.
References _inputHistory, _rendering, RType::Messages::S2C::EntityState::currentAnimation, RType::Messages::S2C::EntityState::entityId, RType::Messages::S2C::EntityState::health, RType::Messages::S2C::EntityState::lastProcessedInput, RType::Messages::S2C::EntityState::position, simulateInputHistory(), RType::Messages::S2C::EntityState::spriteH, RType::Messages::S2C::EntityState::spriteW, RType::Messages::S2C::EntityState::spriteX, RType::Messages::S2C::EntityState::spriteY, RType::Messages::S2C::EntityState::type, RType::Messages::Shared::Vec2::x, and RType::Messages::Shared::Vec2::y.
Referenced by handleGameState().

|
private |
Perform rendering of current frame.
Delegates to Rendering system.
Definition at line 347 of file GameLoop.cpp.
References _rendering, shutdown(), and stop().
Referenced by run().

| void GameLoop::run | ( | ) |
Start the main game loop.
Launches the game loop that runs until:
Loop structure:
Definition at line 161 of file GameLoop.cpp.
References _accumulator, _currentFrame, _fixedTimestep, _initialized, _myEntityId, _myPlayerId, _rendering, _replicator, _running, calculateDeltaTime(), fixedUpdate(), Replicator::getMyPlayerId(), LOG_ERROR, LOG_INFO, LOG_WARNING, processInput(), Replicator::processMessages(), render(), Replicator::sendChatMessage(), and update().

| void GameLoop::setReconciliationThreshold | ( | float | threshold | ) |
Set the reconciliation threshold for client-side prediction.
| threshold | Distance in pixels before server correction is applied |
Adjusts how tolerant the client-side prediction is before reconciling with the server's authoritative position. This should be tuned based on observed network conditions:
Call this method when you detect network latency changes or based on user preferences/settings.
Definition at line 284 of file GameLoop.cpp.
References _rendering, and LOG_INFO.
| void GameLoop::shutdown | ( | ) |
Stop and clean up all subsystems.
Stops in reverse initialization order:
Definition at line 258 of file GameLoop.cpp.
References _initialized, _inputBuffer, _rendering, _running, and LOG_INFO.
Referenced by render(), and ~GameLoop().
|
private |
Definition at line 742 of file GameLoop.cpp.
References _gameSpeedMultiplier, _inputHistory, _playerSpeed, RType::Messages::Shared::MoveDown, RType::Messages::Shared::MoveLeft, RType::Messages::Shared::MoveRight, and RType::Messages::Shared::MoveUp.
Referenced by processServerReconciliation().
| void GameLoop::stop | ( | ) |
Stop the game loop.
Requests game loop to stop at next iteration. Game will terminate cleanly after current frame.
Definition at line 279 of file GameLoop.cpp.
References _running, and LOG_INFO.
Referenced by handleUIEvent(), and render().
|
private |
Update game logic (variable timestep)
Called once per frame for non-critical logic.
| deltaTime | Time elapsed since last call (in seconds) |
Definition at line 298 of file GameLoop.cpp.
References _playerSpeed, _rendering, _replicator, and Replicator::getLatency().
Referenced by run().

|
private |
Definition at line 256 of file GameLoop.hpp.
Referenced by run().
|
private |
Definition at line 275 of file GameLoop.hpp.
Referenced by handleGameState(), and processInput().
|
private |
Definition at line 257 of file GameLoop.hpp.
Referenced by run().
|
private |
Definition at line 269 of file GameLoop.hpp.
Referenced by handleEntityDestroyed(), handleGameStart(), and processInput().
|
private |
Definition at line 247 of file GameLoop.hpp.
Referenced by handleLeftRoom(), and initialize().
|
private |
Definition at line 255 of file GameLoop.hpp.
Referenced by processInput(), and run().
|
private |
Definition at line 276 of file GameLoop.hpp.
Referenced by handleGameruleUpdate(), processInput(), and simulateInputHistory().
|
private |
Definition at line 253 of file GameLoop.hpp.
Referenced by initialize(), run(), and shutdown().
|
private |
Definition at line 248 of file GameLoop.hpp.
Referenced by initialize(), and shutdown().
|
private |
Definition at line 264 of file GameLoop.hpp.
Referenced by processInput(), processServerReconciliation(), and simulateInputHistory().
|
private |
Definition at line 260 of file GameLoop.hpp.
Referenced by processInput().
|
private |
Definition at line 273 of file GameLoop.hpp.
Referenced by processInput().
|
private |
|
private |
Definition at line 279 of file GameLoop.hpp.
Referenced by handleGameState().
|
private |
Definition at line 267 of file GameLoop.hpp.
Referenced by handleEntityDestroyed(), handleGameStart(), handleGameState(), processInput(), and run().
|
private |
Definition at line 268 of file GameLoop.hpp.
Referenced by handleLeftRoom(), handleRoomState(), and run().
|
private |
Definition at line 272 of file GameLoop.hpp.
|
private |
Definition at line 274 of file GameLoop.hpp.
Referenced by handleGameruleUpdate(), processInput(), simulateInputHistory(), and update().
|
private |
Definition at line 250 of file GameLoop.hpp.
Referenced by getReconciliationThreshold(), handleChatMessage(), handleEntityDestroyed(), handleGameOver(), handleGameStart(), handleGameState(), handleLeftRoom(), handleRoomList(), handleRoomState(), initialize(), processInput(), processServerReconciliation(), render(), run(), setReconciliationThreshold(), shutdown(), and update().
|
private |
Definition at line 249 of file GameLoop.hpp.
Referenced by handleUIEvent(), processInput(), run(), and update().
|
private |
Definition at line 252 of file GameLoop.hpp.
Referenced by run(), shutdown(), and stop().
|
staticconstexprprivate |
Definition at line 263 of file GameLoop.hpp.
Referenced by processInput().