如何创建居中的 GLFW window?
How do I create a centered GLFW window?
我正在寻找
的 SDL 等价物
gWindow = SDL_CreateWindow( "SDL window",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, windowFlags );
GLFW。我可以在使用以下代码创建 glfw window 后将其居中,但是,一瞬间,window 显示为非居中。所以看起来不太流畅
// 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);
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.
例子
使用上面的代码,您可以在 glfwCreateWindow
之前添加对 glfwWindowHint
的调用,如下所示:
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
一旦您将 window 定位到您想要的位置,就可以像这样调用 glfwShowWindow
:
glfwShowWindow(gWindow);
根据您的用例,在创建 window 后使用 glfwDefaultWindowHints
重置 window 提示可能也是明智的,以确保将来 window 创建未修改。
将所有这些放在一起,我有类似的东西:
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);
我正在寻找
的 SDL 等价物gWindow = SDL_CreateWindow( "SDL window",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, windowFlags );
GLFW。我可以在使用以下代码创建 glfw window 后将其居中,但是,一瞬间,window 显示为非居中。所以看起来不太流畅
// 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);
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.
例子
使用上面的代码,您可以在 glfwCreateWindow
之前添加对 glfwWindowHint
的调用,如下所示:
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
一旦您将 window 定位到您想要的位置,就可以像这样调用 glfwShowWindow
:
glfwShowWindow(gWindow);
根据您的用例,在创建 window 后使用 glfwDefaultWindowHints
重置 window 提示可能也是明智的,以确保将来 window 创建未修改。
将所有这些放在一起,我有类似的东西:
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);