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.

189 lines
4.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. SDL2_framerate.c: framerate manager
  3. Copyright (C) 2012-2014 Andreas Schiffler
  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. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. Andreas Schiffler -- aschiffler at ferzkopp dot net
  19. */
  20. #include "SDL2_framerate.h"
  21. /*!
  22. \brief Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
  23. \return The tick count.
  24. */
  25. Uint32 _getTicks()
  26. {
  27. Uint32 ticks = SDL_GetTicks();
  28. /*
  29. * Since baseticks!=0 is used to track initialization
  30. * we need to ensure that the tick count is always >0
  31. * since SDL_GetTicks may not have incremented yet and
  32. * return 0 depending on the timing of the calls.
  33. */
  34. if (ticks == 0) {
  35. return 1;
  36. } else {
  37. return ticks;
  38. }
  39. }
  40. /*!
  41. \brief Initialize the framerate manager.
  42. Initialize the framerate manager, set default framerate of 30Hz and
  43. reset delay interpolation.
  44. \param manager Pointer to the framerate manager.
  45. */
  46. void SDL_initFramerate(FPSmanager * manager)
  47. {
  48. /*
  49. * Store some sane values
  50. */
  51. manager->framecount = 0;
  52. manager->rate = FPS_DEFAULT;
  53. manager->rateticks = (1000.0f / (float) FPS_DEFAULT);
  54. manager->baseticks = _getTicks();
  55. manager->lastticks = manager->baseticks;
  56. }
  57. /*!
  58. \brief Set the framerate in Hz
  59. Sets a new framerate for the manager and reset delay interpolation.
  60. Rate values must be between FPS_LOWER_LIMIT and FPS_UPPER_LIMIT inclusive to be accepted.
  61. \param manager Pointer to the framerate manager.
  62. \param rate The new framerate in Hz (frames per second).
  63. \return 0 for sucess and -1 for error.
  64. */
  65. int SDL_setFramerate(FPSmanager * manager, Uint32 rate)
  66. {
  67. if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) {
  68. manager->framecount = 0;
  69. manager->rate = rate;
  70. manager->rateticks = (1000.0f / (float) rate);
  71. return (0);
  72. } else {
  73. return (-1);
  74. }
  75. }
  76. /*!
  77. \brief Return the current target framerate in Hz
  78. Get the currently set framerate of the manager.
  79. \param manager Pointer to the framerate manager.
  80. \return Current framerate in Hz or -1 for error.
  81. */
  82. int SDL_getFramerate(FPSmanager * manager)
  83. {
  84. if (manager == NULL) {
  85. return (-1);
  86. } else {
  87. return ((int)manager->rate);
  88. }
  89. }
  90. /*!
  91. \brief Return the current framecount.
  92. Get the current framecount from the framerate manager.
  93. A frame is counted each time SDL_framerateDelay is called.
  94. \param manager Pointer to the framerate manager.
  95. \return Current frame count or -1 for error.
  96. */
  97. int SDL_getFramecount(FPSmanager * manager)
  98. {
  99. if (manager == NULL) {
  100. return (-1);
  101. } else {
  102. return ((int)manager->framecount);
  103. }
  104. }
  105. /*!
  106. \brief Delay execution to maintain a constant framerate and calculate fps.
  107. Generate a delay to accomodate currently set framerate. Call once in the
  108. graphics/rendering loop. If the computer cannot keep up with the rate (i.e.
  109. drawing too slow), the delay is zero and the delay interpolation is reset.
  110. \param manager Pointer to the framerate manager.
  111. \return The time that passed since the last call to the function in ms. May return 0.
  112. */
  113. Uint32 SDL_framerateDelay(FPSmanager * manager)
  114. {
  115. Uint32 current_ticks;
  116. Uint32 target_ticks;
  117. Uint32 the_delay;
  118. Uint32 time_passed = 0;
  119. /*
  120. * No manager, no delay
  121. */
  122. if (manager == NULL) {
  123. return 0;
  124. }
  125. /*
  126. * Initialize uninitialized manager
  127. */
  128. if (manager->baseticks == 0) {
  129. SDL_initFramerate(manager);
  130. }
  131. /*
  132. * Next frame
  133. */
  134. manager->framecount++;
  135. /*
  136. * Get/calc ticks
  137. */
  138. current_ticks = _getTicks();
  139. time_passed = current_ticks - manager->lastticks;
  140. manager->lastticks = current_ticks;
  141. target_ticks = manager->baseticks + (Uint32) ((float) manager->framecount * manager->rateticks);
  142. if (current_ticks <= target_ticks) {
  143. the_delay = target_ticks - current_ticks;
  144. SDL_Delay(the_delay);
  145. } else {
  146. manager->framecount = 0;
  147. manager->baseticks = _getTicks();
  148. }
  149. return time_passed;
  150. }