cocos2d-iphone:iPhone Retina纹理在iPad上导致低fps

时间:2013-02-01 09:10:47

标签: iphone objective-c ipad cocos2d-iphone retina-display

我想问你的帮助,我有点迷茫。我正在升级我的游戏,所以它可能是普遍的。我正在使用cocos2d-iphone版本1.0.1。我扩展了CCDirectorIOS类,因此它将iPhone的分辨率扩展到iPad(仅宽度,无拉伸)。它几乎完美地工作。 我得到稳定的60 fps:

  • 3GS iPhone / iPod,普通纹理,带视网膜的4G iPod / iPhone
  • 纹理。

这是一个令人讨厌的部分:

  • iPad 1/2/3 / mini,iPhone应用程序(非通用),Retina纹理=稳定60 fps,
  • iPad 1/2和mini,app设置为Universal,Retina纹理= 20 fps,
  • 使用普通纹理60 fps和带有Retina纹理的iPad 3 =稳定60 fps。

所以它运行在具有20 fps视网膜纹理的低端iPad上。我无法理解。

以下是修改过的CCDirectorIOS类的来源(仅修改):

//CCDirectorIOS.h only the modified

typedef enum {
/// sets a 2D projection (orthogonal projection).
kCCDirectorProjection2D,

/// sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500.
kCCDirectorProjection3D,

/// it calls "updateProjection" on the projection delegate.
kCCDirectorProjectionCustom,

/// Detault projection is 3D projection
kCCDirectorProjectionDefault = kCCDirectorProjection2D, // this was 3D

// backward compatibility stuff
CCDirectorProjection2D = kCCDirectorProjection2D,
CCDirectorProjection3D = kCCDirectorProjection3D,
CCDirectorProjectionCustom = kCCDirectorProjectionCustom,

} ccDirectorProjection;

@interface CCDirector : NSObject
{
//...

float m_VirtualWindowScale; //default 1
float m_VirtualWindowHeight; // default 0
float m_VirtualWindowWidth; // default 0
float mScaleFactor;
//...
}
//...

// stretch and virtual height
-(void) setVirtualWindowScale: (float) scale andH:(float) height;
-(float) getVirtualWindowScale;
-(float) getVirtualWindowHeight;
-(float) getVirtualWindowWidth;
//...
@end



//CCDirectorIOS.m only the modified

//...
-(void) setProjection:(ccDirectorProjection)projection
{
CGSize size = winSizeInPixels_;

switch (projection) {
case kCCDirectorProjection2D:
{
glViewport(0, 0, size.width, size.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ccglOrtho(0, size.width / m_VirtualWindowScale, 0, size.height / m_VirtualWindowScale, -1024 * CC_CONTENT_SCALE_FACTOR(), 1024 * CC_CONTENT_SCALE_FACTOR()); //-----------
glMatrixMode(GL_MODELVIEW);
//glDepthFunc(GL_ALWAYS);
glLoadIdentity();
break;
}

case kCCDirectorProjection3D:
{
float zeye = [self getZEye];

glViewport(0, 0, size.width, size.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//   gluPerspective(60, (GLfloat)size.width/size.height, zeye-size.height/2, zeye+size.height/2 );
gluPerspective(60, (GLfloat)size.width/size.height, 0.5f, 1500);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( size.width/2, size.height/2, zeye,
size.width/2, size.height/2, 0,
0.0f, 1.0f, 0.0f);
break;
}

case kCCDirectorProjectionCustom:
if( projectionDelegate_ )
[projectionDelegate_ updateProjection];
break;

default:
CCLOG(@"cocos2d: Director: unrecognized projecgtion");
break;
}

projection_ = projection;
}

//...

-(CGPoint)convertToGL:(CGPoint)uiPoint
{
CGSize s = winSizeInPoints_;
float newY = s.height - uiPoint.y;
float newX = s.width - uiPoint.x;

CGPoint ret = CGPointZero;
switch ( deviceOrientation_) {
case CCDeviceOrientationPortrait:
ret = ccp( uiPoint.x, newY );
break;
case CCDeviceOrientationPortraitUpsideDown:
ret = ccp(newX, uiPoint.y);
break;
case CCDeviceOrientationLandscapeLeft:
ret.x = uiPoint.y;
ret.y = uiPoint.x;
break;
case CCDeviceOrientationLandscapeRight:
ret.x = newY;
ret.y = newX;
break;
}
ret = ccpMult(ret, 1.0f / m_VirtualWindowScale); //-----------
return ret;
}

-(CGPoint)convertToUI:(CGPoint)glPoint
{
CGSize winSize = winSizeInPoints_;
int oppositeX = winSize.width - glPoint.x;
int oppositeY = winSize.height - glPoint.y;
CGPoint uiPoint = CGPointZero;
switch ( deviceOrientation_) {
case CCDeviceOrientationPortrait:
uiPoint = ccp(glPoint.x, oppositeY);
break;
case CCDeviceOrientationPortraitUpsideDown:
uiPoint = ccp(oppositeX, glPoint.y);
break;
case CCDeviceOrientationLandscapeLeft:
uiPoint = ccp(glPoint.y, glPoint.x);
break;
case CCDeviceOrientationLandscapeRight:
// Can't use oppositeX/Y because x/y are flipped
uiPoint = ccp(winSize.width-glPoint.y, winSize.height-glPoint.x);
break;
}
//uiPoint = ccpMult(uiPoint, 1/__ccContentScaleFactor);
uiPoint = ccpMult(uiPoint, 1/(__ccContentScaleFactor* m_VirtualWindowScale)); //-----------
return uiPoint;
}

//...

在Appdelegate的applicationDidFinishLaunch结束时,我添加了以下内容:

if ([[CCDirector sharedDirector] enableRetinaDisplay: YES]) {
NSLog(@"Retina Display enabled");
}

if ([[UIDevice currentDevice].model isEqualToString:@"iPad"]) {
[[CCDirector sharedDirector] setContentScaleFactor:2.0f];
}

NSLog(@"inpixels: %f inpoints: %f", [[CCDirector sharedDirector] winSizeInPixels].width, [[CCDirector sharedDirector] winSize].width);

int screenWidth = [[CCDirector sharedDirector] winSize].width; 
int screenHeight = [[CCDirector sharedDirector] winSize].height;

float scale = screenWidth / 320.0f;
float virtualHeight = screenHeight / scale;

[[CCDirector sharedDirector] setVirtualWindowScale:scale andH:virtualHeight]; 

我将所有内容放在virtualHeight和VirtualWidth上,所以一切都停留在屏幕上。

1 个答案:

答案 0 :(得分:1)

您不应使用scalefactor,即不要使用下面的行 [SPStage setContentScaleFactor:2.0f]

如果你设置比例因子,一切都以30 fps顺利运行。但缺点是该应用程序现在没有高清显卡运行。 iPad上的一切看起来都很模糊。

这是一个艰难的决定。最好将iPhone应用程序变成Universal,但你必须牺牲性能。使用定制纹理和内容比例为1的方式可以提供完美的结果,但这意味着需要更多修改代码的工作。