#pragma once
#include <chrono>
#include <thread>
/** класс временной синхронизации
@param FPS число действий(кадров) в секунду
@details https://stackoverflow.com/posts/59165784/revisions */
template<std::intmax_t FPS> class Frame_rater final {
public:
  Frame_rater() 
    : time_between_frames{1} // std::ratio<1, FPS> seconds
    , tp{std::chrono::high_resolution_clock::now()}
  {}
  void reset() { tp = std::chrono::high_resolution_clock::now(); }
  void sleep() {
    tp += time_between_frames; // add to time point
    std::this_thread::sleep_until(tp); // and sleep until that time point
  }
private:
  //! a duration with a length of 1/FPS seconds
  std::chrono::duration<long double, std::ratio<1, FPS>> time_between_frames;
  //! the time point we'll add to in every loop
  std::chrono::time_point<std::chrono::high_resolution_clock,
    decltype(time_between_frames)> tp;
};