我们的程序是风景模式还是肖像模式?使用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