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

这样就能够实现全屏了!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
參考了一下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)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值