c++linuxopenglsdl-2openglcontext

SDL2 - Check if OpenGL context is created


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?


Solution

  • 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
    }