1 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個贊
這個問題與 Go 無關(guān)。用 C 編寫的示例也重現(xiàn)了該問題:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
簡短的回答是:glfwWaitEvents在 macOS 上使用。
長答案是 glfwPollEvents 處理事件(如果有)。否則,它會立即返回。這意味著(微不足道的)程序正在盡可能快地一遍又一遍地清除屏幕,消耗大量的 CPU 時間。替代方案 glfwWaitEvents 在沒有事件等待時阻塞,消耗很少或不消耗 CPU 時間??梢哉f,使用大量 CPU 的程序不應(yīng)導(dǎo)致窗口滯后,但這實(shí)際上取決于操作系統(tǒng)及其分配資源的方式。
至于使用這兩個函數(shù)中的哪一個,完全取決于我們還想讓我們的程序做什么。如果我們只想對用戶輸入做出反應(yīng)而沒有別的,glfwWaitEvents 可能是要走的路。如果我們想在事件循環(huán)中做其他事情,glfwPollEvents 可能更合適,特別是如果我們想獨(dú)立于用戶輸入重繪窗口內(nèi)容。
另外:glfwSwapInterval(1) 也可能會避免延遲,因?yàn)樗鼤?dǎo)致 glfwSwapBuffers 阻塞幾毫秒,再次讓 CPU 休息。
- 1 回答
- 0 關(guān)注
- 184 瀏覽
添加回答
舉報(bào)