Files
khaotic-engine-Reborn/enginecustom/src/inc/system/fps_limiter.h
CatChow0 389cde97c2 Minor - Adds target FPS control to engine settings - V14.5.0
Adds functionality to control the target FPS of the engine via the ImGui settings panel.

This includes:
- Adding target FPS variables and accessors in the application class.
- Adding a set_target_fps function to the fps_limiter class.
- Updating the ImGui engine settings panel to include a target FPS setting.
- Modifying the main loop to make use of the fps limiter and the engine target FPS.
2025-10-01 16:58:37 +02:00

40 lines
1.3 KiB
C++

#pragma once
#include <chrono>
class fps_limiter {
public:
/**
* Constructor for fps_limiter class
* @param target_fps Target frames per second for the limiter. Default is 60.0f FPS.
*/
explicit fps_limiter(const float target_fps = 60.0f)
: min_delta_(1.0f / target_fps), last_time_(std::chrono::high_resolution_clock::now()) {}
/**
* Function to check if enough time has passed since the last execution.
* @return True if the time since the last call is greater than or equal to the minimum delta time, otherwise false.
*/
bool should_run() {
const auto now = std::chrono::high_resolution_clock::now();
if (const float elapsed = std::chrono::duration<float>(now - last_time_).count(); elapsed >= min_delta_) {
last_time_ = now;
return true;
}
return false;
}
/**
* Dynamically set the target FPS limit.
* @param target_fps New target frames per second.
*/
void set_target_fps(float target_fps) {
if (target_fps > 0.0f) {
min_delta_ = 1.0f / target_fps;
}
}
private:
float min_delta_; // Minimum time in seconds between frames
std::chrono::high_resolution_clock::time_point last_time_; // Time point of last allowed execution
};