opengl window 初始化问题

opengl window initialization issue

我正在尝试将 GLFW 封装在 class 中以使我的代码更清晰。

所以在我的 class 中,我已经完成了基本的 window 创建,但是当我执行程序时,它总是进入 if 语句,我检查是否初始化到底对不对,不知道为什么。

这就是我的

#include "include/Window.h"
#include <iostream>

Window::Window(int x, int y) {
    _window = glfwCreateWindow(x, y, "Heightmap Visualizer", NULL, NULL);
}

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

int Window::createWindow() {
    if (!_window) {
        std::cerr << "went here" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(_window);
    run();
    glfwTerminate();
    return 0;
}

void Window::run() {
    while (!glfwWindowShouldClose(_window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(_window);
        glfwPollEvents();
    }
}

这是头文件

#include <GLFW/glfw3.h>

class Window {
public:
    Window(int x, int y);
    ~Window();
    int createWindow();
    void run();

private:
    GLFWwindow* _window;
};

确保 glfwInit()。

也祝你在学习GL和C++的路上好运!