timer.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #if defined(Hiro_Timer)
  2. @implementation CocoaTimer : NSObject
  3. -(id) initWith:(hiro::mTimer&)timerReference {
  4. if(self = [super init]) {
  5. timer = &timerReference;
  6. instance = nil;
  7. }
  8. return self;
  9. }
  10. -(NSTimer*) instance {
  11. return instance;
  12. }
  13. -(void) update {
  14. if(instance) {
  15. [instance invalidate];
  16. instance = nil;
  17. }
  18. if(timer->enabled()) {
  19. instance = [NSTimer
  20. scheduledTimerWithTimeInterval:timer->state.interval / 1000.0
  21. target:self selector:@selector(run:) userInfo:nil repeats:YES
  22. ];
  23. }
  24. }
  25. -(void) run:(NSTimer*)instance {
  26. if(hiro::Application::state().quit) return;
  27. if(timer->enabled()) {
  28. timer->doActivate();
  29. }
  30. }
  31. @end
  32. namespace hiro {
  33. auto pTimer::construct() -> void {
  34. @autoreleasepool {
  35. cocoaTimer = [[CocoaTimer alloc] initWith:self()];
  36. }
  37. }
  38. auto pTimer::destruct() -> void {
  39. @autoreleasepool {
  40. if([cocoaTimer instance]) [[cocoaTimer instance] invalidate];
  41. [cocoaTimer release];
  42. }
  43. }
  44. auto pTimer::setEnabled(bool enabled) -> void {
  45. @autoreleasepool {
  46. [cocoaTimer update];
  47. }
  48. }
  49. auto pTimer::setInterval(uint interval) -> void {
  50. @autoreleasepool {
  51. [cocoaTimer update];
  52. }
  53. }
  54. }
  55. #endif