在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率
2012-05-15 11:11
363 查看
本文为转载:http://cankeyyin.blog.163.com/blog/static/12336178320124149391202/
原理:将iphone的hd图片给ipad用,即:
使用原iphone版HD资源(960*640),不用适配到1024*768,四周可留黑边
不改动原有逻辑代码
经测试,这个适配方法可以让一份代码同时运行与iphone、iphone retina、ipad、ipad retina四种分辨率。
1.让ipad能够读入HD图片
在cocos2d-x源代码CCFileUtils_ios.mm中,将所有
cocos2d::CC_CONTENT_SCALE_FACTOR() == 2
改成
cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 || (UI_USER_INTERFACE_IDIOM() ==UIUserInterfaceIdiomPad
2.让ipad显示640*960游戏区域,并居中
在iOS/AppController.mm中,将
EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
改成(此处为竖屏分辨率,横屏调换可x,y坐标):
EAGLView *__glView = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
__glView = [EAGLView viewWithFrame: CGRectMake(0, 0, 320, 480)
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
__glView.transform = CGAffineTransformMakeScale(1.0, 1.0);
__glView.frame = CGRectMake((768-640)/2,(1024-960)/2, 640,960);//屏幕窗口
}
else {
__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
}
3.开启Retina分辨率
classes/AppDelegate中:
pDirector->enableRetinaDisplay(true);
4.坐标调整 某些情况下坐标坐标会扩展到640,960 所以需要用百分比的形式表示坐标
int screenWidth=CCDirector::sharedDirector()->getWinSize().width;
int screenHeitht=CCDirector::sharedDirector()->getWinSize().height;
如果原来有用坐标跟常数运算,比如ccp(x+常数,y-常数)这一类,常数要变换一下
坐标常数=坐标常数* screenWidth/320;
5.以上步骤完成后,可以在iphone、iphone retina、ipad上正常运行,但不能在ipad retina上运行,要在pad retina运行,需要在源代码CCEGLView_ios.mm加入以下代码,
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return false;
bool CCEGLView::canSetContentScaleFactor()
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return false;
return [[EAGLView sharedEGLView] respondsToSelector:@selector(setContentScaleFactor:)]
&& [[UIScreen mainScreen] scale] != 1.0;
}
6.将程序设置为universal形式
Build Settings->Deployment->Targeted Device Family设置成iphone/ipad
原理:将iphone的hd图片给ipad用,即:
使用原iphone版HD资源(960*640),不用适配到1024*768,四周可留黑边
不改动原有逻辑代码
经测试,这个适配方法可以让一份代码同时运行与iphone、iphone retina、ipad、ipad retina四种分辨率。
1.让ipad能够读入HD图片
在cocos2d-x源代码CCFileUtils_ios.mm中,将所有
cocos2d::CC_CONTENT_SCALE_FACTOR() == 2
改成
cocos2d::CC_CONTENT_SCALE_FACTOR() == 2 || (UI_USER_INTERFACE_IDIOM() ==UIUserInterfaceIdiomPad
2.让ipad显示640*960游戏区域,并居中
在iOS/AppController.mm中,将
EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
改成(此处为竖屏分辨率,横屏调换可x,y坐标):
EAGLView *__glView = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
__glView = [EAGLView viewWithFrame: CGRectMake(0, 0, 320, 480)
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
__glView.transform = CGAffineTransformMakeScale(1.0, 1.0);
__glView.frame = CGRectMake((768-640)/2,(1024-960)/2, 640,960);//屏幕窗口
}
else {
__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
}
3.开启Retina分辨率
classes/AppDelegate中:
pDirector->enableRetinaDisplay(true);
4.坐标调整 某些情况下坐标坐标会扩展到640,960 所以需要用百分比的形式表示坐标
int screenWidth=CCDirector::sharedDirector()->getWinSize().width;
int screenHeitht=CCDirector::sharedDirector()->getWinSize().height;
如果原来有用坐标跟常数运算,比如ccp(x+常数,y-常数)这一类,常数要变换一下
坐标常数=坐标常数* screenWidth/320;
5.以上步骤完成后,可以在iphone、iphone retina、ipad上正常运行,但不能在ipad retina上运行,要在pad retina运行,需要在源代码CCEGLView_ios.mm加入以下代码,
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return false;
bool CCEGLView::canSetContentScaleFactor()
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return false;
return [[EAGLView sharedEGLView] respondsToSelector:@selector(setContentScaleFactor:)]
&& [[UIScreen mainScreen] scale] != 1.0;
}
6.将程序设置为universal形式
Build Settings->Deployment->Targeted Device Family设置成iphone/ipad
相关文章推荐
- [转]在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率
- 在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率
- cocos2d-x 完美适配iphone、ipad(包括retina模式)
- Cocos2d-x 完美适配iphone、ipad(包括retina模式)
- cocos2d-x 完美适配iphone、ipad(包括retina模式)
- cocos2d-x 完美适配iphone、ipad(包括retina模式)
- 最完整的让cocos2d游戏适配IPAD Retina的教程
- 零基础实战iphone、ipad游戏开发(cocos2d、cocos2d-x、Unity3d)四大游戏项目
- Netty+MUI从零打造一个仿微信的高性能聊天项目,兼容iPhone/iPad/安卓
- 如何用一个IPad屏幕适配各尺寸的IPhone
- 最完整的让cocos2d游戏适配IPAD Retina的教程
- COCOS2D游戏适配IPAD RETINA
- cocos2d-x项目快速适配IPAD的方法
- COCOS2D游戏适配IPAD RETINA
- cocos2d-x项目快速适配IPAD的方法
- 是做一个 Universal 应用好,还是分开做成 iPhone iPad 两个版本好?
- cocos2d-iphone中兼容iphone/ipad的问题
- iPhone各屏幕尺寸 分辨率 及适配
- iPhone屏幕尺寸、分辨率及适配
- iPhone屏幕尺寸、分辨率及适配