cocos2d-x项目过程记录(ios和android设备的适配)

(原创作品,欢迎转载,注明出处,谢谢:http://www.cnblogs.com/binxindoudou/admin/EditPosts.aspx?postid=3213645

1.原理分析的博客:cocos2d-x屏幕适配原理分析  cocos2d-x屏幕适配新解 

2.cocos2d-x 在4inch模拟器下面也只显示960 * 640的问题 (设置Default@2x.png跟 Default-568h@2x.png)

3.Cocos2d-x-API-详解-CCFileUtils 

4.自己实践效果记录(参考:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Mechanism_of_loading_resources)

1⃣一套资源匹配所有设备(有缺陷,图片资源有限的情况下采用)

在AppDelegate.cpp中

bool AppDelegate::applicationDidFinishLaunching()

{

  ……  

  CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();//获取到OpenGLView  

  //使用如下一句话,就可以实现一套资源进行适配

  //其中(480,320),在代码中只要在始终想着适配480,320这个分辨率的设备就可以了

  //其中kResolutionExactFit是屏幕分辨率适配模式,这个模式是占满全屏幕,进行适当的延伸或者收缩,会出现失去比例,扭曲的画面

  //如果不想出现扭曲的画面,那么可以试试kResolutionShowAll这个模式,会尽量占满屏幕,但比例不变,所以最多有一个方向的留黑边

  pEGLView->setDesignResolutionSize(480, 320, kResolutionExactFit);

  ……

}

2⃣上述方法有游戏效果上的损伤,如果你有多套资源,那么可以采用多套图片适配多个设备。

如果只针对苹果的设备,那么使用cocos2d-iphone的资源加载方法添加--后缀image.png image-hd.png image-ipad.png image-ipadhd.png image-568h.png就可以识别了。但如果要是跨平台的话,那么这个方法可能就不好使了,因为我知道Andriod设备是通过文件夹来识别文件的,并且Android设备本身也是多种分辨率的,不可能覆盖全面,只能挑些主流的适配,尽量在使游戏效果不受影响。当然为了跨平台开发,Xcode调试,安卓打包,那么Xcode也只能使用文件夹加载资源了。

cocos2d-x提供了两种机制来解决屏幕分辨率的问题。

第一种适用于资源路径是这样的(官方解释说是使用CocosBuilder或者Cocos2d-iphone会使用这种路径和处理方法,我没有研究CocosBuilder,所以就在此就直译下官方的说明)

--Resources

  |--a.png

  |--image

    |--b.png

    |--resources-iphonehd

      |--b.png

  |--resources-iphonehd

    |--a.png

使用CCFileUtils::setSearchResolutionsOrder()来添加要找寻的路径,可以这样设置:
std::vector<std::string> resDirOrders; if (resolution is ipad retina) { resDirOrders.push_back("resources-ipadhd"); resDirOrders.push_back("resources-ipad"); resDirOrders.push_back("resources-iphonehd"); } CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders);
在设置了找寻分辨率的路径之后,如果想以如下方式创建精灵
CCSprite *sprite = CCSprite::create("images/tex.png");
那么引擎将会以以下方式寻找这张图片
先在images/resources-ipadhd/中找;
如果没找到,在images/resources-iphonehd/中找;
如果没找到,在images/resources-iphonehd/中找;
如果没有,在images/中找。
注意:
这个策略不适合做多分辨率的匹配,不可能做到匹配所有设备的资源的,尤其是对于跨平台的,可以这么干
std::vector<std::string> resDirOrders;
if (resolution is reslution1)
{
    resDirOrders->push_back("path1");
    resDirOrders->push_back("path2");
    ...
}
else if (resolution is resolution2)
{
    resDirOrders->push_back("path-a");
    resDirOrders->push_back("path-b");
    ...
}
...

CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders);
简单理解起来,就是这种方法找的顺序,你提供一个路径:images/tex.png
然后引擎会认为你的images文件加下会有resource-iphone resource-iphonehd都会有此图片,就会依照你设置的顺序去找,因为你的设置是最容易找到这张图片的,但是如果没有,那么也
应该会有通用的就是images/下的tex.png。
适用范围:每个资源文件夹都是一样的构造,都会有通用的文件和针对特定设备的文件夹,那么依照你的设置,引擎只会索引相应设备下的。
你应该依据不同设备设置好查找路径。具体的设备的识别及注意事项如下:

  CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();  

  CCSize frameSize = pEGLView->getFrameSize();

  frameSize就是屏幕实际的分辨率

(注意:对于iphone5来说,只有添加上Default-568h@2x.png的启动图片才可以识别成正确的1136*640,否则是960*640)

(注意:CCDirector::sharedDirector()->getWinSize()与frameSize的区别,winSize只得是你的屏幕逻辑像素值)

 (摘自cocos2d-x-2.1.4/samples/Cpp/HelloCpp/Classes/AppDelegate.cpp)

// if the frame's height is larger than the height of medium resource size, select large resource.

    if (frameSize.height > mediumResource.size.height)

    {

  //设置使用资源路径,尽量的使用匹配资源

        searchPath.push_back(largeResource.directory); 

  //注:designResolutionSize就是你的winSize,就是你的逻辑分辨率,会以你的资源尺寸做相应比例的变化。

        pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));

    }

    // if the frame's height is larger than the height of small resource size, select medium resource.

    else if (frameSize.height > smallResource.size.height)

    {

        searchPath.push_back(mediumResource.directory);        

        pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width));

    }

    // if the frame's height is smaller than the height of medium resource size, select small resource.

    else

    {

        searchPath.push_back(smallResource.directory); 

        pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));

    }

  // set searching path

    CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);

在这里要注意的是它的寻找资源的顺序:

vector<string> searchPaths;
searchPaths.push_back("/mnt/sd/example");
searchPaths.push_back("/data/data/org.cocos2dx.example");
CCFileUtils::setSearchPaths(searchPaths);  

8CCSprite *pSprite = CCSprite::create("1.png");

找的时候先找
/mnt/sd/example
然后
/data/data/org.cocos2dx.example
然后/Resources
最后报错

(原创作品,欢迎转载,注明出处,谢谢:http://www.cnblogs.com/binxindoudou/admin/EditPosts.aspx?postid=3213645

posted @ 2013-07-25 09:31  bxddno1  阅读(1933)  评论(0编辑  收藏  举报