2017-05-05 101 views
1

我在OpenGL和GLFW中編寫遊戲引擎。但是,不知怎的,我的窗戶不能關閉。我嘗試了很多東西,但沒有效果。我的代碼有什麼問題?窗口不關閉GLFW

我找不到錯誤的東西 - 對我來說一切似乎都很好。

代碼:

int running; 
GLFWwindow* window; 

Window::~Window() 
{ 
    glfwTerminate(); 
} 


Window::Window(int width, int height, const std::string& title) 
: m_width(width), 
m_height(height), 
m_title(title){ 

    glfwInit(); 

    if (!glfwInit()) 

    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); 
    window = glfwCreateWindow(height, width, __FILE__, NULL, NULL); 
    if (!window) { 
     glfwTerminate(); 

    } 

    running = true; 

} 

void Window::MainLoop() 
{ 

    do 
    { 
     glfwMakeContextCurrent(window); 

     glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 
     glClear(GL_COLOR_BUFFER_BIT); 
     glFlush(); 
     glfwPollEvents(); 

     Draw(); 

     glfwSwapBuffers(window); 

    } 

    while(running); 
} 

void Window::Draw() 
{ 

    glBegin(GL_TRIANGLES); 

    glVertex3f(0.0f, 1.0f, 0.0f); 
    glVertex3f(1.0f,-1.0f, 0.0f); 
    glVertex3f(-1.0f,-1.0f, 0.0f); 
    glEnd(); 
} 

謝謝!

回答

1

有幾件事情,但問題似乎是,你從未設置running = false

嘗試使你的病情而這樣看while(!glfwWindowShouldClose(window));

此外,如果你希望能夠通過按下Esc鍵這應該工作,關閉窗口:while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS);

還考慮讓int running一個bool

而諸如glfwMakeContextCurrent(window);glClearColor(0.2f, 0.3f, 0.3f, 1.0f);之類的東西如果不打算更改它們,則不需要放入循環中。

有關openGL的更多信息,並獲得一些基本的理解和工作示例,請考慮閱讀https://learnopengl.com/

+0

太好了,謝謝!它可能是不重要的,但是你是否也知道爲什麼當用標題替換__FILE__時,我得到''沒有匹配函數調用glfwCreateWindow'? – user6632515

+0

嘗試'title.c_str()'。函數期望C類型字符串= char數組(const char *)。 – Aldarrion