A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Clock.h 733B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #pragma once
  2. #include <string>
  3. #include <chrono>
  4. #include <ostream>
  5. namespace Swan {
  6. class Clock {
  7. public:
  8. void tick(float dt) { time_ += dt; }
  9. void reset() { time_ = 0; }
  10. float duration() { return time_; }
  11. bool periodic(float secs);
  12. private:
  13. float time_ = 0;
  14. };
  15. class RTClock {
  16. public:
  17. void reset() {
  18. start_ = std::chrono::steady_clock::now();
  19. }
  20. double duration() const {
  21. return std::chrono::duration<double>(
  22. std::chrono::steady_clock::now() - start_).count();
  23. }
  24. friend std::ostream &operator<<(std::ostream &os, const RTClock &clock) {
  25. os << (double)clock.duration() << 's';
  26. return os;
  27. }
  28. private:
  29. std::chrono::time_point<std::chrono::steady_clock> start_ =
  30. std::chrono::steady_clock::now();
  31. };
  32. }