參考了一下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)