timer.macos.m 977 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #import <Foundation/Foundation.h>
  2. #include <brl.mod/blitz.mod/blitz.h>
  3. void brl_timer__TimerFired( void *data );
  4. @interface BBTimer : NSObject{
  5. NSTimer *_timer;
  6. void *_data;
  7. }
  8. -(id)initWithPeriod:(double)period data:(void*)data;
  9. -(void)stop;
  10. @end
  11. @implementation BBTimer
  12. -(id)initWithPeriod:(double)period data:(void*)data{
  13. self=[super init];
  14. _timer=[NSTimer scheduledTimerWithTimeInterval:period target:self selector:@selector(onTick:) userInfo:nil repeats:YES];
  15. _data=data;
  16. return self;
  17. }
  18. -(void)stop{
  19. [_timer invalidate];
  20. _timer=0;
  21. }
  22. -(void)onTick:(NSTimer*)timer{
  23. brl_timer__TimerFired( _data );
  24. }
  25. @end
  26. BBTimer *bbTimerStart( float hertz,BBObject *bbTimer ){
  27. BBTimer *timer=[[BBTimer alloc] initWithPeriod:1.0/hertz data:bbTimer];
  28. if( !timer ) return 0;
  29. BBRETAIN( bbTimer );
  30. return timer;
  31. }
  32. void bbTimerStop( BBTimer *timer,BBObject *bbTimer ){
  33. [timer stop];
  34. [timer release];
  35. BBRELEASE( bbTimer );
  36. }