From 6eabead4e3700bd71c8fef5e2b7f508080154118 Mon Sep 17 00:00:00 2001 From: dualface Date: Thu, 20 Sep 2012 23:32:26 +0800 Subject: [PATCH] [iOS] fix: enableRetina() and setDesignResolutionSize() can not be used simultaneously. FROM kunlun.com, THANKS --- cocos2dx/platform/ios/EAGLView.h | 4 +++ cocos2dx/platform/ios/EAGLView.mm | 44 ++++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cocos2dx/platform/ios/EAGLView.h b/cocos2dx/platform/ios/EAGLView.h index 64c83233ab..2605819266 100755 --- a/cocos2dx/platform/ios/EAGLView.h +++ b/cocos2dx/platform/ios/EAGLView.h @@ -87,6 +87,7 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. BOOL preserveBackbuffer_; CGSize size_; + CGFloat designContentScaleFactor_; BOOL discardFramebufferSupported_; //fsaa addition @@ -142,6 +143,9 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. @property(nonatomic,readwrite) BOOL multiSampling; +/** call from AppController.mm */ +-(void) setDesignContentScaleFactor:(CGFloat)scale; +-(CGFloat) getDesignContentScaleFactor; /** EAGLView uses double-buffer. This method swaps the buffers */ -(void) swapBuffers; diff --git a/cocos2dx/platform/ios/EAGLView.mm b/cocos2dx/platform/ios/EAGLView.mm index 2f9ba0a360..e1807f3dd4 100755 --- a/cocos2dx/platform/ios/EAGLView.mm +++ b/cocos2dx/platform/ios/EAGLView.mm @@ -134,6 +134,7 @@ static EAGLView *view = 0; { if((self = [super initWithFrame:frame])) { + designContentScaleFactor_ = 1.0; isUseUITextField = YES; pixelformat_ = format; depthFormat_ = depth; @@ -162,6 +163,7 @@ static EAGLView *view = 0; CAEAGLLayer* eaglLayer = (CAEAGLLayer*)[self layer]; + designContentScaleFactor_ = 1.0; pixelformat_ = kEAGLColorFormatRGB565; depthFormat_ = 0; // GL_DEPTH_COMPONENT24_OES; multiSampling_= NO; @@ -200,15 +202,27 @@ static EAGLView *view = 0; -(int) getWidth { CGSize bound = [self bounds].size; - return bound.width; + return bound.width * designContentScaleFactor_; } -(int) getHeight { CGSize bound = [self bounds].size; - return bound.height; + return bound.height * designContentScaleFactor_; } +-(void) setDesignContentScaleFactor:(CGFloat)scale +{ + if ([self respondsToSelector:@selector(setContentScaleFactor:)]) { + [self setContentScaleFactor:scale]; // if val == 2.0, enable retina display + designContentScaleFactor_ = scale; + } +} + +-(CGFloat) getDesignContentScaleFactor +{ + return designContentScaleFactor_; +} -(BOOL) setupSurfaceWithSharegroup:(EAGLSharegroup*)sharegroup { @@ -401,8 +415,8 @@ static EAGLView *view = 0; int i = 0; for (UITouch *touch in touches) { ids[i] = (int)touch; - xs[i] = [touch locationInView: [touch view]].x; - ys[i] = [touch locationInView: [touch view]].y; + xs[i] = [touch locationInView: [touch view]].x * designContentScaleFactor_; + ys[i] = [touch locationInView: [touch view]].y * designContentScaleFactor_; ++i; } cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i, ids, xs, ys); @@ -421,8 +435,8 @@ static EAGLView *view = 0; int i = 0; for (UITouch *touch in touches) { ids[i] = (int)touch; - xs[i] = [touch locationInView: [touch view]].x; - ys[i] = [touch locationInView: [touch view]].y; + xs[i] = [touch locationInView: [touch view]].x * designContentScaleFactor_; + ys[i] = [touch locationInView: [touch view]].y * designContentScaleFactor_; ++i; } cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesMove(i, ids, xs, ys); @@ -442,8 +456,8 @@ static EAGLView *view = 0; int i = 0; for (UITouch *touch in touches) { ids[i] = (int)touch; - xs[i] = [touch locationInView: [touch view]].x; - ys[i] = [touch locationInView: [touch view]].y; + xs[i] = [touch locationInView: [touch view]].x * designContentScaleFactor_; + ys[i] = [touch locationInView: [touch view]].y * designContentScaleFactor_; ++i; } cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesEnd(i, ids, xs, ys); @@ -463,8 +477,8 @@ static EAGLView *view = 0; int i = 0; for (UITouch *touch in touches) { ids[i] = (int)touch; - xs[i] = [touch locationInView: [touch view]].x; - ys[i] = [touch locationInView: [touch view]].y; + xs[i] = [touch locationInView: [touch view]].x * designContentScaleFactor_; + ys[i] = [touch locationInView: [touch view]].y * designContentScaleFactor_; ++i; } cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesCancel(i, ids, xs, ys); @@ -786,6 +800,15 @@ static EAGLView *view = 0; break; } + begin.origin.x *= designContentScaleFactor_; + begin.origin.y *= designContentScaleFactor_; + begin.size.width *= designContentScaleFactor_; + begin.size.height *= designContentScaleFactor_; + end.origin.x *= designContentScaleFactor_; + end.origin.y *= designContentScaleFactor_; + end.size.width *= designContentScaleFactor_; + end.size.height *= designContentScaleFactor_; + cocos2d::CCIMEKeyboardNotificationInfo notiInfo; notiInfo.begin = cocos2d::CCRect(begin.origin.x, begin.origin.y, @@ -859,6 +882,7 @@ static EAGLView *view = 0; // NSLog(@"[animation] dis = %f\n", dis); if (dis < 0.0f) dis = 0.0f; + dis /= designContentScaleFactor_; if (!cocos2d::CCEGLView::sharedOpenGLView()->isRetinaEnabled()) {