timer.bmx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. SuperStrict
  2. Rem
  3. bbdoc: Events/Timers
  4. End Rem
  5. Module BRL.Timer
  6. ModuleInfo "Version: 1.04"
  7. ModuleInfo "Author: Simon Armstrong, Mark Sibly, Bruce A Henderson"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "History: 1.04"
  11. ModuleInfo "History: New factory-based timer implementation."
  12. ModuleInfo "History: 1.03"
  13. ModuleInfo "History: Update to use Byte Ptr instead of int."
  14. Import BRL.Event
  15. Rem
  16. History:
  17. Removed use of _cycle:TTimer field to keep timer's alive - didn't work with 'real' GC!
  18. Replaced with BBRETAIN/BBRELEASE in C code.
  19. Added check for OS timer creation failure
  20. Added check for Win32 timer firing after timeKillEvent
  21. Removed brl.standardio dependancy
  22. End Rem
  23. Type TTimer Abstract
  24. Method Ticks:Int() Abstract
  25. Method Stop() Abstract
  26. Method Fire() Abstract
  27. Method Wait:Int() Abstract
  28. Function Create:TTimer( hertz#,event:TEvent=Null ) Abstract
  29. End Type
  30. Rem
  31. bbdoc: Create a timer
  32. returns: A new timer object
  33. about:
  34. #CreateTimer creates a timer object that 'ticks' @hertz times per second.
  35. Each time the timer ticks, @event will be emitted using #EmitEvent.
  36. If @event is Null, an event with an @id equal to EVENT_TIMERTICK and
  37. @source equal to the timer object will be emitted instead.
  38. End Rem
  39. Function CreateTimer:TTimer( hertz#,event:TEvent=Null )
  40. If timer_factories Then
  41. Return timer_factories.Create(hertz, event)
  42. Else
  43. Throw "No Timer installed. Maybe Import BRL.TimerDefault ?"
  44. End If
  45. End Function
  46. Rem
  47. bbdoc: Get timer tick counter
  48. returns: The number of times @timer has ticked over
  49. End Rem
  50. Function TimerTicks:Int( timer:TTimer )
  51. Return timer.Ticks()
  52. End Function
  53. Rem
  54. bbdoc: Wait until a timer ticks
  55. returns: The number of ticks since the last call to #WaitTimer
  56. End Rem
  57. Function WaitTimer:Int( timer:TTimer )
  58. Return timer.Wait()
  59. End Function
  60. Rem
  61. bbdoc: Stop a timer
  62. about:Once stopped, a timer can no longer be used.
  63. End Rem
  64. Function StopTimer( timer:TTimer )
  65. timer.Stop
  66. End Function
  67. Private
  68. Global timer_factories:TTimerFactory
  69. Public
  70. Type TTimerFactory
  71. Field _succ:TTimerFactory
  72. Method New()
  73. If _succ <> Null Then
  74. Throw "Timer already installed : " + _succ.GetName()
  75. End If
  76. _succ=timer_factories
  77. timer_factories=Self
  78. End Method
  79. Method GetName:String() Abstract
  80. Method Create:TTimer(hertz#,event:TEvent=Null) Abstract
  81. End Type