Cocos2d-x在windows下實現全屏(cocos2d-x+win32+fullScreen)

本人使用的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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章