timer.bmx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. Strict
  2. Rem
  3. bbdoc: Events/Timers
  4. End Rem
  5. Module BRL.Timer
  6. ModuleInfo "Version: 1.02"
  7. ModuleInfo "Author: Simon Armstrong, Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. Rem
  12. History:
  13. Removed use of _cycle:TTimer field to keep timer's alive - didn't work with 'real' GC!
  14. Replaced with BBRETAIN/BBRELEASE in C code.
  15. Added check for OS timer creation failure
  16. Added check for Win32 timer firing after timeKillEvent
  17. Removed brl.standardio dependancy
  18. End Rem
  19. Import BRL.System
  20. ?Win32
  21. Import "timer.win32.c"
  22. ?MacOS
  23. Import "timer.macos.m"
  24. ?Linux
  25. Import "timer.linux.c"
  26. ?
  27. Extern
  28. Function bbTimerStart( hertz#,timer:TTimer )
  29. Function bbTimerStop( handle,timer:TTimer )
  30. End Extern
  31. Function _TimerFired( timer:TTimer )
  32. timer.Fire
  33. End Function
  34. Type TTimer
  35. Method Ticks()
  36. Return _ticks
  37. End Method
  38. Method Stop()
  39. If Not _handle Return
  40. bbTimerStop _handle,Self
  41. _handle=0
  42. _event=Null
  43. ' _cycle=Null
  44. End Method
  45. Method Fire()
  46. If Not _handle Return
  47. _ticks:+1
  48. If _event
  49. EmitEvent _event
  50. Else
  51. EmitEvent CreateEvent( EVENT_TIMERTICK,Self,_ticks )
  52. EndIf
  53. End Method
  54. Method Wait()
  55. If Not _handle Return
  56. Local n
  57. Repeat
  58. WaitSystem
  59. n=_ticks-_wticks
  60. Until n
  61. _wticks:+n
  62. Return n
  63. End Method
  64. Function Create:TTimer( hertz#,event:TEvent=Null )
  65. Local t:TTimer=New TTimer
  66. Local handle=bbTimerStart( hertz,t )
  67. If Not handle Return
  68. ' t._cycle=t
  69. t._event=event
  70. t._handle=handle
  71. Return t
  72. End Function
  73. Field _ticks
  74. Field _wticks
  75. Field _cycle:TTimer 'no longer used...see history
  76. Field _event:TEvent
  77. Field _handle
  78. End Type
  79. Rem
  80. bbdoc: Create a timer
  81. returns: A new timer object
  82. about:
  83. #CreateTimer creates a timer object that 'ticks' @hertz times per second.
  84. Each time the timer ticks, @event will be emitted using #EmitEvent.
  85. If @event is Null, an event with an @id equal to EVENT_TIMERTICK and
  86. @source equal to the timer object will be emitted instead.
  87. End Rem
  88. Function CreateTimer:TTimer( hertz#,event:TEvent=Null )
  89. Return TTimer.Create( hertz,event )
  90. End Function
  91. Rem
  92. bbdoc: Get timer tick counter
  93. returns: The number of times @timer has ticked over
  94. End Rem
  95. Function TimerTicks( timer:TTimer )
  96. Return timer.Ticks()
  97. End Function
  98. Rem
  99. bbdoc: Wait until a timer ticks
  100. returns: The number of ticks since the last call to #WaitTimer
  101. End Rem
  102. Function WaitTimer( timer:TTimer )
  103. Return timer.Wait()
  104. End Function
  105. Rem
  106. bbdoc: Stop a timer
  107. about:Once stopped, a timer can no longer be used.
  108. End Rem
  109. Function StopTimer( timer:TTimer )
  110. timer.Stop
  111. End Function