Program Listing for File fps_limiter.hpp

Return to documentation for file (inexor/vulkan-renderer/tools/fps_limiter.hpp)

#pragma once

#include <chrono>
#include <cstdint>
#include <optional>

namespace inexor::vulkan_renderer::tools {

class FPSLimiter {
private:
    std::uint32_t m_max_fps{DEFAULT_FPS};
    std::chrono::milliseconds m_frame_time{DEFAULT_FPS / 1000};
    std::chrono::time_point<std::chrono::high_resolution_clock> m_last_time;
    std::chrono::time_point<std::chrono::high_resolution_clock> m_last_fps_update_time;
    std::chrono::milliseconds m_fps_update_interval{1000};
    std::uint32_t m_frames{0};

    // The requested max_fps will be clamped in between these limits.
    static constexpr std::uint32_t MIN_FPS{1};
    static constexpr std::uint32_t MAX_FPS{2000};

public:
    static constexpr std::uint32_t DEFAULT_FPS{1000};

    FPSLimiter(std::uint32_t max_fps = DEFAULT_FPS);

    void set_max_fps(std::uint32_t max_fps);

    [[nodiscard]] bool is_next_frame_allowed();

    [[nodiscard]] std::optional<std::uint32_t> get_fps();
};

} // namespace inexor::vulkan_renderer::tools