我們的程序是風景模式還是肖像模式?使用cocos2d-x在Win32平台做開發時,如何進行設置?在繼續下面的教程之前,有必要弄清楚這些問題。
什么是風景模式?什么是肖像模式?
根據我的理解:寬度大於高度有着更寬闊的視野,適合欣賞風景;高度大於寬度能給站立的人物一個特寫,適合畫肖像。
在cocos2d-x代碼中,對設備方向有這樣的定義:
1 /** @typedef ccDeviceOrientation
2 Possible device orientations
3 */
4 typedef enum {
5 /// Device oriented vertically, home button on the bottom
6 kCCDeviceOrientationPortrait = 0, // UIDeviceOrientationPortrait,
7 /// Device oriented vertically, home button on the top
8 kCCDeviceOrientationPortraitUpsideDown = 1, // UIDeviceOrientationPortraitUpsideDown,
9 /// Device oriented horizontally, home button on the right
10 kCCDeviceOrientationLandscapeLeft = 2, // UIDeviceOrientationLandscapeLeft,
11 /// Device oriented horizontally, home button on the left
12 kCCDeviceOrientationLandscapeRight = 3, // UIDeviceOrientationLandscapeRight,
13
14 // Backward compatibility stuff
15 CCDeviceOrientationPortrait = kCCDeviceOrientationPortrait,
16 CCDeviceOrientationPortraitUpsideDown = kCCDeviceOrientationPortraitUpsideDown,
17 CCDeviceOrientationLandscapeLeft = kCCDeviceOrientationLandscapeLeft,
18 CCDeviceOrientationLandscapeRight = kCCDeviceOrientationLandscapeRight,
19 } ccDeviceOrientation;
根據注釋所說,肖像模式是設備為垂直方向的,home鍵在下方。想象一下手機,這個不難理解。
如何設置設備的方向呢?
在cocos2d-x上我們通過CCDirector的getDeviceOrientation獲取設備方向,setDeviceOrientation設置設備方向。但如果是在Win32平台上就要特別對待。請看:
1 ccDeviceOrientation CCDirector::getDeviceOrientation(void)
2 {
3 return m_eDeviceOrientation;
4 }
5
6 void CCDirector::setDeviceOrientation(ccDeviceOrientation kDeviceOrientation)
7 {
8 ccDeviceOrientation eNewOrientation;
9
10 eNewOrientation = (ccDeviceOrientation)CCApplication::sharedApplication().setOrientation(
11 (CCApplication::Orientation)kDeviceOrientation);
12
13 if (m_eDeviceOrientation != eNewOrientation)
14 {
15 m_eDeviceOrientation = eNewOrientation;
16 }
17 else
18 {
19 // this logic is only run on win32 now
20 // On win32,the return value of CCApplication::setDeviceOrientation is always kCCDeviceOrientationPortrait
21 // So,we should calculate the Projection and window size again.
22 m_obWinSizeInPoints = m_pobOpenGLView->getSize();
23 m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor,
m_obWinSizeInPoints.height * m_fContentScaleFactor);
24 setProjection(m_eProjection);
25 }
26 }
大家可以注意到在Win32平台上,setDeviceOrientation並不更新m_eDeviceOrientation,所以m_eDeviceOrientation總是保持他的默認值——CCDeviceOrientationPortrait。也就是說getDeviceOrientation的返回值總是CCDeviceOrientationPortrait。
那么在Win32平台上如何設置設備方向呢?來看下模板為我們生成的初始化代碼:
1 bool AppDelegate::initInstance()
2 {
3 bool bRet = false;
4 do
5 {
6 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
7
8 // Initialize OpenGLView instance, that release by CCDirector when application terminate.
9 // The HelloWorld is designed as HVGA.
10 CCEGLView * pMainWnd = new CCEGLView();
11 CC_BREAK_IF(! pMainWnd
12 || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320));
13
14 #endif // CC_PLATFORM_WIN32
15 bRet = true;
16 } while (0);
17 return bRet;
18 }
根據前面的知識我們可以推斷出,這是一個風景模式的HVGA窗口。如果想要將他變為肖像模式,只需要將寬度(480)與高度(320)參數對調。這是目前來說最簡單,最有效,甚至可以說是唯一可行的方案。
如果要在iOS或者android上設置設備方向,我只能建議你去看一下 http://www.cocos2d-x.org/projects/cocos2d-x/wiki/About_device_orientation