Test Case 3:
Here I am removing removing all the setContentScaleFactor block from AppDelegate.cpp and adding my own scaling class Utils.cpp. Then trying to scale my image from SplashScreen.cpp.
AppDelegate.cpp
#include "AppDelegate.h"
#include "SplashScreen.h"
#include "Utils.h"
// #define USE_AUDIO_ENGINE 1
// #define USE_SIMPLE_AUDIO_ENGINE 1
#if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE
#error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!"
#endif
#if USE_AUDIO_ENGINE
#include "audio/include/AudioEngine.h"
using namespace cocos2d::experimental;
#elif USE_SIMPLE_AUDIO_ENGINE
#include "audio/include/SimpleAudioEngine.h"
using namespace CocosDenshion;
#endif
USING_NS_CC;
static cocos2d::Size designResolutionSize = cocos2d::Size(1280, 720);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 360);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
#if USE_AUDIO_ENGINE
AudioEngine::end();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::end();
#endif
}
// if you want a different context, modify the value of glContextAttrs
// it will affect all platforms
void AppDelegate::initGLContextAttrs()
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// if you want to use the package manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("IndianPolityWar", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create("IndianPolityWar");
#endif
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(false);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / 60);
// Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
auto frameSize = glview->getFrameSize();
register_all_packages();
//Logging
int dpi = Device::getDPI();
CCLOG("dpi = %d", dpi);
CCLOG("Content Scale Factor: %f", director->getContentScaleFactor());
CCLOG("FrameSize HW: %f %f", frameSize.height, frameSize.width);
Utils::getWinSize(frameSize.height, frameSize.width);
// create a scene. it's an autorelease object
auto scene = SplashScreen::createScene();
// run
director->runWithScene(scene);
return true;
}
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
#if USE_AUDIO_ENGINE
AudioEngine::pauseAll();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
#endif
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
#if USE_AUDIO_ENGINE
AudioEngine::resumeAll();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
SimpleAudioEngine::getInstance()->resumeAllEffects();
#endif
}
SplashScreen.cpp
#include "SplashScreen.h"
#include "SimpleAudioEngine.h"
#include "Utils.h"
USING_NS_CC;
Scene* SplashScreen::createScene()
{
return SplashScreen::create();
}
bool SplashScreen::init()
{
if ( !Scene::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto sprite = Sprite::create("images/splashscreen.png");
Utils::scaleBG(sprite);
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
sprite->setOpacity(0);
this->addChild(sprite, 0);
auto fadeIn = FadeIn::create(0.5f);
sprite->runAction(fadeIn);
//Logging
CCLOG("Bounding HW= %f %f", sprite->getBoundingBox().size.height, sprite->getBoundingBox().size.width);
CCLOG("Image HW= %f %f", sprite->getContentSize().height, sprite->getContentSize().width);
return true;
}
Utils.cpp
#include "Utils.h"
using namespace cocos2d;
static float designScreenHeight = 720;
static float designScreenWidth = 1280;
float screenHeight = 720;
float screenWidth = 1280;
void Utils::scaleBG(cocos2d::Sprite *bg) {
float scaleX = 1;
float scaleY = 1;
if((screenWidth / screenHeight) > (designScreenWidth / designScreenHeight)) {
scaleX = screenWidth / designScreenWidth;
bg->setScale(scaleX);
} else {
scaleY = screenHeight / designScreenHeight;
bg->setScale(scaleY);
}
CCLOG("Utils: screenHeight, screenWidth: %f, %f", screenHeight, screenWidth);
CCLOG("Utils: designScreenHeight, designScreenWidth: %f, %f", designScreenHeight, designScreenWidth);
CCLOG("Utils: scaleX, scaleY: %f, %f", scaleX, scaleY);
}
void Utils::getWinSize(float height, float width) {
screenHeight = height;
screenWidth = width;
}
Output:
On, 480 * 800 Emulator:
dpi = 240
Content Scale Factor: 1.000000
FrameSize HW: 480.000000 800.000000
Utils: screenHeight, screenWidth: 480.000000, 800.000000
Utils: designScreenHeight, designScreenWidth: 720.000000, 1280.000000
Utils: scaleX, scaleY: 1.000000, 0.666667
Bounding HW= 480.000000 853.333313
Image HW= 720.000000 1280.000000
On, 1920 * 1080 Device:
dpi = 480
Content Scale Factor: 1.000000
FrameSize HW: 1080.000000 1920.000000
Utils: screenHeight, screenWidth: 1080.000000, 1920.000000
Utils: designScreenHeight, designScreenWidth: 720.000000, 1280.000000
Utils: scaleX, scaleY: 1.000000, 1.500000
Bounding HW= 1080.000000 1920.000000
Image HW= 720.000000 1280.000000
Now, here what I don’t understand is, though bounding box dimensions are showing correct values, why image height, width is not showing correct? Also, here Image is not showing correctly according to their height, width.