cocos2d-x 3.0 中的所有 android 设备在横向模式下如何支持多分辨率?
How multi resolution support for all android devices in cocos2d-x 3.0 in landscape mode?
我正在将现有的 cocos2d 游戏转换为 android,但我遇到分辨率问题 there.In 现有的 cocos 游戏,我正在使用那里的两个资源一个用于 IPAD 的文件夹和一个用于 IPHONE.I 的文件夹希望使用这些现有的资源文件夹,我可以执行我的 android 游戏。请给我任何适用于 android 分辨率的示例代码。
以前我使用下面的代码 IOS
自动导演 = Director::getInstance();
auto glview = director->getOpenGLView();
std::vector 搜索路径;
尺寸 frameSize = glview->getFrameSize();
if (frameSize.height > 640)
{
UserDefault::getInstance()->setIntegerForKey(DEVICE_TYPE,IPAD);
searchPath.push_back(largeResource.directory);
if(frameSize.height>768)
{
UserDefault::getInstance()->setBoolForKey("RETINA",true);
Director::getInstance()->setContentScaleFactor(0.5f);
UserDefault::getInstance()->setBoolForKey("RET", true);
}
}
else
{
UserDefault::getInstance()->setIntegerForKey(DEVICE_TYPE,IPHONE);
searchPath.push_back(smallResource.directory);
if(director->getWinSize().width == 960)
{
UserDefault::getInstance()->setBoolForKey(IPOD_5,false);
}
else
{
UserDefault::getInstance()->setBoolForKey(IPOD_5,true);
}
}
UserDefault::getInstance()->flush();
FileUtils::getInstance()->setSearchPaths(searchPath);
android 设备上有太多不同的屏幕尺寸。例如,下面是 phones 目前最常见的值(第一个值 - 分辨率,第二个和第三个 - 宽高比):
480x800 - 3:5 or 0.6
480x854 - 0.5621
768x1280 - 3:5 or 0.6
720x1280 - 9:16 or 0.5625
1080x1920 - 9:16 or 0.5625
1440x2560 - 9:16 or 0.5625
对于平板电脑:
2048x1536 (2048x1440) - 3:4 or 0.75 (0.7031)
1280x800 (1280x752) - 10:16 or 0.625 (0.5875)
1920x1200 (1920x1104) - 10:16 or 0.625 (0.575)
2560x1600 (2560x1504) - 10:16 or 0.625 (0.5875)
1024x600 (1024x552) - 0.5859 (0.539)
请记住,平板电脑上有状态栏。它需要一些分辨率,并且 cocos 应用程序看不到设备的总分辨率,而只是被截断了(我在括号中显示了它)。相应地改变纵横比。
所有这些导致分辨率值的选择非常多。因此,最好对宽高比设备的值进行分组。在这种情况下,我在 AppDelegate.cpp 中使用此方法:
static Size designResolutionSize = Size(2560, 1600); // you can choose your designResolutionSize at the top of AppDelegate
在 applicationDidFinishLaunching() 方法中:
bool AppDelegate::applicationDidFinishLaunching() {
…
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("My Game");
director->setOpenGLView(glview);
}
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT);
std::vector <std::string> searchPaths;
singleton->setScreenRelation(glview->getFrameSize().height / glview->getFrameSize().width);
if (singleton->getScreenRelation() > 1)
singleton->setScreenRelation(1 / singleton->getScreenRelation()); // isn’t depend on the screen orientation
if (singleton->getScreenRelation() <= 0.563) // phones with 9:16 aspect ratio
{
searchPaths.push_back("PhoneBacks");
}
if ((singleton->getScreenRelation() > 0.563)&(singleton->getScreenRelation() <= 0.625)) // phones and tablets with 10:16 and 3:5 aspect ratio
{
log("WideTablet, %d", singleton->getScreenRelation());
}
if (singleton->getScreenRelation() > 0.625) // tablets with 3:4 aspect ratio
{
searchPaths.push_back("TabletBacks");
}
cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths);
因此,您只需要具有三种不同纵横比的背景。
这是最重要的部分——ResolutionPolicy::FIXED_HEIGHT
(或FIXED_WIDTH
)选项。对于横向屏幕方向,您需要 FIXED_HEIGHT
政策。这意味着您在所有设备上固定 designResolution.height
= 1600 像素,并且宽度可变。例如,在 720x1280 phone 上,您有 background.width= 1600
和 background.height = 1600/0.5625 = 2844
像素。类似地使用 FIXED_WIDTH
作为纵向屏幕方向。
这种方法并不完美。对于长宽比值相似的背景,可以切割成几个像素,反之亦然,可以看到边缘处的黑色条纹是几个像素宽。此方法也不适合移动背景。但对于固定背景,它提供了一个很好的画面,只需要三种分辨率的背景。
我正在将现有的 cocos2d 游戏转换为 android,但我遇到分辨率问题 there.In 现有的 cocos 游戏,我正在使用那里的两个资源一个用于 IPAD 的文件夹和一个用于 IPHONE.I 的文件夹希望使用这些现有的资源文件夹,我可以执行我的 android 游戏。请给我任何适用于 android 分辨率的示例代码。
以前我使用下面的代码 IOS
自动导演 = Director::getInstance();
auto glview = director->getOpenGLView();
std::vector 搜索路径;
尺寸 frameSize = glview->getFrameSize();
if (frameSize.height > 640)
{
UserDefault::getInstance()->setIntegerForKey(DEVICE_TYPE,IPAD);
searchPath.push_back(largeResource.directory);
if(frameSize.height>768)
{
UserDefault::getInstance()->setBoolForKey("RETINA",true);
Director::getInstance()->setContentScaleFactor(0.5f);
UserDefault::getInstance()->setBoolForKey("RET", true);
}
}
else
{
UserDefault::getInstance()->setIntegerForKey(DEVICE_TYPE,IPHONE);
searchPath.push_back(smallResource.directory);
if(director->getWinSize().width == 960)
{
UserDefault::getInstance()->setBoolForKey(IPOD_5,false);
}
else
{
UserDefault::getInstance()->setBoolForKey(IPOD_5,true);
}
}
UserDefault::getInstance()->flush();
FileUtils::getInstance()->setSearchPaths(searchPath);
android 设备上有太多不同的屏幕尺寸。例如,下面是 phones 目前最常见的值(第一个值 - 分辨率,第二个和第三个 - 宽高比):
480x800 - 3:5 or 0.6
480x854 - 0.5621
768x1280 - 3:5 or 0.6
720x1280 - 9:16 or 0.5625
1080x1920 - 9:16 or 0.5625
1440x2560 - 9:16 or 0.5625
对于平板电脑:
2048x1536 (2048x1440) - 3:4 or 0.75 (0.7031)
1280x800 (1280x752) - 10:16 or 0.625 (0.5875)
1920x1200 (1920x1104) - 10:16 or 0.625 (0.575)
2560x1600 (2560x1504) - 10:16 or 0.625 (0.5875)
1024x600 (1024x552) - 0.5859 (0.539)
请记住,平板电脑上有状态栏。它需要一些分辨率,并且 cocos 应用程序看不到设备的总分辨率,而只是被截断了(我在括号中显示了它)。相应地改变纵横比。
所有这些导致分辨率值的选择非常多。因此,最好对宽高比设备的值进行分组。在这种情况下,我在 AppDelegate.cpp 中使用此方法:
static Size designResolutionSize = Size(2560, 1600); // you can choose your designResolutionSize at the top of AppDelegate
在 applicationDidFinishLaunching() 方法中:
bool AppDelegate::applicationDidFinishLaunching() {
…
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("My Game");
director->setOpenGLView(glview);
}
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT);
std::vector <std::string> searchPaths;
singleton->setScreenRelation(glview->getFrameSize().height / glview->getFrameSize().width);
if (singleton->getScreenRelation() > 1)
singleton->setScreenRelation(1 / singleton->getScreenRelation()); // isn’t depend on the screen orientation
if (singleton->getScreenRelation() <= 0.563) // phones with 9:16 aspect ratio
{
searchPaths.push_back("PhoneBacks");
}
if ((singleton->getScreenRelation() > 0.563)&(singleton->getScreenRelation() <= 0.625)) // phones and tablets with 10:16 and 3:5 aspect ratio
{
log("WideTablet, %d", singleton->getScreenRelation());
}
if (singleton->getScreenRelation() > 0.625) // tablets with 3:4 aspect ratio
{
searchPaths.push_back("TabletBacks");
}
cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths);
因此,您只需要具有三种不同纵横比的背景。
这是最重要的部分——ResolutionPolicy::FIXED_HEIGHT
(或FIXED_WIDTH
)选项。对于横向屏幕方向,您需要 FIXED_HEIGHT
政策。这意味着您在所有设备上固定 designResolution.height
= 1600 像素,并且宽度可变。例如,在 720x1280 phone 上,您有 background.width= 1600
和 background.height = 1600/0.5625 = 2844
像素。类似地使用 FIXED_WIDTH
作为纵向屏幕方向。
这种方法并不完美。对于长宽比值相似的背景,可以切割成几个像素,反之亦然,可以看到边缘处的黑色条纹是几个像素宽。此方法也不适合移动背景。但对于固定背景,它提供了一个很好的画面,只需要三种分辨率的背景。