Resize a window of a Mac app

Hi there,
I’ve made a macOS app using cocos 3.17.2 on Xcode 11. I need a way to resize the window in a “dynamic way”, not only initialize it a little bigger. Now it’s too small.

My code:
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);

bool AppDelegate::applicationDidFinishLaunching() {

// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
    glview = GLViewImpl::createWithRect("myApp", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
    director->setOpenGLView(glview);
}

How can I do it?
Thanks for the attention.

What do you mean by “dynamic way”? Resize window by mouse or select different resolutions inside your app?

I meant resize the window by mouse, thank you.

In that case, i can help you. I speak English not very well, but I will try to formulate it as best as possible.
First of all - you will need to modify cocos2-x sources: CCGLViewImpl-desktop and CCDirector. I already provide you with modified sources:
https://drive.google.com/drive/folders/1YQRNVOqgDgEDMBuJDdgvnfyHPK05K4tc?usp=sharing
All modified/added functions marked by /*NTK ON*/ and /*NTK OFF*/ accordingly.
Then you create resizable window in applicationDidFinishLaunching():

auto director = Director::getInstance();
Rect r = Rect(100, 100, designResolutionSize.width, designResolutionSize.height);
auto desktopImpl = GLViewImpl::createWithRect("my resizable window", r, 1.0, true);
//optional settings for window
//Size windowMinSize = Size(640, 480);
//desktopImpl->setWindowSizeLimits(windowMinSize.width, windowMinSize.height, GLFW_DONT_CARE, GLFW_DONT_CARE);
//Size monitorScreenSize = ... (get monitor size rect, it is topic for another article, if you need it)
//desktopImpl->setWindowAspectRatio(monitorScreenSize.width, monitorScreenSize.height);
desktopImpl->setWindowPos(r.origin.x, r.origin.y);
director->setOpenGLView(desktopImpl);

//other director setup
director->setDisplayStats(false);
director->setAnimationInterval(1.0f / 60);

//and finally run
auto scene = GameScene::createScene();
director->runWithScene(scene);

return true;
2 Likes

Thank you! You’re very kind to answer me.
I have a little problem using your code: first of all, glfwRequestWindowAttention give me an error and I cannot compile it. Since seem that it’s not used anywhere, I removed it.
Then, running the app, it works but the game view remains little if you drag the window. I need it to be:

  • the game view larger as the window when i resize it. How can I adapt the game view to the window?

Before resize:

Schermata 2020-03-25 alle 11.21.49

After resize:

  • the window maintain the proportions of the game view when you resize it.

This should not be possible

I will wait for your answer. Thank you so much.

It depends on ResolutionPolicy:
f.e.:

director->setOpenGLView(desktopImpl);
float designWidth = 1024;
float designHeight = 768;
desktopImpl->setDesignResolutionSize(designWidth, designHeight, ResolutionPolicy::NO_BORDER);

There are many polices, you should choose one to fits you best.

2 Likes

It could be - i’m cooking glfw by myself, from glfw master branch, it could have/havent some methods of official release branch.

glview = GLViewImpl::createWithRect("my_game", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height), 1.0f, true );

glfwSetWindowAspectRatio(static_cast<GLViewImpl *>(glview)->getWindow(), designResolutionSize.width, designResolutionSize.height);

this works for me

And you will get black screen while grab window by mouse and resizing it. Thats why cocos sources change needed - to hook up window resize events and inject it into game loop cycle.

any pure glfw* functions will fail on windows, because of separation of cocos2d-x engine and user game logic into different files and dll calling convertions, thats why stub functions implemented in CCGLViewImpl-desktop.

1 Like

It worked! thank you so much! @hexerror

I don’t think so. https://www.dropbox.com/s/c2qwy2uqfybv8h9/resize%20window.mov?dl=0

I remember I also got it working on windows. But it was a while ago.

As i see - your game freeze animation, while you resizing window. My solution do not freeze update(dt) while resizing.

I’m not sure for v2.xx, but in v3.xx glfw is static linked against cocos2d-x.dll, and any function declared in glfw3.h declared with GLFWAPI specifier, f.e.

GLFWAPI void glfwSetWindowAspectRatio(GLFWwindow* window, int numer, int denom);

which (GLFWAPI, i mean) in case of static link will be empty. But, visual studio solution will produce, among other files, cocos2d-x.dll - the engine itself, with glfw static linked, and “your game”.exe, and to make any call of glfw* methods from “your game”.exe, those methods must be declared as dllexport(__declspec). So, for windows, as i’m see, could be only 3 solutions:

  1. static link whole cocos2d-x into “your game”.exe and leave GLFWAPI empty;
  2. link glfw in cocos2d-x.dll and “your game”.exe as standalone glfw.dll and
#define GLFWAPI __declspec(dllimport)
  1. make c++ stub methods or dispatch custom events where is possible.
1 Like

hey thanks for clearing that up, you are probably right about this. My game actually only works fullscreen now - i did implement it for windows like 3-4 years ago when windowed mode as an option, I’m guessing i had to use native windows functions for it. hwnd most likely.

p.s. I thought the topic was for a mac app xD

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.