Question

I am initiating the display, the renderer and the OGL context off of the display. 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. If I don't include glew in the header, these functions aren't even identified.

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?

Was it helpful?

Solution

According to your description of SceneManager's constructor in comments, your problem has to do with when the constructor is called.

I called it in the constructor of SceneManager, and as you can see it happens right after everything is created. Again to be on the safe side, I tried calling it before scenemanager, it didn't output anything. 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. Thus, scene is actually constructed long before you get around to making a copy assignment on this line: 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. 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top