Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

61 wiersze
1.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
  5. // © 2016 Guillaume Bittoun <guillaume.bittoun@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. #include <chrono>
  15. #include <thread>
  16. //
  17. // The timer class
  18. // ---------------
  19. //
  20. namespace lol
  21. {
  22. class timer
  23. {
  24. public:
  25. inline timer() { (void)get_seconds(true); }
  26. inline void reset() { (void)get_seconds(true); }
  27. inline float get() { return get_seconds(true); }
  28. inline float poll() { return get_seconds(false); }
  29. void wait(float seconds)
  30. {
  31. if (seconds > 0.0f)
  32. {
  33. float secs_elapsed = get_seconds(false);
  34. std::this_thread::sleep_for(std::chrono::duration<float>(seconds - secs_elapsed));
  35. }
  36. }
  37. private:
  38. std::chrono::steady_clock::time_point m_tp;
  39. float get_seconds(bool do_reset)
  40. {
  41. auto tp = std::chrono::steady_clock::now(), tp0 = m_tp;
  42. if (do_reset)
  43. m_tp = tp;
  44. return std::chrono::duration_cast<std::chrono::duration<float>>(tp - tp0).count();
  45. }
  46. };
  47. } /* namespace lol */