sdlticks.pas 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. unit sdlticks;
  2. {
  3. $Id: sdlticks.pas,v 1.1 2004/09/30 22:35:47 savage Exp $
  4. }
  5. {******************************************************************************}
  6. { }
  7. { JEDI-SDL : Pascal units for SDL - Simple DirectMedia Layer }
  8. { SDL GetTicks Class Wrapper }
  9. { }
  10. { }
  11. { The initial developer of this Pascal code was : }
  12. { Dominique Louis <[email protected]> }
  13. { }
  14. { Portions created by Dominique Louis are }
  15. { Copyright (C) 2004 - 2100 Dominique Louis. }
  16. { }
  17. { }
  18. { Contributor(s) }
  19. { -------------- }
  20. { Dominique Louis <[email protected]> }
  21. { }
  22. { Obtained through: }
  23. { Joint Endeavour of Delphi Innovators ( Project JEDI ) }
  24. { }
  25. { You may retrieve the latest version of this file at the Project }
  26. { JEDI home page, located at http://delphi-jedi.org }
  27. { }
  28. { The contents of this file are used with permission, subject to }
  29. { the Mozilla Public License Version 1.1 (the "License"); you may }
  30. { not use this file except in compliance with the License. You may }
  31. { obtain a copy of the License at }
  32. { http://www.mozilla.org/MPL/MPL-1.1.html }
  33. { }
  34. { Software distributed under the License is distributed on an }
  35. { "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or }
  36. { implied. See the License for the specific language governing }
  37. { rights and limitations under the License. }
  38. { }
  39. { Description }
  40. { ----------- }
  41. { SDL Window Wrapper }
  42. { }
  43. { }
  44. { Requires }
  45. { -------- }
  46. { SDL.dll on Windows platforms }
  47. { libSDL-1.1.so.0 on Linux platform }
  48. { }
  49. { Programming Notes }
  50. { ----------------- }
  51. { }
  52. { }
  53. { }
  54. { }
  55. { Revision History }
  56. { ---------------- }
  57. { }
  58. { September 23 2004 - DL : Initial Creation }
  59. {
  60. $Log: sdlticks.pas,v $
  61. Revision 1.1 2004/09/30 22:35:47 savage
  62. Changes, enhancements and additions as required to get SoAoS working.
  63. }
  64. {******************************************************************************}
  65. interface
  66. uses
  67. sdl;
  68. type
  69. TSDLTicks = class
  70. private
  71. m_startTime : Int64;
  72. m_ticksPerSecond : Int64;
  73. s_lastTime : Int64;
  74. public
  75. constructor Create;
  76. destructor Destroy; override; // destructor
  77. {*****************************************************************************
  78. Init
  79. If the hi-res timer is present, the tick rate is stored and the function
  80. returns true. Otherwise, the function returns false, and the timer should
  81. not be used.
  82. *****************************************************************************}
  83. function Init : boolean;
  84. function GetElapsedSeconds( elapsedFrames : Cardinal = 1 ) : single;
  85. {***************************************************************************
  86. GetFPS
  87. Returns the average frames per second over elapsedFrames, which defaults to
  88. one. If this is not called every frame, the client should track the number
  89. of frames itself, and reset the value after this is called.
  90. ***************************************************************************}
  91. function GetFPS( elapsedFrames : Cardinal = 1 ) : single;
  92. {***************************************************************************
  93. LockFPS
  94. Used to lock the frame rate to a set amount. This will block until enough
  95. time has passed to ensure that the fps won't go over the requested amount.
  96. Note that this can only keep the fps from going above the specified level;
  97. it can still drop below it. It is assumed that if used, this function will
  98. be called every frame. The value returned is the instantaneous fps, which
  99. will be <= targetFPS.
  100. ***************************************************************************}
  101. function LockFPS( targetFPS : Byte ) : single;
  102. end;
  103. implementation
  104. { TSDLTicks }
  105. constructor TSDLTicks.Create;
  106. begin
  107. end;
  108. destructor TSDLTicks.Destroy;
  109. begin
  110. inherited;
  111. end;
  112. function TSDLTicks.GetElapsedSeconds( elapsedFrames: Cardinal ): single;
  113. var
  114. currentTime : Int64;
  115. begin
  116. // s_lastTime := m_startTime;
  117. currentTime := SDL_GetTicks;
  118. //QueryPerformanceCounter( currentTime );
  119. result := (currentTime - s_lastTime) / m_ticksPerSecond;
  120. // reset the timer
  121. s_lastTime := currentTime;
  122. end;
  123. function TSDLTicks.GetFPS( elapsedFrames: Cardinal ): single;
  124. var
  125. currentTime : integer;
  126. fps : single;
  127. begin
  128. // s_lastTime := m_startTime;
  129. currentTime := SDL_GetTicks;
  130. fps := elapsedFrames * m_ticksPerSecond / ( currentTime - s_lastTime);
  131. // reset the timer
  132. s_lastTime := currentTime;
  133. result := fps;
  134. end;
  135. function TSDLTicks.Init: boolean;
  136. begin
  137. m_startTime := SDL_GetTicks;
  138. s_lastTime := m_startTime;
  139. m_ticksPerSecond := 1000;
  140. result := true;
  141. end;
  142. function TSDLTicks.LockFPS(targetFPS: Byte): single;
  143. var
  144. currentTime : integer;
  145. fps : single;
  146. begin
  147. if (targetFPS = 0) then
  148. targetFPS := 1;
  149. s_lastTime := m_startTime;
  150. // delay to maintain a constant frame rate
  151. repeat
  152. currentTime := SDL_GetTicks;
  153. fps := m_ticksPerSecond / (currentTime - s_lastTime);
  154. until (fps > targetFPS);
  155. // reset the timer
  156. s_lastTime := m_startTime;
  157. result := fps;
  158. end;
  159. end.