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);
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;
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?
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.
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:
static link whole cocos2d-x into “your game”.exe and leave GLFWAPI empty;
link glfw in cocos2d-x.dll and “your game”.exe as standalone glfw.dll and
#define GLFWAPI __declspec(dllimport)
make c++ stub methods or dispatch custom events where is possible.
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.