R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ServerLoop.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** ServerLoop.hpp - Fixed timestep game loop for deterministic gameplay
6*/
7
8#pragma once
9
10#include <atomic>
11#include <memory>
12#include <mutex>
13#include <thread>
18
19namespace server {
20
21 class EventBus;
22
39 class ServerLoop : public IServerLoop {
40 public:
47 explicit ServerLoop(std::unique_ptr<IGameLogic> gameLogic, std::shared_ptr<EventBus> eventBus,
48 float gameSpeedMultiplier = 1.0f);
49
53 ~ServerLoop() override;
54
59 bool initialize();
60
65 void start() override;
66
70 void stop() override;
71
76 bool isRunning() const override { return _loopThread.joinable(); }
77
82 uint32_t getCurrentTick() const;
83
89
94 std::shared_ptr<ecs::wrapper::ECSWorld> getECSWorld();
95
101
102 private:
107 void _gameLoopThread(std::stop_token stopToken);
108
112 void _fixedUpdate();
113
114 // Game logic
115 std::unique_ptr<IGameLogic> _gameLogic;
116 std::shared_ptr<EventBus> _eventBus;
118
119 // Threading
120 std::jthread _loopThread;
121 std::atomic<bool> _initialized{false};
122
123 // Timing
124 static constexpr float BASE_FIXED_TIMESTEP = 1.0f / 60.0f; // 60 Hz base rate
125 static constexpr float MAX_FRAME_ACCUMULATOR = 0.2f; // Skip frames if lag > 200ms
126
127 float _gameSpeedMultiplier{1.0f}; // Game speed multiplier (0.25 to 1.0)
129 BASE_FIXED_TIMESTEP}; // Scaled timestep = base * multiplier (passed to systems)
130 double _timeAccumulator{0.0};
131 uint32_t _frameCount{0};
132 uint32_t _skippedFrames{0};
133
134 // Synchronization
135 mutable std::mutex _stateMutex;
136 };
137
138} // namespace server
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
Utility class to measure frame durations.
Interface for server-side game logic orchestration.
Interface for the main server loop.
Deterministic fixed-timestep game loop.
float getGameSpeedMultiplier() const
Get the game speed multiplier.
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)
static constexpr float MAX_FRAME_ACCUMULATOR
IGameLogic & getGameLogic()
Get reference to game logic.
bool isRunning() const override
Check if game loop is running (IServerLoop implementation)
std::unique_ptr< IGameLogic > _gameLogic
bool initialize()
Initialize the game loop.
FrameTimer _frameTimer
std::jthread _loopThread
void start() override
Start the game loop (IServerLoop implementation) Runs in background thread.
std::atomic< bool > _initialized
std::shared_ptr< EventBus > _eventBus
static constexpr float BASE_FIXED_TIMESTEP
std::mutex _stateMutex