File: timer.cpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (60 lines) | stat: -rw-r--r-- 1,051 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#if defined(Hiro_Timer)

@implementation CocoaTimer : NSObject

-(id) initWith:(hiro::mTimer&)timerReference {
  if(self = [super init]) {
    timer = &timerReference;
    instance = nil;
  }
  return self;
}

-(NSTimer*) instance {
  return instance;
}

-(void) update {
  if(instance) {
    [instance invalidate];
    instance = nil;
  }
  if(timer->enabled()) {
    instance = [NSTimer
      scheduledTimerWithTimeInterval:timer->state.interval / 1000.0
      target:self selector:@selector(run:) userInfo:nil repeats:YES
    ];
  }
}

-(void) run:(NSTimer*)instance {
  if(hiro::Application::state().quit) return;

  if(timer->enabled()) {
    timer->doActivate();
  }
}

@end

namespace hiro {

auto pTimer::construct() -> void {
  cocoaTimer = [[CocoaTimer alloc] initWith:self()];
}

auto pTimer::destruct() -> void {
  if([cocoaTimer instance]) [[cocoaTimer instance] invalidate];
}

auto pTimer::setEnabled(bool enabled) -> void {
  [cocoaTimer update];
}

auto pTimer::setInterval(u32 interval) -> void {
  [cocoaTimer update];
}

}

#endif