Timer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright (c) 2006-2020 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_TIMER_TIMER_H
  21. #define LOVE_TIMER_TIMER_H
  22. // LOVE
  23. #include "common/Module.h"
  24. namespace love
  25. {
  26. namespace timer
  27. {
  28. class Timer : public Module
  29. {
  30. public:
  31. Timer();
  32. virtual ~Timer() {}
  33. // Implements Module.
  34. ModuleType getModuleType() const override { return M_TIMER; }
  35. const char *getName() const override { return "love.timer"; }
  36. /**
  37. * Measures the time between this call and the previous call,
  38. * and updates internal values accordingly.
  39. **/
  40. double step();
  41. /**
  42. * Tries to sleep for the specified amount of time. The precision is
  43. * usually 1ms.
  44. * @param seconds The number of seconds to sleep for.
  45. **/
  46. void sleep(double seconds) const;
  47. /**
  48. * Gets the time between the last two frames, assuming step is called
  49. * each frame.
  50. **/
  51. double getDelta() const;
  52. /**
  53. * Gets the average FPS over the last second. Beucase the value is only updated
  54. * once per second, it does not look erratic when displayed on screen.
  55. * @return The "current" FPS.
  56. **/
  57. int getFPS() const;
  58. /**
  59. * Gets the average delta time (seconds per frame) over the last second.
  60. **/
  61. double getAverageDelta() const;
  62. /**
  63. * Gets the amount of time in seconds passed since its first invocation
  64. * (which happens as part of Timer::step at the start of love.run).
  65. * Useful for profiling code or measuring intervals.
  66. * The time is microsecond-precise, and increases monotonically.
  67. * @return The time (in seconds)
  68. **/
  69. static double getTime();
  70. private:
  71. // Frame delta vars.
  72. double currTime;
  73. double prevTime;
  74. double prevFpsUpdate;
  75. // Updated with a certain frequency.
  76. int fps;
  77. double averageDelta;
  78. // The frequency by which to update the FPS.
  79. double fpsUpdateFrequency;
  80. // Frames since last FPS update.
  81. int frames;
  82. // The current timestep.
  83. double dt;
  84. // Returns the timer period on some platforms.
  85. static double getTimerPeriod();
  86. // Like getTime, but relative to an unspecified time.
  87. static double getTimeAbsolute();
  88. }; // Timer
  89. } // timer
  90. } // love
  91. #endif // LOVE_TIMER_TIMER_H