cocos2d-x 3.x on Windows: How do I change the size of my game window?
Asked Answered
G

2

6

When developing using cocos2d-x 3.x for a device, it automatically sets the GL view to fit the device. In VS2012 on windows, it creates a seemingly-arbitrarily sized window. How do I set the size of that window?

Gallon answered 4/6, 2014 at 4:3 Comment(0)
G
12

My solution was as follows.

In AppDelegate.cpp:

bool AppDelegate::applicationDidFinishLaunching() {
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) { 
        glview = GLView::create("My Game");
        glview->setFrameSize(800, 600);     // or whatever
        director->setOpenGLView(glview);
    }
    ...
}

In my particular use case, I'm setting the window sizes to various resolutions and aspect ratios to test my layouts. I'm sharing Q&A format because I couldn't find a straight answer to this anywhere.

Gallon answered 4/6, 2014 at 4:3 Comment(1)
Wrapping the call to setFrameSize() with #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) means this just affects desktop builds...Haun
B
0

+1 to Tom. Also, I would just like to share my answer and experience to everyone.

Remember, do not set the screen size "AFTER" the director set the GL view. This causes some problem with bounds detection on some nodes most notably cocos2d::ui::Button and other ways on how create buttons. I am using cocos2d-x v3.6

Check my answer here: cocos2d::Menu click detection is a bit off to bottom left whenever using a custom window size

Doing this way will cause some problem:

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

director->getOpenGLView()->setFrameSize(width , height);

Clicking the button, doesn't kick in the callback. Which is frustrating. Probably out of your frustration as well you click all through out the screen and notice there are some part of the screen (most likely bottom left part) which triggers the callback as if the bounding box of the node lies there. It doesn't even match the position and the size of the node in question. I can reproduce the problem whenever I change the screen size like the one I describe above. So never ever do it like that.

Tom's answer is the correct way of changing screen size.

I hope this will deter anyone doing it this way. I swear to God, this post could have save me heck of time solving this.

Cheers!

Ballistics answered 21/6, 2015 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.