简体   繁体   English

SDL2丢失了OpenGL上下文或未定义的功能

[英]SDL2 either lost OpenGL context or functions not defined

I am initiating the display, the renderer and the OGL context off of the display. 我正在从显示器启动显示器,渲染器和OGL上下文。 None of these gives an error, following is how I create these elements. 这些都没有错误,以下是我创建这些元素的方式。

if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
    printf("SDL initialization failed: %s\n", SDL_GetError());
    return false;
}

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

if ((display = SDL_CreateWindow("",
    50, 50, 600, 600, SDL_WINDOW_OPENGL)) == NULL)
{
    printf("Could not create window: %s\n", SDL_GetError());
    return false;
}

if ((graphics = SDL_CreateRenderer(display, -1, 0)) == NULL)
{
    printf("Could not get renderer: %s\n", SDL_GetError());
    return false;
}

if ((*context = SDL_GL_CreateContext(display)) == NULL)
{
    printf("Could not get context: %s\n", SDL_GetError());
    return false;
}

GLenum err = glewInit();
if (GLEW_OK != err)
{
    printf("GLEW couldn't be initialized: %s\n", glewGetErrorString(err));
    return false;
}

scene = SceneManager();

return true;

Now, after these, methods like glClearColor or glClear work as intended, however, methods like glCreateProgram or glActiveTexture point to NULL and throw a runtime exception. 现在,在这些之后,诸如glClearColor或glClear之类的方法可以按预期工作,但是,诸如glCreateProgram或glActiveTexture之类的方法指向NULL并抛出运行时异常。 If I don't include glew in the header, these functions aren't even identified. 如果我没有在标题中包含glew,这些功能甚至都不会被识别。

Am I losing my OpenGL context somehow and end up with an error, or are these functions not defined in SDL and I have to use another library to link these functions? 我是否会以某种方式丢失OpenGL上下文并导致错误,还是SDL中未定义这些函数,而我必须使用另一个库来链接这些函数?

According to your description of SceneManager 's constructor in comments, your problem has to do with when the constructor is called. 根据您在注释中对SceneManager的构造函数的描述,您的问题与调用构造函数有关。

I called it in the constructor of SceneManager, and as you can see it happens right after everything is created. 我在SceneManager的构造函数中调用了它,您可以看到它在创建所有内容后立即发生。 Again to be on the safe side, I tried calling it before scenemanager, it didn't output anything. 出于安全考虑,我再次尝试在Scenemanager之前调用它,但它没有输出任何内容。 So it somehow gets destroyed in here somewhere? 因此,它以某种方式在这里的某个地方被破坏了?

At the beginning of whatever scope SceneManager scene is declared, C++ implicitly constructs the object using the default constructor. 在声明任何范围的SceneManager scene开始时,C ++都会使用默认构造函数隐式构造该对象。 Thus, scene is actually constructed long before you get around to making a copy assignment on this line: scene = SceneManager (); 因此, scene实际上是在您可以在此行上进行复制分配之前很久才构造的: scene = SceneManager ();

You are not "losing your OpenGL context", you simply do not have one when the scene 's constructor is called for the very first time. 您并不是在“丢失OpenGL上下文”,而是在第一次调用scene的构造函数时根本就没有。 This why you usually declare these things like SceneManager* scene and then defer instantiation of the object scene points to until much later using new . 这就是为什么通常要声明诸如SceneManager* scene类的东西,然后将对象scene点的实例化推迟到以后再使用new You can either do that, or stop doing this in the constructor. 您可以这样做,也可以在构造函数中停止这样做。 But you cannot do things the way you have them right now, or you will call GL functions before SDL even creates a context for them. 但是您不能以现在拥有它们的方式来做事情,否则您将在SDL甚至为它们创建上下文之前调用GL函数。

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

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