我有一个 int main();
函数和一个 int Game();
函数,在主函数中我有一个窗口,我在这两个函数中都使用了它。我已经在全局范围内定义了窗口,当我尝试在 main 函数中的 GLFW 循环之前运行 Game();
函数时,窗口会打开一秒钟然后关闭。然后我得到
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 被 glfwTerminate()
终止了在函数 Game
中。从函数 Game
中删除 glfwTerminate()
。
但是请注意,一旦您关闭了一个窗口,您就必须创建一个新窗口。一个选项是隐藏 glfwHideWindow
并显示glfwShowWindow
一个窗口。
关于c++ - 在多个函数中使用全局 glfw 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58679755/