Skip to content

Commit 3bf1531

Browse files
committed
Merge pull request #15 from spritebuilder/headless-develop
Added lifecycle events to CCDirectorDelegate
2 parents 78a50d9 + 1df3491 commit 3bf1531

File tree

8 files changed

+88
-15
lines changed

8 files changed

+88
-15
lines changed

cocos2d/CCConfiguration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#import "Platforms/CCGL.h"
3030

31+
#define CC_MINIMUM_TABLET_SCREEN_DIAGONAL 6.0
3132
extern Class CCGraphicsBufferClass;
3233
extern Class CCGraphicsBufferBindingsClass;
3334
extern Class CCRenderCommandDrawClass;

cocos2d/CCConfiguration.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,14 @@ -(NSInteger) runningDevice
171171
NSInteger ret=-1;
172172

173173
#if __CC_PLATFORM_ANDROID
174-
175174

176175
AndroidDisplayMetrics *metrics = [[AndroidDisplayMetrics alloc] init];
177176
[[CCActivity currentActivity].windowManager.defaultDisplay getMetrics:metrics];
178177
double yInches= metrics.heightPixels/metrics.ydpi;
179178
double xInches= metrics.widthPixels/metrics.xdpi;
180179
double diagonalInches = sqrt(xInches*xInches + yInches*yInches);
181-
if (diagonalInches<=6){
182-
180+
if (diagonalInches<=CC_MINIMUM_TABLET_SCREEN_DIAGONAL){
181+
183182

184183
if([CCDirector sharedDirector].contentScaleFactor > 1.0)
185184
{
@@ -198,7 +197,7 @@ -(NSInteger) runningDevice
198197
{
199198
ret = CCDeviceiPad;
200199
}
201-
200+
202201
}
203202
#elif __CC_PLATFORM_IOS
204203

cocos2d/CCDirector.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ -(void) calculateDeltaTime
349349

350350
-(void) purgeCachedData
351351
{
352+
if([_delegate respondsToSelector:@selector(purgeCachedData)])
353+
{
354+
[_delegate purgeCachedData];
355+
}
356+
352357
[CCRENDERSTATE_CACHE flush];
353358
[CCLabelBMFont purgeCachedData];
354359
if ([_sharedDirector view])
@@ -689,6 +694,11 @@ - (void)startTransition:(CCTransition *)transition
689694

690695
-(void) end
691696
{
697+
if([_delegate respondsToSelector:@selector(end)])
698+
{
699+
[_delegate end];
700+
}
701+
692702
[_runningScene onExitTransitionDidStart];
693703
[_runningScene onExit];
694704
[_runningScene cleanup];
@@ -784,6 +794,11 @@ -(void) pause
784794
if( _isPaused )
785795
return;
786796

797+
if([_delegate respondsToSelector:@selector(pause)])
798+
{
799+
[_delegate pause];
800+
}
801+
787802
_oldAnimationInterval = _animationInterval;
788803

789804
// when paused, don't consume CPU
@@ -799,6 +814,11 @@ -(void) resume
799814
if( ! _isPaused )
800815
return;
801816

817+
if([_delegate respondsToSelector:@selector(resume)])
818+
{
819+
[_delegate resume];
820+
}
821+
802822
[self setAnimationInterval: _oldAnimationInterval];
803823

804824
if( gettimeofday( &_lastUpdate, NULL) != 0 ) {
@@ -814,6 +834,11 @@ -(void) resume
814834

815835
- (void)startAnimation
816836
{
837+
if([_delegate respondsToSelector:@selector(startAnimation)])
838+
{
839+
[_delegate startAnimation];
840+
}
841+
817842
_nextDeltaTimeZero = YES;
818843
}
819844

cocos2d/CCProtocols.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,43 @@
134134
@protocol CCDirectorDelegate <NSObject>
135135

136136
@optional
137+
138+
/** Ends the execution, releases the running scene.
139+
It doesn't remove the OpenGL view from its parent. You have to do it manually.
140+
*/
141+
-(void) end;
142+
143+
/** Pauses the running scene.
144+
The running scene will be _drawed_ but all scheduled timers will be paused
145+
While paused, the draw rate will be 4 FPS to reduce CPU consumption
146+
*/
147+
-(void) pause;
148+
149+
/** Resumes the paused scene
150+
The scheduled timers will be activated again.
151+
The "delta time" will be 0 (as if the game wasn't paused)
152+
*/
153+
-(void) resume;
154+
155+
/** Stops the animation. Nothing will be drawn. The main loop won't be triggered anymore.
156+
If you want to pause your animation call [pause] instead.
157+
*/
158+
-(void) stopAnimation;
159+
160+
/** The main loop is triggered again.
161+
Call this function only if [stopAnimation] was called earlier
162+
@warning Don't call this function to start the main loop. To run the main loop call runWithScene
163+
*/
164+
-(void) startAnimation;
165+
166+
#pragma mark Director - Memory Helper
167+
168+
/** Removes all the cocos2d data that was cached automatically.
169+
It will purge the CCTextureCache, CCLabelBMFont cache.
170+
IMPORTANT: The CCSpriteFrameCache won't be purged. If you want to purge it, you have to purge it manually.
171+
*/
172+
-(void) purgeCachedData;
173+
137174
/** Called by CCDirector when the projection is updated, and "custom" projection is used */
138175
-(GLKMatrix4) updateProjection;
139176

cocos2d/Platforms/Android/CCActivity.m

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,21 @@ - (void)onDestroy
137137
- (void)onResume
138138
{
139139
#if USE_MAIN_THREAD
140-
[self resume];
140+
[self handleResume];
141141
#else
142142
if(_thread == nil)
143143
{
144144
[super onResume];
145145
return;
146146
}
147147

148-
[self performSelector:@selector(resume) onThread:_thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
148+
[self performSelector:@selector(handleResume) onThread:_thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
149149
#endif
150150

151151
[super onResume];
152152
}
153153

154-
- (void)resume
154+
- (void)handleResume
155155
{
156156
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
157157
[[CCDirector sharedDirector] resume];
@@ -160,21 +160,21 @@ - (void)resume
160160
- (void)onPause
161161
{
162162
#if USE_MAIN_THREAD
163-
[self pause];
163+
[self handlePause];
164164
#else
165165
if(_thread == nil)
166166
{
167167
[super onPause];
168168
return;
169169
}
170170

171-
[self performSelector:@selector(pause) onThread:_thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
171+
[self performSelector:@selector(handlePause) onThread:_thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
172172
#endif
173173

174174
[super onPause];
175175
}
176176

177-
- (void)pause
177+
- (void)handlePause
178178
{
179179
[[CCDirector sharedDirector] pause];
180180
}
@@ -183,21 +183,21 @@ - (void)pause
183183
- (void)onLowMemory
184184
{
185185
#if USE_MAIN_THREAD
186-
[self purgeChaceData];
186+
[self handleLowMemory];
187187
#else
188188
if(_thread == nil)
189189
{
190190
[super onLowMemory];
191191
return;
192192
}
193193

194-
[self performSelector:@selector(purgeChaceData) onThread:_thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
194+
[self performSelector:@selector(handleLowMemory) onThread:_thread withObject:nil waitUntilDone:YES modes:@[NSDefaultRunLoopMode]];
195195
#endif
196196

197197
[super onLowMemory];
198198
}
199199

200-
- (void)purgeChaceData
200+
- (void)handleLowMemory
201201
{
202202
[[CCDirector sharedDirector] purgeCachedData];
203203
}

cocos2d/Platforms/Android/CCDirectorAndroid.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ -(void) mainLoop:(id)sender
151151

152152
- (void)setAnimationInterval:(NSTimeInterval)interval
153153
{
154-
155154
_animationInterval = interval;
156155
if(_displayLink)
157156
{
@@ -187,6 +186,11 @@ - (void) stopAnimation
187186
if(!_animating)
188187
return;
189188

189+
if([_delegate respondsToSelector:@selector(stopAnimation)])
190+
{
191+
[_delegate stopAnimation];
192+
}
193+
190194
CCLOG(@"cocos2d: animation stopped");
191195

192196
[_displayLink invalidate];

cocos2d/Platforms/iOS/CCDirectorIOS.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ -(CGPoint)convertTouchToGL:(CCTouch*)touch
173173

174174
-(void) end
175175
{
176-
177176
[super end];
178177
}
179178

@@ -362,6 +361,11 @@ - (void) stopAnimation
362361
if(!_animating)
363362
return;
364363

364+
if([_delegate respondsToSelector:@selector(stopAnimation)])
365+
{
366+
[_delegate stopAnimation];
367+
}
368+
365369
CCLOG(@"cocos2d: animation stopped");
366370

367371
#if CC_DIRECTOR_IOS_USE_BACKGROUND_THREAD

cocos2d/Support/NSAttributedString+CCAdditions.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#import "cocos2d.h"
3131
#import <CoreText/CoreText.h>
3232

33+
BOOL NSMutableAttributedStringSetDefaultAttribute(NSMutableAttributedString *attrString, NSString*attr, id defaultValue);
34+
CGColorRef CGColorCreateWithPlatformSpecificColor(id platformColor);
35+
CTFontRef CTFontCreateWithPlatformSpecificFont(id font);
3336

3437
BOOL NSMutableAttributedStringSetDefaultAttribute(NSMutableAttributedString *attrString, NSString*attr, id defaultValue){
3538
NSRange fullRange = NSMakeRange(0, attrString.length);

0 commit comments

Comments
 (0)