Cocos2d-x全屏适配问题

11 篇文章 0 订阅

看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!

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
參考了一下NeHe 的教程, 在 cocos2d-x 2.0 上可以做點小手腳在 Windows 上全屏顯示! 參考了一下NeHe 的教程, 在 cocos2d-x 2.0 上可以做點小手腳在 Windows 上全屏顯示! 主要修改兩個檔案: CCEGLView.h CCEGLView.cpp 它們在工程里的位置是 libcocos2d->platform->win32 先打開 CCEGLView.h 在 public: 底下加上一個新功能: void setFullScreen(bool flag); 另外在 private: 里加上一個新變數: bool m_bFullScreen; 接下來是 CCEGLView.cpp: 在 Constructor 加上 m_bFullScreen 的初始值: CCEGLView::CCEGLView() : m_bCaptured(false) , m_hWnd(NULL) , m_hDC(NULL) , m_hRC(NULL) , m_lpfnAccelerometerKeyHook(NULL) , m_bFullScreen(false) 再加上我們的 setFullScreen(): void CCEGLView::setFullScreen(bool flag) { m_bFullScreen = flag; } 然後在 Create() 里, 把CreateWindowEx() 那段改成下邊這樣: /////////////// FULLSCREEN HACK - BEGIN DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style DWORD dwStyle = WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX; // Window Style if (m_bFullScreen) { DEVMODE dmScreenSettings; // Device Mode memset(&dmScreenSettings;,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = w; // Selected Screen Width dmScreenSettings.dmPelsHeight = h; // Selected Screen Height dmScreenSettings.dmBitsPerPel = 32; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (ChangeDisplaySettings(&dmScreenSettings;,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { // If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode. if (MessageBox(NULL, L"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?",L"cocos2d-x",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) { setFullScreen(false); // back to windowed mode } else { // Pop Up A Message Box Letting User Know The Program Is Closing. MessageBox(NULL,L"Program Will Now Close.",L"ERROR",MB_OK|MB_ICONSTOP); return FALSE; // Return FALSE } } else // yeah! we are in fullscreen { dwExStyle = WS_EX_APPWINDOW; dwStyle=WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; //ShowCursor(FALSE); RECT rect; rect.left=(long)0; // Set Left Value To 0 rect.right=(long)w; // Set Right Value To Requested Width rect.top=(long)0; // Set Top Value To 0 rect.bottom=(long)h; // Set Bottom Value To Requested Height AdjustWindowRectEx(▭, dwStyle, FALSE, dwExStyle); // Adjust To True Requested Size } } /////////////// FULLSCREEN HACK - END // create window m_hWnd = CreateWindowEx( dwExStyle, // Extended Style For The Window kWindowClassName, // Class Name wszBuf, // Window Title dwStyle, // Defined Window Style 0, 0, // Window Position 0, // Window Width 0, // Window Height NULL, // No Parent Window NULL, // No Menu hInstance, // Instance NULL ); CC_BREAK_IF(! m_hWnd); 基本上這樣已搞定. 最後在我們自己項目的 main.cpp 里選擇全屏即可: AppDelegate app; CCEGLView& eglView = CCEGLView::sharedOpenGLView(); eglView.setFullScreen(true); eglView.setViewName("Hello Tests"); eglView.setFrameSize(1920, 1080); 要注意事項: - eglView.setFullScreen(true) 一定要在 eglView.setViewName("xxx") 前面. - eglView.setFrameSize(1920, 1080) 設定的大小, 一定要是顯卡可以支持的. - 不能再用CCDirector::sharedDirector()->enableRetinaDisplay(true)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值