Unable to create OpenGL window in SDL2

Started by
3 comments, last by jomodev 4 years, 4 months ago

I am currently working on a C++ + SDL2 game engine and I am stuck on a problem. The problem is that whenever I pass SDL_WINDOW_OPENGL to SDL_CreateWindow(), it return null and so my program stops. But it works fine if I pass any other flag while not passing SDL_WINDOW_OPENGL.

This is the function I am calling SDL_CreateWindow():

Game::Game(const char* title, int w, int h, int x, int y, int glmajversion, int glminversion, Uint32 flags){
  if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
    std::cout<<"[Error]: Unable to initialize SDL2."<<std::endl;
  }

  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, glminversion);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, glmajversion);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
  window = SDL_CreateWindow(title, x, y, w, h, flags);
  if (!window){
    std::cout<<"[Error]: Unable to create SDL2 window."<<std::endl;
    exit(-1);
  }
  context = SDL_GL_CreateContext(window);
  SDL_GL_MakeCurrent(window, context);
}
Advertisement
Have you tried calling SDL_GetError() and printing the result somewhere?

Also what values are you passing into the other parameters of the Game constructor?

What platform are you running on?

Also, what does the output of the following show for your system?

glxinfo | grep OpenGL

Have you tried a basic SDL window as in the Wiki example?

https://wiki.libsdl.org/SDL_CreateWindow

This looks similar to the issue you are running into:

https://stackoverflow.com/questions/36081319/sdl-cant-create-opengl-windows

This topic is closed to new replies.

Advertisement