diff options
Diffstat (limited to 'libs/cocos2d/CCScheduler.h')
-rwxr-xr-x | libs/cocos2d/CCScheduler.h | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/libs/cocos2d/CCScheduler.h b/libs/cocos2d/CCScheduler.h new file mode 100755 index 0000000..122b8fe --- /dev/null +++ b/libs/cocos2d/CCScheduler.h | |||
@@ -0,0 +1,199 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2008-2010 Ricardo Quesada | ||
5 | * Copyright (c) 2011 Zynga Inc. | ||
6 | * | ||
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
8 | * of this software and associated documentation files (the "Software"), to deal | ||
9 | * in the Software without restriction, including without limitation the rights | ||
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
11 | * copies of the Software, and to permit persons to whom the Software is | ||
12 | * furnished to do so, subject to the following conditions: | ||
13 | * | ||
14 | * The above copyright notice and this permission notice shall be included in | ||
15 | * all copies or substantial portions of the Software. | ||
16 | * | ||
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
23 | * THE SOFTWARE. | ||
24 | */ | ||
25 | |||
26 | |||
27 | |||
28 | #import "Support/uthash.h" | ||
29 | #import "ccTypes.h" | ||
30 | |||
31 | typedef void (*TICK_IMP)(id, SEL, ccTime); | ||
32 | |||
33 | // | ||
34 | // CCTimer | ||
35 | // | ||
36 | /** Light weight timer */ | ||
37 | @interface CCTimer : NSObject | ||
38 | { | ||
39 | id target; | ||
40 | TICK_IMP impMethod; | ||
41 | |||
42 | ccTime elapsed; | ||
43 | |||
44 | @public // optimization | ||
45 | ccTime interval; | ||
46 | SEL selector; | ||
47 | } | ||
48 | |||
49 | /** interval in seconds */ | ||
50 | @property (nonatomic,readwrite,assign) ccTime interval; | ||
51 | |||
52 | /** Allocates a timer with a target and a selector. | ||
53 | */ | ||
54 | +(id) timerWithTarget:(id) t selector:(SEL)s; | ||
55 | |||
56 | /** Allocates a timer with a target, a selector and an interval in seconds. | ||
57 | */ | ||
58 | +(id) timerWithTarget:(id) t selector:(SEL)s interval:(ccTime)seconds; | ||
59 | |||
60 | /** Initializes a timer with a target and a selector. | ||
61 | */ | ||
62 | -(id) initWithTarget:(id) t selector:(SEL)s; | ||
63 | |||
64 | /** Initializes a timer with a target, a selector and an interval in seconds. | ||
65 | */ | ||
66 | -(id) initWithTarget:(id) t selector:(SEL)s interval:(ccTime)seconds; | ||
67 | |||
68 | |||
69 | /** triggers the timer */ | ||
70 | -(void) update: (ccTime) dt; | ||
71 | @end | ||
72 | |||
73 | |||
74 | |||
75 | // | ||
76 | // CCScheduler | ||
77 | // | ||
78 | /** Scheduler is responsible of triggering the scheduled callbacks. | ||
79 | You should not use NSTimer. Instead use this class. | ||
80 | |||
81 | There are 2 different types of callbacks (selectors): | ||
82 | |||
83 | - update selector: the 'update' selector will be called every frame. You can customize the priority. | ||
84 | - custom selector: A custom selector will be called every frame, or with a custom interval of time | ||
85 | |||
86 | The 'custom selectors' should be avoided when possible. It is faster, and consumes less memory to use the 'update selector'. | ||
87 | |||
88 | */ | ||
89 | |||
90 | struct _listEntry; | ||
91 | struct _hashSelectorEntry; | ||
92 | struct _hashUpdateEntry; | ||
93 | |||
94 | @interface CCScheduler : NSObject | ||
95 | { | ||
96 | ccTime timeScale_; | ||
97 | |||
98 | // | ||
99 | // "updates with priority" stuff | ||
100 | // | ||
101 | struct _listEntry *updatesNeg; // list of priority < 0 | ||
102 | struct _listEntry *updates0; // list priority == 0 | ||
103 | struct _listEntry *updatesPos; // list priority > 0 | ||
104 | struct _hashUpdateEntry *hashForUpdates; // hash used to fetch quickly the list entries for pause,delete,etc. | ||
105 | |||
106 | // Used for "selectors with interval" | ||
107 | struct _hashSelectorEntry *hashForSelectors; | ||
108 | struct _hashSelectorEntry *currentTarget; | ||
109 | BOOL currentTargetSalvaged; | ||
110 | |||
111 | // Optimization | ||
112 | TICK_IMP impMethod; | ||
113 | SEL updateSelector; | ||
114 | |||
115 | BOOL updateHashLocked; // If true unschedule will not remove anything from a hash. Elements will only be marked for deletion. | ||
116 | } | ||
117 | |||
118 | /** Modifies the time of all scheduled callbacks. | ||
119 | You can use this property to create a 'slow motion' or 'fast fordward' effect. | ||
120 | Default is 1.0. To create a 'slow motion' effect, use values below 1.0. | ||
121 | To create a 'fast fordward' effect, use values higher than 1.0. | ||
122 | @since v0.8 | ||
123 | @warning It will affect EVERY scheduled selector / action. | ||
124 | */ | ||
125 | @property (nonatomic,readwrite) ccTime timeScale; | ||
126 | |||
127 | /** returns a shared instance of the Scheduler */ | ||
128 | +(CCScheduler *)sharedScheduler; | ||
129 | |||
130 | /** purges the shared scheduler. It releases the retained instance. | ||
131 | @since v0.99.0 | ||
132 | */ | ||
133 | +(void)purgeSharedScheduler; | ||
134 | |||
135 | /** 'tick' the scheduler. | ||
136 | You should NEVER call this method, unless you know what you are doing. | ||
137 | */ | ||
138 | -(void) tick:(ccTime)dt; | ||
139 | |||
140 | /** The scheduled method will be called every 'interval' seconds. | ||
141 | If paused is YES, then it won't be called until it is resumed. | ||
142 | If 'interval' is 0, it will be called every frame, but if so, it recommened to use 'scheduleUpdateForTarget:' instead. | ||
143 | If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again. | ||
144 | |||
145 | @since v0.99.3 | ||
146 | */ | ||
147 | -(void) scheduleSelector:(SEL)selector forTarget:(id)target interval:(ccTime)interval paused:(BOOL)paused; | ||
148 | |||
149 | /** Schedules the 'update' selector for a given target with a given priority. | ||
150 | The 'update' selector will be called every frame. | ||
151 | The lower the priority, the earlier it is called. | ||
152 | @since v0.99.3 | ||
153 | */ | ||
154 | -(void) scheduleUpdateForTarget:(id)target priority:(NSInteger)priority paused:(BOOL)paused; | ||
155 | |||
156 | /** Unshedules a selector for a given target. | ||
157 | If you want to unschedule the "update", use unscheudleUpdateForTarget. | ||
158 | @since v0.99.3 | ||
159 | */ | ||
160 | -(void) unscheduleSelector:(SEL)selector forTarget:(id)target; | ||
161 | |||
162 | /** Unschedules the update selector for a given target | ||
163 | @since v0.99.3 | ||
164 | */ | ||
165 | -(void) unscheduleUpdateForTarget:(id)target; | ||
166 | |||
167 | /** Unschedules all selectors for a given target. | ||
168 | This also includes the "update" selector. | ||
169 | @since v0.99.3 | ||
170 | */ | ||
171 | -(void) unscheduleAllSelectorsForTarget:(id)target; | ||
172 | |||
173 | /** Unschedules all selectors from all targets. | ||
174 | You should NEVER call this method, unless you know what you are doing. | ||
175 | |||
176 | @since v0.99.3 | ||
177 | */ | ||
178 | -(void) unscheduleAllSelectors; | ||
179 | |||
180 | /** Pauses the target. | ||
181 | All scheduled selectors/update for a given target won't be 'ticked' until the target is resumed. | ||
182 | If the target is not present, nothing happens. | ||
183 | @since v0.99.3 | ||
184 | */ | ||
185 | -(void) pauseTarget:(id)target; | ||
186 | |||
187 | /** Resumes the target. | ||
188 | The 'target' will be unpaused, so all schedule selectors/update will be 'ticked' again. | ||
189 | If the target is not present, nothing happens. | ||
190 | @since v0.99.3 | ||
191 | */ | ||
192 | -(void) resumeTarget:(id)target; | ||
193 | |||
194 | /** Returns whether or not the target is paused | ||
195 | @since v1.0.0 | ||
196 | */ | ||
197 | -(BOOL) isTargetPaused:(id)target; | ||
198 | |||
199 | @end | ||