In cocos2dx v4.0, the screen becomes half size when you change the device from portrait to landscape

Hello.

I was using cocos2dx v3.12 in my iOS app, but I upgraded to cocos2dx v4 due to performance and the removal of UIWebView.

However, when I upgraded to the v4 system, I had a problem.
Specifically, if you change the orientation of the device’s screen from portrait to landscape, the cocos2dx screen is halved.

If you investigate, it seems that the CCEAGLView on the iOS side is not halved, but some component on the cocos2dx side is halved.

Codes for screen rotation are as follows

// create CCEAGLView

CCEAGLView* PlatformFunctions::getSharedGlView(CGRect bounding) {
    static dispatch_once_t onceToken;
    static CCEAGLView * o;
    dispatch_once(&onceToken, ^{
        auto app = cocos2d::Application::getInstance();
        app->initGLContextAttrs();
        cocos2d::GLViewImpl::convertAttrs();

        o = [CCEAGLView viewWithFrame: bounding
                          pixelFormat: kEAGLColorFormatRGBA8
                          depthFormat: GL_DEPTH24_STENCIL8_OES
                   preserveBackbuffer: NO
                           sharegroup: nil
                        multiSampling: NO
                      numberOfSamples:0 ];

        o.opaque = NO;
        o.multipleTouchEnabled = YES;
        cocos2d::Director::getInstance()->setOpenGLView(cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)o));
        app->run();
    });
    return o;
}


// AppDelegate.cpp

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

    auto visibleSize = director->getWinSize();
    auto designResolutionSize = director->getOpenGLView()->getDesignResolutionSize();
    // Always display vertically at first.
    glview->setDesignResolutionSize(DESIGN_RESOLUTION_SIZE_LONG, DESIGN_RESOLUTION_SIZE_LONG, ResolutionPolicy::FIXED_HEIGHT);

    // Receive events from iOS when rotating the screen
    Director::getInstance()->getEventDispatcher()->addCustomEventListener(kOnScreenOrientationChanged, [=](cocos2d::EventCustom *event) {
        ScreenOrientation orientation = *(ScreenOrientation*)event->getUserData();
        ResolutionPolicy policy = orientation == Portrait ? ResolutionPolicy::FIXED_HEIGHT : ResolutionPolicy::FIXED_WIDTH;
        director->getOpenGLView()->setDesignResolutionSize(DESIGN_RESOLUTION_SIZE_LONG, DESIGN_RESOLUTION_SIZE_LONG, policy);
    });

#ifdef DEBUG
    // turn on display FPS
    director->setDisplayStats(true);
#endif

    // fps setting
    PlatformFunctions::adjustFPS();

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

    PlatformFunctions::onApplicationDidFinishLaunching();

    // run
    director->runWithScene(scene);

    return true;
}


The development environment is as follows

  • iOS version: iOS 13.6
  • Device: iPhone XS / iPhone X / Simulator
  • cocos2dx version: v4.0

Capture

Here are the vertical and horizontal screen captures for my app on v3 and v4 systems.
The following captures have been simplified to make the glitch easier to understand.

Size winSize = Director::getInstance()->getWinSize();
ScreenOrientation orientation = winSize.width < winSize.height ? Portrait : Landscape;
auto _bg = LayerColor::create(Color4B::RED, winSize.width, winSize.height);
// this is Scene
this->addChild(_bg);

cocos2dx v3.12 capture

portrate

landscape

cocos2dx v4.0 capture

portrate

landscape

You should call setFrameSize before call setDesignResolutionSize on event ScreenOrientationChanged. It worked for me

@thaihoangduylinh
thank you reply!

I fixed this problem.
The reason for this is that every rotation generates a GLViewImpl and incorrect setDesignResolutionSize API width, height.