使用glfwCreateWindow使用GLFW3创建窗口:
GLFWwindow* glfwCreateWindow ( int width,
int height,
const char *title,
GLFWmonitor *monitor,
GLFWwindow *share
)
Run Code Online (Sandbox Code Playgroud)
如果monitor参数不是NULL,则在给定监视器上以全屏模式创建窗口.可以通过调用glfwGetPrimaryMonitor接收主监视器,或者选择glfwGetMonitors的结果之一.但是如何在当前监视器上创建一个全屏窗口,即窗口当前以窗口模式运行的监视器?似乎无法接收当前使用的监视器.有glfwGetWindowMonitor,但它只NULL在窗口模式下以全屏模式返回监视器.
您可以使用glfwGetWindowPos/glfwGetWindowSize找到当前的监视器.此函数返回包含更大窗口区域的监视器.
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;
}
Run Code Online (Sandbox Code Playgroud)