在多个函数中使用全局 glfw window

using a global glfw window in multiple functions

我有一个 int main(); 函数和一个 int Game(); 函数,在主函数中我有一个 window 我在这两个函数中都使用了。我已经在全局范围内定义了 window,当我在主函数中的 GLFW 循环之前尝试 运行 Game(); 函数时,window 打开一秒钟然后关闭。然后我得到

Error: The GLFW library is not initialised

打印自 error_callback();

这是我的代码

#include <GLFW/glfw3.h>
#include <GL/freeglut.h>
#include <GL/GL.h>
#include <stdio.h>
#include <string>
#include <iostream>

using namespace std;

void drawText(const char *text, int length, int x, int y);
void framebuffer_size_callback(GLFWwindow* wndow, int width, int height);
void DrawCube(GLfloat centerPosX, GLfloat centerPosY, GLfloat centerPosZ, GLfloat edgeLength);
void button(GLfloat red, GLfloat green, GLfloat blue, int x, int y, int width, int height);
void error_callback(int error, const char* description);

int Game();

int width = 860, height = 490;
GLFWwindow* window;



int main(int argc, char **argv) {
    //GLTtext* text = gltCreateText();
    //gltSetText(text, "ElectroCraft");

    //GLFWwindow* window;
    int width = 860, height = 490;
    if (!glfwInit()) {
        printf("failed to init glfw");
        return -1;
    }
    glutInit(&argc, argv);
    window = glfwCreateWindow(width, height, "ElectroCraft", NULL, NULL);
    glfwMakeContextCurrent(window);
    if (!window) {
        printf("failed to start window");
        glfwTerminate();
        return -1;
    }
    Game();
    glEnable(GL_DEPTH_TEST);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, 0, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    string text;

    glfwSetErrorCallback(error_callback);

    while (!glfwWindowShouldClose(window)) {
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
        glClearColor(53.0f / 255.0f, 81.0f / 255.0f, 92.0f / 255.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_LINES);
        glVertex2f(0, height-80);
        glVertex2f(width, height - 80);
        glEnd();
        button(192.0f / 255.0f, 192.0f / 255.0f, 192.0f / 255.0f, width / 2 - 70, height / 2, 260, 50);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 1;
}

void error_callback(int error, const char* description) {
    fprintf(stderr, "Error: %s\n", description);
}

int Game() {
    //glfwMakeContextCurrent(window);
    glEnable(GL_DEPTH_TEST);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, 0, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    while (!glfwWindowShouldClose) {
        glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
        glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 1;
}

这可能是因为我没有在全局启动 GLFW 库,但我似乎无法执行此操作,因为如果 if 语句在函数外部,我会收到错误消息

the window opens for a second and then closes

当然,因为glfwWindowShouldClose在函数Game中不是函数调用。 glfwWindowShouldClose 是一个函数指针,因此 !glfwWindowShouldClose 的计算结果为 false 并且循环永远不会运行:

while (!glfwWindowShouldClose) {

while (!glfwWindowShouldClose(window)) {

I then get Error: The GLFW library is not initialised

这是因为GLFW在函数Game中被glfwTerminate()终止了。从函数 Game.

中删除 glfwTerminate()

但是请注意,关闭 window 后,您必须创建一个新的 window。一个选项是隐藏 glfwHideWindow and show glfwShowWindow a window。