timeri.inc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. }
  17. {Function timeGetTime : DWord; StdCall; External 'WINMM' name 'timeGetTime';}
  18. Constructor TPTCTimer.Create;
  19. Begin
  20. internal_init_timer;
  21. m_old := 0;
  22. m_time := 0;
  23. m_start := 0;
  24. m_current := 0;
  25. m_running := False;
  26. End;
  27. Constructor TPTCTimer.Create(_time : Double);
  28. Begin
  29. internal_init_timer;
  30. m_old := 0;
  31. m_time := 0;
  32. m_start := 0;
  33. m_current := 0;
  34. m_running := False;
  35. settime(_time);
  36. End;
  37. Constructor TPTCTimer.Create(Const timer : TPTCTimer);
  38. Begin
  39. internal_init_timer;
  40. ASSign(timer);
  41. End;
  42. Destructor TPTCTimer.Destroy;
  43. Begin
  44. stop;
  45. Inherited Destroy;
  46. End;
  47. Procedure TPTCTimer.Assign(Const timer : TPTCTimer);
  48. Begin
  49. If Self = timer Then
  50. Raise TPTCError.Create('self assignment is not allowed');
  51. m_old := timer.m_old;
  52. m_time := timer.m_time;
  53. m_start := timer.m_start;
  54. m_current := timer.m_current;
  55. m_running := timer.m_running;
  56. End;
  57. Function TPTCTimer.Equals(Const timer : TPTCTimer) : Boolean;
  58. Begin
  59. Equals := (m_old = timer.m_old) And (m_time = timer.m_time) And
  60. (m_start = timer.m_start) And (m_current = timer.m_current) And
  61. (m_running = timer.m_running);
  62. End;
  63. Procedure TPTCTimer.settime(_time : Double);
  64. Begin
  65. m_current := _time;
  66. m_start := clock;
  67. m_time := m_start + _time;
  68. m_old := m_time - delta;
  69. End;
  70. Procedure TPTCTimer.start;
  71. Begin
  72. If Not m_running Then
  73. Begin
  74. m_start := clock;
  75. m_old := clock;
  76. m_running := True;
  77. End;
  78. End;
  79. Procedure TPTCTimer.stop;
  80. Begin
  81. m_running := False;
  82. End;
  83. Function TPTCTimer.time : Double;
  84. Var
  85. _time : Double;
  86. Begin
  87. If m_running Then
  88. Begin
  89. _time := clock;
  90. If _time > m_time Then
  91. m_time := _time;
  92. m_current := m_time - m_start;
  93. End;
  94. time := m_current;
  95. End;
  96. Function TPTCTimer.delta : Double;
  97. Var
  98. _time : Double;
  99. _delta : Double;
  100. Begin
  101. If m_running Then
  102. Begin
  103. _time := clock;
  104. _delta := _time - m_old;
  105. m_old := _time;
  106. If _delta < 0 Then
  107. _delta := 0;
  108. delta := _delta;
  109. End
  110. Else
  111. delta := 0;
  112. End;
  113. Function TPTCTimer.resolution : Double;
  114. Begin
  115. {$IFDEF GO32V2}
  116. resolution := TimerResolution;
  117. {$ENDIF GO32V2}
  118. {$IFDEF WIN32}
  119. resolution := 1 / m_frequency;
  120. { resolution := 1 / 1000;}
  121. {$ENDIF WIN32}
  122. {$IFDEF UNIX}
  123. resolution := 1 / 1000000;
  124. {$ENDIF UNIX}
  125. End;
  126. Procedure TPTCTimer.internal_init_timer;
  127. {$IFDEF WIN32}
  128. Var
  129. _freq : QWord;
  130. {$ENDIF WIN32}
  131. Begin
  132. {$IFDEF WIN32}
  133. QueryPerformanceFrequency(PLARGE_INTEGER(@_freq));
  134. m_frequency := _freq;
  135. {$ENDIF WIN32}
  136. End;
  137. {$IFDEF GO32V2}
  138. Function TPTCTimer.clock : Double;
  139. Begin
  140. clock := GetClockTics() * TimerResolution;
  141. End;
  142. {$ENDIF GO32V2}
  143. {$IFDEF WIN32}
  144. Function TPTCTimer.clock : Double;
  145. Var
  146. _time : QWord;
  147. Begin
  148. QueryPerformanceCounter(PLARGE_INTEGER(@_time));
  149. clock := _time / m_frequency;
  150. { clock := timeGetTime / 1000;}
  151. End;
  152. {$ENDIF WIN32}
  153. {$IFDEF UNIX}
  154. Function TPTCTimer.clock : Double;
  155. Var
  156. tm : TimeVal;
  157. Begin
  158. fpGetTimeOfDay(@tm, Nil);
  159. clock := tm.tv_sec + (Double(tm.tv_usec)) / 1000000;
  160. End;
  161. {$ENDIF UNIX}