【问题标题】:GLFW3 create window returns nullGLFW3 创建窗口返回 null
【发布时间】:2015-06-17 04:14:24
【问题描述】:

我试图将所有 GLFW3 初始化代码从主文件放到单独的文件中。 当我运行代码时,我在 glew init 函数上得到 EXC_BAD_ACCESS,因为 GLFW 无法创建窗口。在代码分离之前一切正常。是否可以在其他功能中有 GLFW 设置代码?

我开始学习 C 和 openGL,因此我们将不胜感激。

这是 window_manager.h

typedef struct Window_manager
{
    GLFWwindow *window;
    GLuint window_width;
    GLuint window_height;
    const char *window_title;
} Window_manager;

Window_manager *set_up_window(GLuint width, GLuint height, const char *title);

window_manager.c 中的代码

Window_manager *set_up_window(GLuint width, GLuint height, const char *title)
{
    Window_manager *win_man = malloc(sizeof(Window_manager));
    // Init GLFW
    //glfwSetErrorCallback(error_fiutallback);

    if (!glfwInit())
    exit(EXIT_FAILURE);

    // Set all the required options for GLFW
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

    win_man->window_width = width;
    win_man->window_height = height;
    win_man->window_title = title;
    win_man->window = glfwCreateWindow(win_man->window_width, win_man->window_height, win_man->window_title, NULL, NULL);
    glfwMakeContextCurrent(win_man->window);

    // Set the required callback functions
    //glfwSetKeyCallback(win_man->window, key_callback);

    return win_man;
}

在 main.c 中

int main(int argc, const char * argv[])
{
    Window_manager *win_man = set_up_window(800, 600, "fjut");       
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    GLenum err = glewInit();
    if (GLEW_OK != err)
 {
    //Problem: glewInit failed, something is seriously wrong.
    printf("Error: %s\n", glewGetErrorString(err));
 }
 fprintf(stdout, "Status: Using GLEW %s\n",
 glewGetString(GLEW_VERSION));

【问题讨论】:

  • 您是否也尝试过设置 GLFW_CONTEXT_VERSION_MINOR?
  • @BDL glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);这条线完美无缺。感谢您的帮助。

标签: c opengl struct code-separation


【解决方案1】:

没有打开窗口的原因是除了其他窗口提示之外,还必须指定 GLFW_CONTEXT_VERSION_MINOR。这可以通过例如:

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多