简体   繁体   English

SDL2 - 检查是否已创建OpenGL上下文

[英]SDL2 - Check if OpenGL context is created

I am creating an application using SDL2 & OpenGL, and it worked fine on 3 different computers. 我正在使用SDL2和OpenGL创建一个应用程序,它在3台不同的计算机上运行良好。 But on another computer (an updated arch linux), it doesn't, and it crashes with this error: 但是在另一台计算机(更新的arch linux)上,它没有,它崩溃了这个错误:

OpenGL context already created

So my question is: How do I check if the OpenGL context has already been created? 所以我的问题是:如何检查是否已经创建了OpenGL上下文? 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? 如果我不能这样做,我该如何绕过这个问题?

SDL2 does not in fact create an OpenGL context without you asking to make one. 事实上,SDL2实际上并没有要求创建一个OpenGL上下文。 However, if you ask it to create an OpenGL context when OpenGL doesn't work at all, SDL2 likes to, erm , freestyle a bit. 但是,如果你问它创建时的OpenGL完全不工作,OpenGL上下文,SDL2喜欢, ,自由泳位。 (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) (实际原因是它在错误检查中做得不好,所以如果X无法创建OpenGL上下文,它会认为是因为已经创建了上下文)

So, to answer the third question ("how do I bypass this issue"), you have to fix OpenGL before attempting to use it. 因此,要回答第三个问题(“我如何绕过此问题”),您必须在尝试使用OpenGL之前修复它。 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: 要回答第一个和第二个,好吧,没有我知道的API调用...但你可以采用稍微不同的方式:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM