La fenêtre ne ferme pas GLFW

J’écris un moteur de jeu sous OpenGL et GLFW. Cependant, ma fenêtre ne peut pas être fermée. J’ai essayé beaucoup de choses mais ça n’a pas d’effet. Qu’est ce qui ne va pas avec mon code?

Je ne peux pas trouver la mauvaise chose dedans – tout me semble aller bien.

Code:

int running; GLFWwindow* window; Window::~Window() { glfwTerminate(); } Window::Window(int width, int height, const std::ssortingng& 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(); } 

Merci!

Il y a plusieurs choses, mais le problème semble être que vous ne définissez jamais running = false .

Essayez de while(!glfwWindowShouldClose(window)); votre condition while l’apparence suivante while(!glfwWindowShouldClose(window));

Si vous souhaitez également pouvoir fermer la fenêtre en appuyant sur while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS); cela devrait fonctionner: while(!glfwWindowShouldClose(window) && glfwGetKey(window_, GLFW_KEY_ESCAPE) != GLFW_PRESS);

Pensez également à faire de votre int running un bool

Et des choses telles que glfwMakeContextCurrent(window); et glClearColor(0.2f, 0.3f, 0.3f, 1.0f); n’avez pas besoin d’être à l’intérieur de votre boucle si vous n’avez pas l’intention de les changer.

Pour plus d’informations sur openGL et pour acquérir des connaissances de base et des exemples de travail, pensez à lire https://learnopengl.com/ .