My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* cocos2d for iPhone
*
* http://www.cocos2d-iphone.org
*
* Copyright (C) 2008,2009 Ricardo Quesada
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the 'cocos2d for iPhone' license.
*
* You will find a copy of this license within the cocos2d for iPhone
* distribution inside the "LICENSE" file.
*
*/

// cocos2d imports
#import "Scheduler.h"
#import "ccMacros.h"

//
// Timer
//
@implementation Timer

@synthesize interval;

-(id) init
{
NSException* myException = [NSException
exceptionWithName:@"TimerInvalid"
reason:@"Invalid init for Timer. Use initWithTarget:sel:"
userInfo:nil];
@throw myException;
}

+(id) timerWithTarget:(id) t selector:(SEL)s
{
return [[[self alloc] initWithTarget:t selector:s] autorelease];
}

+(id) timerWithTarget:(id) t selector:(SEL)s interval:(ccTime) i
{
return [[[self alloc] initWithTarget:t selector:s interval:i] autorelease];
}

-(id) initWithTarget:(id) t selector:(SEL)s
{
return [self initWithTarget:t selector:s interval:0];
}

-(id) initWithTarget:(id) t selector:(SEL)s interval:(ccTime) seconds
{
if( (self=[super init]) ) {
#ifdef DEBUG
NSMethodSignature *sig = [t methodSignatureForSelector:s];
NSAssert(sig !=0 , @"Signature not found for selector - does it have the following form? -(void) name: (ccTime) dt");
#endif

// target is being retained. Be careful with ciruclar references
target = [t retain];
selector = s;
impMethod = (TICK_IMP) [t methodForSelector:s];
elapsed = -1;
interval = seconds;
}
return self;
}

- (NSString*) description
{
return [NSString stringWithFormat:@"<%@ = %08X | target:%@ selector:(%@)>", [self class], self, [target class], NSStringFromSelector(selector)];
}

-(void) dealloc
{
CCLOG(@"cocos2d: deallocing %@", self);
[target release];
[super dealloc];
}

-(void) fire: (ccTime) dt
{
if( elapsed == - 1)
elapsed = 0;
else
elapsed += dt;
if( elapsed >= interval ) {
impMethod(target, selector, elapsed);
elapsed = 0;
}
}
@end

//
// Scheduler
//
@implementation Scheduler

static Scheduler *sharedScheduler;

@synthesize timeScale = timeScale_;

+ (Scheduler *)sharedScheduler
{
@synchronized([Scheduler class])
{
if (!sharedScheduler)
[[Scheduler alloc] init];

return sharedScheduler;
}
// to avoid compiler warning
return nil;
}

+(id)alloc
{
@synchronized([Scheduler class])
{
NSAssert(sharedScheduler == nil, @"Attempted to allocate a second instance of a singleton.");
sharedScheduler = [super alloc];
return sharedScheduler;
}
// to avoid compiler warning
return nil;
}

- (id) init
{
if( (self=[super init]) ) {
scheduledMethods = [[NSMutableArray arrayWithCapacity:50] retain];
methodsToRemove = [[NSMutableArray arrayWithCapacity:20] retain];
methodsToAdd = [[NSMutableArray arrayWithCapacity:20] retain];

timeScale_ = 1.0f;
}

return self;
}

- (void) dealloc
{
CCLOG(@"cocos2d: deallocing %@", self);
[scheduledMethods release];
[methodsToRemove release];
[methodsToAdd release];
sharedScheduler = nil;

[super dealloc];
}

-(void) scheduleTimer: (Timer*) t
{
// it is possible that sometimes (in transitions in particular) an scene unschedule a timer
// and before the timer is deleted, it is re-scheduled
if( [methodsToRemove containsObject:t] )
{
[methodsToRemove removeObject:t];
return;
}

if( [scheduledMethods containsObject:t] || [methodsToAdd containsObject:t]) {
NSLog(@"Scheduler.schedulerTimer: timer %@ already scheduled", t);
NSException* myException = [NSException
exceptionWithName:@"SchedulerTimerAlreadyScheduled"
reason:@"Scheduler.scheduleTimer already scheduled"
userInfo:nil];
@throw myException;
}

[methodsToAdd addObject: t];
}

-(void) unscheduleTimer: (Timer*) t;
{
// someone wants to remove it before it was added
if( [methodsToAdd containsObject:t] ) {
[methodsToAdd removeObject:t];
return;
}

if( ![scheduledMethods containsObject:t] ) {
NSLog(@"Scheduler.unscheduleTimer: timer not scheduled");
NSException* myException = [NSException
exceptionWithName:@"SchedulerTimerNotFound"
reason:@"Scheduler.unscheduleTimer not found"
userInfo:nil];
@throw myException;
}

[methodsToRemove addObject:t];
}

-(void) unscheduleAllTimers
{
[methodsToAdd removeAllObjects];
[methodsToRemove removeAllObjects];
[scheduledMethods removeAllObjects];
}

-(void) tick: (ccTime) dt
{
if( timeScale_ != 1.0f )
dt *= timeScale_;

for( id k in methodsToRemove )
[scheduledMethods removeObject:k];

[methodsToRemove removeAllObjects];

for( id k in methodsToAdd )
[scheduledMethods addObject:k];
[methodsToAdd removeAllObjects];

for( Timer *t in scheduledMethods )
[t fire: dt];
}
@end

Change log

r1404 by ricardoquesada on Sep 26, 2009   Diff
uses "cocos2d" prefix with CCLOG
Go to: 
Project members, sign in to write a code review

Older revisions

r1238 by ricardoquesada on Aug 5, 2009   Diff
part of  issue #484 
little fixes
r1189 by ricardoquesada on Jul 27, 2009   Diff
fixed  issue #463 
r1134 by ricardoquesada on Jul 13, 2009   Diff
part of  issue #421 
Director#end releases all singletons
and it's cache.
All revisions of this file

File info

Size: 4779 bytes, 217 lines
Powered by Google Project Hosting