R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
FrameTimer.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by hugo on 06/12/2025
4** File description:
5** FrameTimer.hpp
6*/
7
8#pragma once
9
10#include <chrono>
11#include <thread>
12
13namespace server {
14
21 class FrameTimer {
22 public:
23 FrameTimer() = default;
24 ~FrameTimer() = default;
25
29 void reset() { _start = std::chrono::steady_clock::now(); }
30
35 double elapsed() const {
36 auto now = std::chrono::steady_clock::now();
37 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - _start);
38 return duration.count() / 1000000.0;
39 }
40
45 double tick() {
46 auto now = std::chrono::steady_clock::now();
47 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - _start);
48 _start = now; // Reset immediately
49 return duration.count() / 1000000.0;
50 }
51
56 static void sleepMilliseconds(int milliseconds) {
57 std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
58 }
59
60 private:
61 std::chrono::steady_clock::time_point _start{std::chrono::steady_clock::now()};
62 };
63
64} // namespace server
Utility class to measure frame durations.
std::chrono::steady_clock::time_point _start
FrameTimer()=default
double elapsed() const
Get the elapsed time in seconds since last reset.
~FrameTimer()=default
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.