문제

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.

도움이 되었습니까?

해결책

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;
}

다른 팁

After discussion on IRC it seems that it is not possible to retrieve the currently active monitor (as in the monitor the window is currently drawn on) with GLFW. Therefore it is not possible to create a full screen window on the current monitor.

EDIT: Even though there is no GLFW functionality to directly achieve this, the answer of Shmo provides an elegant solution.

Here is Shmo's answer, ported over to LWJGL:

/** Determines the current monitor that the specified window is being displayed on.
 * If the monitor could not be determined, the primary monitor will be returned.
 * 
 * @param window The window to query
 * @return The current monitor on which the window is being displayed, or the primary monitor if one could not be determined
 * @author <a href="https://stackoverflow.com/a/31526753/2398263">Shmo</a><br>
 * Ported to LWJGL by Brian_Entei */
@NativeType("GLFWmonitor *")
public static final long glfwGetCurrentMonitor(long window) {
    int[] wx = {0}, wy = {0}, ww = {0}, wh = {0};
    int[] mx = {0}, my = {0}, mw = {0}, mh = {0};
    int overlap, bestoverlap;
    long bestmonitor;
    PointerBuffer monitors;
    GLFWVidMode mode;

    bestoverlap = 0;
    bestmonitor = glfwGetPrimaryMonitor();// (You could set this back to NULL, but I'd rather be guaranteed to get a valid monitor);

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

    while(monitors.hasRemaining()) {
        long monitor = monitors.get();
        mode = glfwGetVideoMode(monitor);
        glfwGetMonitorPos(monitor, mx, my);
        mw[0] = mode.width();
        mh[0] = mode.height();

        overlap =
                Math.max(0, Math.min(wx[0] + ww[0], mx[0] + mw[0]) - Math.max(wx[0], mx[0])) *
                Math.max(0, Math.min(wy[0] + wh[0], my[0] + mh[0]) - Math.max(wy[0], my[0]));

        if (bestoverlap < overlap) {
            bestoverlap = overlap;
            bestmonitor = monitor;
        }
    }

    return bestmonitor;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top