我使用GLFW,因此使用OpenGL設置窗口。因爲我剛剛開始學習OpenGL及其周圍的所有東西,這可能聽起來像一個愚蠢的問題,但爲什麼GLFW的示例程序在窗口未被主動顯示時使用接近100%的CPU(被另一個窗口最小化或隱藏)?當GLFW/OpenGL窗口不可見時,CPU使用率過高
這裏是GLFW〔實施例,我和Xcode運行它在Mac OS:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
if (!glfwInit()) /* Initialize the library */
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 */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
順便說一下,您可能會考慮使用VSYNC--這會大大地節制您的繪圖線程。通常,當您以高幀速率繪製時,它會比螺旋鎖更智能,這樣可以有效減少CPU負載。除非您有超高要求的輸入延遲要求,否則渲染***的速度並不快得多。 –
感謝VYSNC的建議,我已經在做glfwSwapInterval(1),我認爲這就夠了? –
是的,這就夠了。我沒有在任何地方看到你的代碼,所以我認爲你使用的是默認的交換時間間隔。 –