user13742796
user13742796

Reputation: 41

How do I create a centered GLFW window?

I'm looking for the SDL equivalent of

gWindow = SDL_CreateWindow( "SDL window", 
            SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
            WINDOW_WIDTH, WINDOW_HEIGHT, windowFlags );

for GLFW. I can center the glfw window after its creation with the below code, however, for a split second, the window is shown non-centered. So it doesn't look smooth.

    // init glfw
    if (!initGLFW())
    {
        return -1;
    }

    // create main window
    int count;
    int windowWidth, windowHeight;
    int monitorX, monitorY;
    
    GLFWwindow* gWindow;
    GLFWmonitor** monitors = glfwGetMonitors(&count);
    const GLFWvidmode* videoMode = glfwGetVideoMode(monitors[0]);
    // width: 75% of the screen
    windowWidth = videoMode->width / 1.5;
    // Aspect ratio 16 to 9
    windowHeight = windowWidth / 16 * 9;
    
    glfwGetMonitorPos(monitors[0], &monitorX, &monitorY);

    gWindow = glfwCreateWindow(windowWidth, windowHeight, "Engine v" ENGINE_VERSION, NULL, NULL);
    if (!gWindow)
    {
        glfwTerminate();
        std::cout << "Failed to create main window" << std::endl;
        return -1;
    }

    glfwSetWindowPos(gWindow,
                     monitorX + (videoMode->width - windowWidth) / 2,
                     monitorY + (videoMode->height - windowHeight) / 2);

Upvotes: 1

Views: 2531

Answers (1)

msbit
msbit

Reputation: 4320

From the documentation:

By default, newly created windows use the placement recommended by the window system. To create the window at a specific position, make it initially invisible using the GLFW_VISIBLE window hint, set its position and then show it.

Example

Using your code above, you would add a call to glfwWindowHint before glfwCreateWindow like so:

glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

and once you have positioned the window where you would like it, make a call to glfwShowWindow like so:

glfwShowWindow(gWindow);

Depending on your use case, it may also be prudent to reset the window hints using glfwDefaultWindowHints after creating the window, to ensure that future window creation isn't modified.

Putting all that together, I have something like:

if (!glfwInit())
{   
    return -1; 
}   

int count;
int windowWidth, windowHeight;
int monitorX, monitorY;
    
GLFWwindow* gWindow;
GLFWmonitor** monitors = glfwGetMonitors(&count);
const GLFWvidmode* videoMode = glfwGetVideoMode(monitors[0]);
windowWidth = videoMode->width / 1.5;
windowHeight = windowWidth / 16 * 9;
    
glfwGetMonitorPos(monitors[0], &monitorX, &monitorY);

// (1).  Set the visibility window hint to false for subsequent window creation
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

gWindow = glfwCreateWindow(windowWidth, windowHeight, "Engine v", NULL, NULL);
if (!gWindow)
{   
    glfwTerminate();
    std::cout << "Failed to create main window" << std::endl;
    return -1; 
}   

// (2).  Reset the window hints to default
glfwDefaultWindowHints();

glfwSetWindowPos(gWindow,
                 monitorX + (videoMode->width - windowWidth) / 2,
                 monitorY + (videoMode->height - windowHeight) / 2); 

// (3).  Show the window
glfwShowWindow(gWindow);

Upvotes: 1

Related Questions