
本文为转载: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
总结以上是内存溢出为你收集整理的在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率全部内容,希望文章能够帮你解决在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)