sdlticks.pas 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. unit sdlticks;
  2. {
  3. $Id: sdlticks.pas,v 1.2 2006/11/08 08:22:48 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.2 2006/11/08 08:22:48 savage
  62. updates tp sdlgameinterface and sdlticks functions.
  63. Revision 1.1 2004/09/30 22:35:47 savage
  64. Changes, enhancements and additions as required to get SoAoS working.
  65. }
  66. {******************************************************************************}
  67. interface
  68. uses
  69. sdl;
  70. type
  71. TSDLTicks = class
  72. private
  73. FStartTime : UInt32;
  74. FTicksPerSecond : UInt32;
  75. FElapsedLastTime : UInt32;
  76. FFPSLastTime : UInt32;
  77. FLockFPSLastTime : UInt32;
  78. public
  79. constructor Create;
  80. destructor Destroy; override; // destructor
  81. {*****************************************************************************
  82. Init
  83. If the hi-res timer is present, the tick rate is stored and the function
  84. returns true. Otherwise, the function returns false, and the timer should
  85. not be used.
  86. *****************************************************************************}
  87. function Init : boolean;
  88. {***************************************************************************
  89. GetGetElapsedSeconds
  90. Returns the Elapsed time, since the function was last called.
  91. ***************************************************************************}
  92. function GetElapsedSeconds : Single;
  93. {***************************************************************************
  94. GetFPS
  95. Returns the average frames per second.
  96. If this is not called every frame, the client should track the number
  97. of frames itself, and reset the value after this is called.
  98. ***************************************************************************}
  99. function GetFPS : single;
  100. {***************************************************************************
  101. LockFPS
  102. Used to lock the frame rate to a set amount. This will block until enough
  103. time has passed to ensure that the fps won't go over the requested amount.
  104. Note that this can only keep the fps from going above the specified level;
  105. it can still drop below it. It is assumed that if used, this function will
  106. be called every frame. The value returned is the instantaneous fps, which
  107. will be less than or equal to the targetFPS.
  108. ***************************************************************************}
  109. procedure LockFPS( targetFPS : Byte );
  110. end;
  111. implementation
  112. { TSDLTicks }
  113. constructor TSDLTicks.Create;
  114. begin
  115. inherited;
  116. FTicksPerSecond := 1000;
  117. end;
  118. destructor TSDLTicks.Destroy;
  119. begin
  120. inherited;
  121. end;
  122. function TSDLTicks.GetElapsedSeconds : Single;
  123. var
  124. currentTime : Cardinal;
  125. begin
  126. currentTime := SDL_GetTicks;
  127. result := ( currentTime - FElapsedLastTime ) / FTicksPerSecond;
  128. // reset the timer
  129. FElapsedLastTime := currentTime;
  130. end;
  131. function TSDLTicks.GetFPS : Single;
  132. var
  133. currentTime, FrameTime : UInt32;
  134. fps : single;
  135. begin
  136. currentTime := SDL_GetTicks;
  137. FrameTime := ( currentTime - FFPSLastTime );
  138. if FrameTime = 0 then
  139. FrameTime := 1;
  140. fps := FTicksPerSecond / FrameTime;
  141. // reset the timer
  142. FFPSLastTime := currentTime;
  143. result := fps;
  144. end;
  145. function TSDLTicks.Init : boolean;
  146. begin
  147. FStartTime := SDL_GetTicks;
  148. FElapsedLastTime := FStartTime;
  149. FFPSLastTime := FStartTime;
  150. FLockFPSLastTime := FStartTime;
  151. result := true;
  152. end;
  153. procedure TSDLTicks.LockFPS( targetFPS : Byte );
  154. var
  155. currentTime : UInt32;
  156. targetTime : single;
  157. begin
  158. if ( targetFPS = 0 ) then
  159. targetFPS := 1;
  160. targetTime := FTicksPerSecond / targetFPS;
  161. // delay to maintain a constant frame rate
  162. repeat
  163. currentTime := SDL_GetTicks;
  164. until ( ( currentTime - FLockFPSLastTime ) > targetTime );
  165. // reset the timer
  166. FLockFPSLastTime := currentTime;
  167. end;
  168. end.