Skip to content

Instantly share code, notes, and snippets.

@kyokomi
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyokomi/28e00b2802dda0a1324c to your computer and use it in GitHub Desktop.
Save kyokomi/28e00b2802dda0a1324c to your computer and use it in GitHub Desktop.
cocos2d-xの画面サイズをいい感じに設定するユーティリティ
//
// 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
@kyokomi
Copy link
Author

kyokomi commented Oct 27, 2014

使い方

AppDelegate.cpp

#include "AppDelegate.h"
#include "GLViewUtil.h"

USING_NS_CC;

// 〜省略〜

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::create("My Game");
        director->setOpenGLView(glview);
    }

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    // ★640x960にフィットした画面にする(縦横の黒棒なし)
    auto fitSize = GLViewUtil::getFitDesignResolutionSize(640, 960);
    glview->setDesignResolutionSize(fitSize.width, fitSize.height, ResolutionPolicy::SHOW_ALL);

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();

    // run
    director->runWithScene(scene);

    return true;
}

// 〜省略〜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment