Cocos2d-x在windows下实现全屏(cocos2d-x+win32+fullScreen)

来源:互联网 发布:真三国无双7捏脸数据 编辑:程序博客网 时间:2024/06/11 17:13

本人使用的cocos2dx版本为cocos2d-x_v2.1.5b,之前查到一些解决全屏的办法,但是这些方法对新版本已经不再适用,经过辛苦查询,总算是皇天不负有心人,找到了新版本的解决办法。参考原文:http://www.cocos2d-x.org/forums/6/topics/24432

方法如下:(亲测可行)

1、在目录cocos2dx\platform\win32下找到CCEGLView.hCCEGLView.cpp,用记事本打开,在CCEGLView.h中添加如下代码

bool enterFullscreen(int fullscreenWidth=0, int fullscreenHeight=0);bool exitFullscreen(int windowX, int windowY, int windowedWidth, int windowedHeight, int windowedPaddingX, int windowedPaddingY);int getFullscreenWidth();int getFullscreenHeight();

2、在CCEGLView.cpp中添加如下代码

int CCEGLView::getFullscreenWidth(){    return GetDeviceCaps(m_hDC, HORZRES);}int CCEGLView::getFullscreenHeight(){    return GetDeviceCaps(m_hDC, VERTRES);}bool CCEGLView::enterFullscreen(int fullscreenWidth, int fullscreenHeight){    DEVMODE fullscreenSettings;    bool isChangeSuccessful;    if(fullscreenWidth == 0 || fullscreenHeight == 0)    {        fullscreenWidth  = GetDeviceCaps(m_hDC, HORZRES);        fullscreenHeight = GetDeviceCaps(m_hDC, VERTRES);    }    int colourBits       = GetDeviceCaps(m_hDC, BITSPIXEL);    int refreshRate      = GetDeviceCaps(m_hDC, VREFRESH);    EnumDisplaySettings(NULL, 0, &fullscreenSettings);    fullscreenSettings.dmPelsWidth        = fullscreenWidth;    fullscreenSettings.dmPelsHeight       = fullscreenHeight;    fullscreenSettings.dmBitsPerPel       = colourBits;    fullscreenSettings.dmDisplayFrequency = refreshRate;    fullscreenSettings.dmFields           = DM_PELSWIDTH |                                            DM_PELSHEIGHT |                                            DM_BITSPERPEL |                                            DM_DISPLAYFREQUENCY;    SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);    SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);    SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, fullscreenWidth, fullscreenHeight, SWP_SHOWWINDOW);    isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;    ShowWindow(m_hWnd, SW_MAXIMIZE);    resize(fullscreenWidth, fullscreenHeight);    return isChangeSuccessful;}bool CCEGLView::exitFullscreen(int windowX, int windowY, int windowedWidth, int windowedHeight, int windowedPaddingX, int windowedPaddingY){    bool isChangeSuccessful;    SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, WS_EX_LEFT);    SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE);    isChangeSuccessful = ChangeDisplaySettings(NULL, CDS_RESET) == DISP_CHANGE_SUCCESSFUL;    SetWindowPos(m_hWnd, HWND_NOTOPMOST, windowX, windowY, windowedWidth + windowedPaddingX, windowedHeight + windowedPaddingY, SWP_SHOWWINDOW);    ShowWindow(m_hWnd, SW_RESTORE);    return isChangeSuccessful;}

3、如何使用

main.cpp中添加

// Create the application instance    AppDelegate app;    CCEGLView* eglView = CCEGLView::sharedOpenGLView();    // Set the frame size to the full screen value    eglView->setFrameSize(eglView->getFullscreenWidth(), eglView->getFullscreenHeight());    // Enter full screen mode with the resolution size specified at exact fit    eglView->setDesignResolutionSize(1024, 768, kResolutionExactFit);    eglView->enterFullscreen(0, 0);    int ret = CCApplication::sharedApplication()->run();

这样就能够实现全屏了!

这个是我修改过的版本http://pan.baidu.com/share/link?shareid=3843693490&uk=4061830256