Last active
August 29, 2015 14:08
-
-
Save kyokomi/28e00b2802dda0a1324c to your computer and use it in GitHub Desktop.
cocos2d-xの画面サイズをいい感じに設定するユーティリティ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GLViewUtil.h | |
// | |
// Created by kyokomi on 2014/10/28. | |
// | |
// | |
#ifndef KyokomiDemo_GLViewUtil_h | |
#define KyokomiDemo_GLViewUtil_h | |
#include "base/CCDirector.h" | |
#include "platform/CCGLView.h" | |
namespace GLViewUtil { | |
enum class FitType { | |
WIDTH, | |
HEIGHT, | |
}; | |
cocos2d::Size calcFitDesignResolutionSize(float width, float height, FitType fitType, float scale = 1.0f) | |
{ | |
auto visibleSize = cocos2d::Director::getInstance()->getVisibleSize(); | |
float baseScaleWidth = width * scale; | |
float baseScaleHeight = height * scale; | |
int widthDiff = abs((int)visibleSize.width - (int)width); | |
int heightDiff = abs((int)visibleSize.height - (int)height); | |
if (widthDiff == 0 && heightDiff == 0) { | |
return cocos2d::Size(baseScaleWidth, baseScaleHeight); | |
} | |
float divX = visibleSize.width / baseScaleWidth; | |
float divY = visibleSize.height / baseScaleHeight; | |
float fixWidth = widthDiff / divY; | |
float fixHeight = heightDiff / divX; | |
if (fitType == FitType::HEIGHT) { | |
// 縦優先 | |
float addWidth = (heightDiff / divY * (width / height)); | |
return cocos2d::Size(baseScaleWidth + fixWidth - addWidth, baseScaleHeight); | |
} else { | |
// 横優先 | |
float addheight = (widthDiff / divX * (height / width)); | |
return cocos2d::Size(baseScaleWidth, baseScaleHeight + fixHeight - addheight); | |
} | |
} | |
cocos2d::Size getFitDesignResolutionSize(float width, float height, FitType fitType, float scale = 1.0f) | |
{ | |
cocos2d::Size fitSize = GLViewUtil::calcFitDesignResolutionSize(width, height, FitType::WIDTH, scale); | |
float baseScaleWidth = width * scale; | |
float baseScaleHeight = height * scale; | |
if (fitType == FitType::HEIGHT && fitSize.width < baseScaleWidth) { | |
return GLViewUtil::calcFitDesignResolutionSize(width, height, FitType::WIDTH, scale); | |
} else if (fitType == FitType::WIDTH && fitSize.height < baseScaleHeight) { | |
return GLViewUtil::calcFitDesignResolutionSize(width, height, FitType::HEIGHT, scale); | |
} | |
return fitSize; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
使い方
AppDelegate.cpp