Resize Window Using GLFW3

When porting our game from mobile to desktop we wanted to add a few desktop specific interface enhancements. Things like using the keyboard, mouse, or managing the Window. By default games are setup to run full screen instead of within a window on desktop platforms, but since our game is pixel art full screen is a bit large on modern 24″+ monitors. So we’re sticking with running inside a window for now.

Ideally we’d allow players to resize the window. In order to support resizing a running scene we would need support to dynamically update every single UI element. Each scene or menu does size, scale, and position UI widgets on the current device (or now window) size, so we will allow changing resolution from a menu and then we will just reload that menu with replaceScene() after the changing the window frame size. We could build in support for this and attempt to unify the changes into a base class or small set of resize methods, but it would be better done at the first design and creation phase of a game’s development.

/// glview - the cocos2d-x platform OpenGL view reference 
/// frameSize - the desired window size
void OptionsResolutionMenu::static_changeResolution(GLViewImpl* glview, Size frameSize)
    auto glfwindow = glview->getWindow();
    auto fsize = glview->getFrameSize();
    auto screenSize = glview->getMonitorSize();
    int sw = screenSize.width > 0 ? (int)screenSize.width : 800;
    int sh = screenSize.height > 0 ? (int)screenSize.height : 480;
    // win position
    int winx = 0, winy = 0;
    glfwGetWindowPos(glfwindow, &winx, &winy);

    Rect viewport = glview->getViewPortRect();
    Vec2 viewcenter = Vec2(viewport.getMidX(), viewport.getMidY());

    // update the cocos2d view frame
    glview->setFrameSize(frameSize.width, frameSize.height);

    int x = int(sw - frameSize.width)/2;
    int y = int(sh - frameSize.height)/2;
    glfwSetWindowPos(glfwindow, x, y);

// call method with desired size (we get this from user options menu)
float resWidth = 800;
float resHeight = 600; 
OptionsResolutionMenu::static_changeResolution(glviewImpl, Size(resWidth, resHeight));
GameManager::getInstance()->runSceneWithID(kSceneOptionsResolution, 0);