Cocos2d-x全屏适配问题

看sample之后就会亲自动手体验cocos2dx的美妙,但是新人难免遇到窘境,开局虽然苦涩,但是美妙就在解决问题之后。

自己搞一个小demo,用到的图片就一套,而我的测试手机分辨率不同,直接跑起来,只占了中间一块。而我马上着手全屏问题的解决。

首先,看了一下HIMI的全屏适配方案,真是云里雾里。在main.cpp中直接添加下面注释部分:

void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv*  env, jobject thiz, jint w, jint h)
{
    if (!CCDirector::sharedDirector()->getOpenGLView())
    {
        CCEGLView *view = CCEGLView::sharedOpenGLView();
        view->setFrameSize(w, h);

//        view->setDesignResolutionSize(320,480,kResolutionExactFit);
        //强制全屏,当屏幕宽高规格和原始素材比例不一致的时候,可能会把屏幕挤压。
//        view->setDesignResolutionSize(320,480,kResolutionNoBorder);
        //当出现屏幕规格和原始素材比例不一致时,将会切去屏幕边缘,以免出现挤压画面的现象。
//        view->setDesignResolutionSize(320,480,kResolutionShowAll);
        //当出现屏幕规格和原始素材比例不一致时,会用黑色填充多余部分,这种方式不会造成屏幕挤压,但是会出现黑边。
        AppDelegate *pAppDelegate = new AppDelegate();
        CCApplication::sharedApplication()->run();
    }
    else
    {
        ccGLInvalidateStateCache();
        CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
        ccDrawInit();
        CCTextureCache::reloadAllTextures();
        CCNotificationCenter::sharedNotificationCenter()->postNotification(EVENT_COME_TO_FOREGROUND, NULL);
        CCDirector::sharedDirector()->setGLDefaultValues(); 
    }
}
改完直接跑在我的I9001上,运行的非常好(我真是很高兴),但是跑在Nexus4上,程序直接Crash。我是百思不得其解。只好去cocos2dx论坛发帖求助,无人理我。

看来还是自己寻找解决办法吧。

我把sample下的HelloCpp跑起来,发现不同的分辨率,它表现的也有问题。然后我尝试把assert中的ipad和ipadhd文件夹删掉,再次运行,应用也是Crash。

真是郁闷至极!我在想,如果今天还是没有进展,那么就放弃吧。反正sample中使用大图片,程序都能跑的。最后,运气还是不错,找到了一条线索。

应该在AppDelegate::applicationDidFinishLaunching()方法下面添加上面的语句。最后,终于找到合适的位置:

bool AppDelegate::applicationDidFinishLaunching()
{

    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());

    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(320,480,kResolutionExactFit);
    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = HMenu::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}
再次运行,程序全屏适配的跑起来了。又扫清一条道路,继续前进!
看HelloCpp的AppDelegate才知道,原来出现适配问题(在我的手机上看,背景图片虽然是全屏,但显示不全),只因下面这条if else:

    // Set the design resolution
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionShowAll);
#else
    pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);
#endif
WINRT和WP8用的ShowAll方案,而其他平台通通是NoBorder方案,怪不得显示不全。


而后面的条件判断语句更是解释了为什么我在删掉ipad和ipadhd文件夹后程序crash的问题。

    // In this demo, we select resource according to the frame's height.
    // If the resource size is different from design resolution size, you need to set contentScaleFactor.
    // We use the ratio of resource's height to the height of design resolution,
    // this can make sure that the resource's height could fit for the height of design resolution.

    // 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);

        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));
    }
程序通过对比分辨率(高)来选择图片,而合适的图片被我删掉,当然就报下面的错误了。

12-23 19:22:36.249: D/cocos2d-x debug info(6615): Get data from file(CloseNormal.png) failed!
12-23 19:22:36.249: D/cocos2d-x debug info(6615): Get data from file(CloseSelected.png) failed!
12-23 19:22:36.269: D/cocos2d-x debug info(6615): Get data from file(HelloWorld.png) failed!

新人在新的环境,难免出现判断失误和不知所措,过程,我还需要一个过程。

你可能感兴趣的:(Cocos2d-x全屏适配问题)