Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a full screen window on the current monitor with GLFW

Tags:

opengl

glfw

Creating a window with GLFW3 is done using glfwCreateWindow:

GLFWwindow* glfwCreateWindow ( int width,
                               int height,
                               const char *title,
                               GLFWmonitor *monitor,
                               GLFWwindow *share 
                             ) 

If the monitor parameter is not NULL, the window is created in full screen mode on the given monitor. One can receive the primary monitor by calling glfwGetPrimaryMonitor, or chose one of the results of glfwGetMonitors. But how can I create a full screen window on the current monitor, i.e. the monitor the window is currently running in windowed mode? There seems to be no way to receive the currently used monitor. There is glfwGetWindowMonitor, but it only returns the monitor in full screen mode, NULL in windowed mode.

like image 435
slash Avatar asked Jan 29 '14 02:01

slash


People also ask

How do I find my monitor size on Glfw?

The physical size of a monitor in millimetres, or an estimation of it, can be retrieved with glfwGetMonitorPhysicalSize.

What is a window hint?

These are hints for the window manager that indicate what type of function the window has. The window manager can use this when determining decoration and behaviour of the window. The hint must be set before mapping the window.

How do I get rid of Glfw?

When the user attempts to close the window, for example by clicking the close widget or using a key chord like Alt+F4, the close flag of the window is set.


1 Answers

You can find the current monitor with glfwGetWindowPos/glfwGetWindowSize. This function returns the monitor that contains the greater window area.

static int mini(int x, int y)
{
    return x < y ? x : y;
}

static int maxi(int x, int y)
{
    return x > y ? x : y;
}

GLFWmonitor* get_current_monitor(GLFWwindow *window)
{
    int nmonitors, i;
    int wx, wy, ww, wh;
    int mx, my, mw, mh;
    int overlap, bestoverlap;
    GLFWmonitor *bestmonitor;
    GLFWmonitor **monitors;
    const GLFWvidmode *mode;

    bestoverlap = 0;
    bestmonitor = NULL;

    glfwGetWindowPos(window, &wx, &wy);
    glfwGetWindowSize(window, &ww, &wh);
    monitors = glfwGetMonitors(&nmonitors);

    for (i = 0; i < nmonitors; i++) {
        mode = glfwGetVideoMode(monitors[i]);
        glfwGetMonitorPos(monitors[i], &mx, &my);
        mw = mode->width;
        mh = mode->height;

        overlap =
            maxi(0, mini(wx + ww, mx + mw) - maxi(wx, mx)) *
            maxi(0, mini(wy + wh, my + mh) - maxi(wy, my));

        if (bestoverlap < overlap) {
            bestoverlap = overlap;
            bestmonitor = monitors[i];
        }
    }

    return bestmonitor;
}
like image 106
Shmo Avatar answered Nov 15 '22 11:11

Shmo