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中添加如下代码

[cpp]  view plain copy
  1. bool enterFullscreen(int fullscreenWidth=0, int fullscreenHeight=0);  
  2. bool exitFullscreen(int windowX, int windowY, int windowedWidth, int windowedHeight, int windowedPaddingX, int windowedPaddingY);  
  3. int getFullscreenWidth();  
  4. int getFullscreenHeight();  

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

[cpp]  view plain copy
  1. int CCEGLView::getFullscreenWidth()  
  2. {  
  3.     return GetDeviceCaps(m_hDC, HORZRES);  
  4. }  
  5.   
  6. int CCEGLView::getFullscreenHeight()  
  7. {  
  8.     return GetDeviceCaps(m_hDC, VERTRES);  
  9. }  
  10.   
  11. bool CCEGLView::enterFullscreen(int fullscreenWidth, int fullscreenHeight)  
  12. {  
  13.     DEVMODE fullscreenSettings;  
  14.     bool isChangeSuccessful;  
  15.   
  16.     if(fullscreenWidth == 0 || fullscreenHeight == 0)  
  17.     {  
  18.         fullscreenWidth  = GetDeviceCaps(m_hDC, HORZRES);  
  19.         fullscreenHeight = GetDeviceCaps(m_hDC, VERTRES);  
  20.     }  
  21.   
  22.     int colourBits       = GetDeviceCaps(m_hDC, BITSPIXEL);  
  23.     int refreshRate      = GetDeviceCaps(m_hDC, VREFRESH);  
  24.   
  25.     EnumDisplaySettings(NULL, 0, &fullscreenSettings);  
  26.     fullscreenSettings.dmPelsWidth        = fullscreenWidth;  
  27.     fullscreenSettings.dmPelsHeight       = fullscreenHeight;  
  28.     fullscreenSettings.dmBitsPerPel       = colourBits;  
  29.     fullscreenSettings.dmDisplayFrequency = refreshRate;  
  30.     fullscreenSettings.dmFields           = DM_PELSWIDTH |  
  31.                                             DM_PELSHEIGHT |  
  32.                                             DM_BITSPERPEL |  
  33.                                             DM_DISPLAYFREQUENCY;  
  34.   
  35.     SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);  
  36.     SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);  
  37.     SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, fullscreenWidth, fullscreenHeight, SWP_SHOWWINDOW);  
  38.     isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;  
  39.     ShowWindow(m_hWnd, SW_MAXIMIZE);  
  40.   
  41.     resize(fullscreenWidth, fullscreenHeight);  
  42.   
  43.     return isChangeSuccessful;  
  44. }  
  45.   
  46. bool CCEGLView::exitFullscreen(int windowX, int windowY, int windowedWidth, int windowedHeight, int windowedPaddingX, int windowedPaddingY)  
  47. {  
  48.     bool isChangeSuccessful;  
  49.   
  50.     SetWindowLongPtr(m_hWnd, GWL_EXSTYLE, WS_EX_LEFT);  
  51.     SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE);  
  52.     isChangeSuccessful = ChangeDisplaySettings(NULL, CDS_RESET) == DISP_CHANGE_SUCCESSFUL;  
  53.     SetWindowPos(m_hWnd, HWND_NOTOPMOST, windowX, windowY, windowedWidth + windowedPaddingX, windowedHeight + windowedPaddingY, SWP_SHOWWINDOW);  
  54.     ShowWindow(m_hWnd, SW_RESTORE);  
  55.   
  56.     return isChangeSuccessful;  
  57. }  

3、如何使用

main.cpp中添加

[cpp]  view plain copy
  1. // Create the application instance  
  2.     AppDelegate app;  
  3.     CCEGLView* eglView = CCEGLView::sharedOpenGLView();  
  4.   
  5.     // Set the frame size to the full screen value  
  6.     //eglView->setFrameSize(eglView->getFullscreenWidth(), eglView->getFullscreenHeight());  
  7.   
  8.     eglView->enterFullscreen(0, 0);  
  9.   
  10.     int ret = CCApplication::sharedApplication()->run();  

这样就能够实现全屏了!

你可能感兴趣的:(Cocos2d-x在windows下实现全屏(cocos2d-x+win32+fullScreen))