SDL2 - Check if OpenGL context is created

安稳与你 提交于 2020-01-13 09:32:11

问题


I am creating an application using SDL2 & OpenGL, and it worked fine on 3 different computers. But on another computer (an updated arch linux), it doesn't, and it crashes with this error:

OpenGL context already created

So my question is: How do I check if the OpenGL context has already been created? And then, if it is already created, how do I get a handle for it?

If I can't do this, how do I bypass this issue?


回答1:


SDL2 does not in fact create an OpenGL context without you asking to make one. However, if you ask it to create an OpenGL context when OpenGL doesn't work at all, SDL2 likes to, erm, freestyle a bit. (The actual reason is that it does a bad job in error checking, so if X fails to create an OpenGL context, it assumes that it's because a context was already created)

So, to answer the third question ("how do I bypass this issue"), you have to fix OpenGL before attempting to use it. Figures, right?

To answer the first and second, well, no API call that I know of... but you can do it a slightly different way:

SDL_Window* window = NULL;
SDL_GLContext* context = NULL; // NOTE: This is a pointer!

...

int main(int argc, char** argv) {
    // Stuff here, initialize 'window'

    *context = SDL_GL_CreateContext(window);

    // More stuff here

    if (context) {
        // context is initialized!! yay!
    }

    return 2; // Just to confuse people a bit =P
}


来源:https://stackoverflow.com/questions/19530720/sdl2-check-if-opengl-context-is-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!