GLFW-无需使用循环而保持窗口

GLFW - hold window without using loop

本文关键字:窗口 循环 GLFW-      更新时间:2023-10-16

为了使结果打开(窗口屏幕),我必须执行以下操作:

while (glfwGetWindowParam(GLFW_OPENED))
{
    // do some stuff
    glDrawPixels(...);
    glfwSwapBuffers();
}

但是,这个循环穿过时循环的主体,直到我停止它。我不想实时渲染。

glfw具有等效于SDL_WAITEVENT:

SDL_Surface* screen;
// do something with screen
SDL_Flip(screen);
SDL_Event event;
while (SDL_WaitEvent(&event))
{
   // handle input
}

如果我尝试执行这样的操作,则该程序会呈现结果,但是过了一会儿就停止响应:

//do stuff
glDrawPixels(...);
glfwSwapBuffers();
while (glfwGetWindowParam(GLFW_OPENED))
{
   if (glfwGetKey(GLFW_KEY_ESC) == GLFW_PRESS)
   {
       glfwTerminate();
   }
}

您的典型GLFW主应用循环可能看起来像这样:

while (!glfwWindowShouldClose(window)) {
  glfwPollEvents();
  update();
  draw();
  glfwSwapBuffers(window);
}
glfwDestroyWindow(window);

输入可以通过回调(例如键盘输入回调)来处理。我使用包装程序类来处理我的glfw东西,所以创建一个窗口后,我会做这样的事情:

glfwSetWindowUserPointer(window, this);
glfwSetKeyCallback(window, glfwKeyCallback);

和我的键盘回调看起来像这样:

void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  GlfwApp * instance = (GlfwApp *) glfwGetWindowUserPointer(window);
  instance->onKey(key, scancode, action, mods);
}

和onkey方法的基类实现如下:

void GlfwApp::onKey(int key, int scancode, int action, int mods) {
  if (GLFW_PRESS != action) {
    return;
  }
  switch (key) {
  case GLFW_KEY_ESCAPE:
    glfwSetWindowShouldClose(window, 1);
    return;
  }
}

您可以在这里和这里看到整个班级。

glfw3具有glfwWaitEvents,可能会做您想要的。

http://www.glfw.org/docs/latest/group__window.html#ga554e3781f0a997656b26b26b2c56c56c56c56c835e

而不是使用glfwgetWindowParam()我认为您可能想考虑使用glfwwindowshouldclose()。它采用指向GLFW窗口的指针。这是指向具有GLFWWINDOWSHOUNDCLOSE()http://www.glfw.org/docs/latest/group/group__window.html

的GLFWW文档页面的链接。