#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
};
