Timer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright (c) 2006-2011 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_SDL_TIMER_H
  21. #define LOVE_TIMER_SDL_TIMER_H
  22. // SDL
  23. #include <SDL.h>
  24. // LOVE
  25. #include <common/Module.h>
  26. namespace love
  27. {
  28. namespace timer
  29. {
  30. namespace sdl
  31. {
  32. /**
  33. * An SDL timer module. Can keep track of time between certain function
  34. * calls, and provides access to a FPS metric which updates once each second.
  35. **/
  36. class Timer : public Module
  37. {
  38. private:
  39. // Frame delta vars.
  40. Uint32 currTime;
  41. Uint32 prevTime;
  42. Uint32 prevFpsUpdate;
  43. // Updated with a certain frequency.
  44. double fps;
  45. // The frequency by which to update the FPS.
  46. double fpsUpdateFrequency;
  47. // Frames since last FPS update.
  48. int frames;
  49. // The current timestep.
  50. double dt;
  51. public:
  52. /**
  53. * Constructor. Initializes the SDL/timer subsystem.
  54. **/
  55. Timer();
  56. /**
  57. * Destructor.
  58. **/
  59. ~Timer();
  60. /**
  61. * Gets the name of the module.
  62. * @return Always returns "love.timer.sdl".
  63. **/
  64. const char * getName() const;
  65. /**
  66. * Measures the time between this call and the previous call,
  67. * and updates internal values accordinly.
  68. **/
  69. void step();
  70. /**
  71. * Tries to sleep for the specified amount of time. The precision is
  72. * usually 1ms.
  73. * @param seconds The number of seconds to sleep for.
  74. **/
  75. void sleep(double seconds);
  76. /**
  77. * Gets the time between the last two frames, assuming step is called
  78. * each frame.
  79. **/
  80. double getDelta() const;
  81. /**
  82. * Gets the average FPS over the last second. Beucase the value is only updated
  83. * once per second, it does not look erratic when displayed on screen.
  84. * @return The "current" FPS.
  85. **/
  86. double getFPS() const;
  87. /**
  88. * Gets the amount of time since the program started. Only useful for timing
  89. * code or measuring intervals.
  90. * @return The time (in seconds) since the program started.
  91. **/
  92. double getTime() const;
  93. /**
  94. * Gets the amount of time passed since an unspecified time. The time is accurate
  95. * to the microsecond, and is limited to 24 hours.
  96. * @return The time (in seconds)
  97. **/
  98. double getMicroTime() const;
  99. }; // Timer
  100. } // sdl
  101. } // timer
  102. } // love
  103. #endif // LOVE_TIMER_SDL_TIMER_H