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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. double dur = clock.duration();
  26. if (dur > 1)
  27. os << dur << 's';
  28. else
  29. os << dur * 1000.0 << "ms";
  30. return os;
  31. }
  32. private:
  33. std::chrono::time_point<std::chrono::steady_clock> start_ =
  34. std::chrono::steady_clock::now();
  35. };
  36. }