opengl GLFW3创建窗口返回空值

tzdcorbm  于 2022-11-23  发布在  其他
关注(0)|答案(2)|浏览(131)

我试图把所有的GLFW3初始化代码从主文件到单独的文件。当我运行代码,我得到EXC_BAD_ACCESS上的glew初始化函数,因为GLFW不能创建窗口。在代码分离之前一切都是好的。有没有可能有GLFW设置代码在其他函数?
这是窗口管理器. 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);

windows_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));
nbnkbykc

nbnkbykc1#

不打开窗口的原因是除了其他窗口提示外,还必须指定GLFW_CONTEXT_VERSION_MINOR。这可以通过以下方式完成:

glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
dojqjjoe

dojqjjoe2#

令人惊讶的是,在我对下面的代码行进行注解后,它对我起作用了

/*
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
*/

相关问题