diff --git a/cocos2d/CCDirector.h b/cocos2d/CCDirector.h index c3a11de31e3..1fda4c0a921 100644 --- a/cocos2d/CCDirector.h +++ b/cocos2d/CCDirector.h @@ -146,6 +146,9 @@ and when to execute the Scenes. /* action manager associated with this director */ CCActionManager *actionManager_; + + /* OpenGLView. On iOS it is a copy of self.view */ + CCGLView *view_; } /** returns the cocos2d thread. diff --git a/cocos2d/CCDirector.m b/cocos2d/CCDirector.m index c77fe5fb0a5..71d6ab410c6 100644 --- a/cocos2d/CCDirector.m +++ b/cocos2d/CCDirector.m @@ -174,7 +174,7 @@ - (id) init - (NSString*) description { - return [NSString stringWithFormat:@"<%@ = %08X | Size: %0.f x %0.f, view = %@>", [self class], self, winSizeInPoints_.width, winSizeInPoints_.height, self.view]; + return [NSString stringWithFormat:@"<%@ = %08X | Size: %0.f x %0.f, view = %@>", [self class], self, winSizeInPoints_.width, winSizeInPoints_.height, view_]; } - (void) dealloc @@ -198,8 +198,8 @@ - (void) dealloc -(void) setGLDefaultValues { - // This method SHOULD be called only after openGLView_ was initialized - NSAssert( [self view], @"openGLView_ must be initialized"); + // This method SHOULD be called only after view_ was initialized + NSAssert( view_, @"view_ must be initialized"); [self setAlphaBlending: YES]; [self setDepthTest: YES]; @@ -300,28 +300,34 @@ - (void) setDepthTest: (BOOL) on -(void) setView:(CCGLView*)view { - NSAssert( view, @"OpenGLView must be non-nil"); +// NSAssert( view, @"OpenGLView must be non-nil"); + if( view != view_ ) { + #ifdef __CC_PLATFORM_IOS - [super setView:view]; + [super setView:view]; #endif + [view_ release]; + view_ = [view retain]; - // set size - winSizeInPixels_ = winSizeInPoints_ = CCNSSizeToCGSize( [view bounds].size ); + // set size + winSizeInPixels_ = winSizeInPoints_ = CCNSSizeToCGSize( [view_ bounds].size ); - [self setGLDefaultValues]; - [self createStatsLabel]; + [self createStatsLabel]; + + // it could be nil + if( view ) + [self setGLDefaultValues]; - CHECK_GL_ERROR_DEBUG(); + CHECK_GL_ERROR_DEBUG(); + } } -#ifdef __CC_PLATFORM_MAC -(CCGLView*) view { - // ignore on Mac - return nil; + return view_; } -#endif // + #pragma mark Director Scene Landscape @@ -422,6 +428,7 @@ -(void) end [delegate_ release]; delegate_ = nil; + [self setView:nil]; // Purge bitmap cache [CCLabelBMFont purgeCachedData]; diff --git a/cocos2d/Platforms/Mac/CCDirectorMac.h b/cocos2d/Platforms/Mac/CCDirectorMac.h index 8b95b7fab3a..63ff82fe908 100644 --- a/cocos2d/Platforms/Mac/CCDirectorMac.h +++ b/cocos2d/Platforms/Mac/CCDirectorMac.h @@ -61,9 +61,6 @@ enum { NSWindow *fullScreenWindow_; - // OpenGLView - CCGLView *view_; - // Event Dispatcher CCEventDispatcher *eventDispatcher_; diff --git a/cocos2d/Platforms/Mac/CCDirectorMac.m b/cocos2d/Platforms/Mac/CCDirectorMac.m index c530d54f7db..1749da15ad0 100644 --- a/cocos2d/Platforms/Mac/CCDirectorMac.m +++ b/cocos2d/Platforms/Mac/CCDirectorMac.m @@ -195,9 +195,6 @@ -(void) setView:(CCGLView *)view { if( view != view_) { - [view_ release]; - view_ = [view retain]; - [super setView:view]; // cache the NSWindow and NSOpenGLView created from the NIB @@ -208,11 +205,6 @@ -(void) setView:(CCGLView *)view } } --(CCGLView*) view -{ - return view_; -} - -(int) resizeMode { return resizeMode_; diff --git a/cocos2d/Platforms/iOS/CCDirectorIOS.m b/cocos2d/Platforms/iOS/CCDirectorIOS.m index 3a2a6698a80..0db10502cdd 100644 --- a/cocos2d/Platforms/iOS/CCDirectorIOS.m +++ b/cocos2d/Platforms/iOS/CCDirectorIOS.m @@ -266,7 +266,7 @@ -(void) setContentScaleFactor:(CGFloat)scaleFactor __ccContentScaleFactor = scaleFactor; winSizeInPixels_ = CGSizeMake( winSizeInPoints_.width * scaleFactor, winSizeInPoints_.height * scaleFactor ); - if( self.view ) + if( view_ ) [self updateContentScaleFactor]; // update projection @@ -276,9 +276,9 @@ -(void) setContentScaleFactor:(CGFloat)scaleFactor -(void) updateContentScaleFactor { - NSAssert( [self.view respondsToSelector:@selector(setContentScaleFactor:)], @"cocos2d v2.0+ runs on iOS 4 or later"); + NSAssert( [view_ respondsToSelector:@selector(setContentScaleFactor:)], @"cocos2d v2.0+ runs on iOS 4 or later"); - [self.view setContentScaleFactor: __ccContentScaleFactor]; + [view_ setContentScaleFactor: __ccContentScaleFactor]; isContentScaleSupported_ = YES; } @@ -293,7 +293,7 @@ -(BOOL) enableRetinaDisplay:(BOOL)enabled return YES; // setContentScaleFactor is not supported - if (! [self.view respondsToSelector:@selector(setContentScaleFactor:)]) + if (! [view_ respondsToSelector:@selector(setContentScaleFactor:)]) return NO; // SD device @@ -312,7 +312,7 @@ -(BOOL) enableRetinaDisplay:(BOOL)enabled // overriden, don't call super -(void) reshapeProjection:(CGSize)size { - winSizeInPoints_ = [self.view bounds].size; + winSizeInPoints_ = [view_ bounds].size; winSizeInPixels_ = CGSizeMake(winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height *__ccContentScaleFactor); [self setProjection:projection_]; @@ -350,16 +350,18 @@ -(void) end -(void) setView:(CCGLView *)view { - [super setView:view]; + if( view != view_) { + [super setView:view]; - // set size - winSizeInPixels_ = CGSizeMake(winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height *__ccContentScaleFactor); + // set size + winSizeInPixels_ = CGSizeMake(winSizeInPoints_.width * __ccContentScaleFactor, winSizeInPoints_.height *__ccContentScaleFactor); - if( __ccContentScaleFactor != 1 ) - [self updateContentScaleFactor]; + if( __ccContentScaleFactor != 1 ) + [self updateContentScaleFactor]; - [view setTouchDelegate: touchDispatcher_]; - [touchDispatcher_ setDispatchEvents: YES]; + [view setTouchDelegate: touchDispatcher_]; + [touchDispatcher_ setDispatchEvents: YES]; + } } // Override to allow orientations other than the default portrait orientation.