title: SDL2 and GLEW, Unable to get proper OpenGL version if using SDL_GL_SetAttribute()
body: <p>I am using SDL2 for OS interfacing and GLEW for OpenGL function loading. Initially I had my SDL_GL_SetAttribute() calls <strong>in-between window creation and context creation</strong>. I noticed that my SDL_GL_SetAttribute() calls made little effect, so I re-read <a href="http://wiki.libsdl.org/SDL_GL_SetAttribute" rel="nofollow">the documentation</a> and it states "The requested attributes should be set <strong>before creating an OpenGL window</strong>". If I try this, I only manage to get an <strong>OpenGL 1.1</strong> context (the one Microsoft ships). If I don't call SDL_GL_SetAttribute() at all I get a 4.4 context with no issue.</p>
  
  <p>One strange thing I've noted is my calls to SDL_GL_GetAttribute() after I create the context seem to return inaccurate values. If I <strong>don't</strong> set any attributes I get:</p>
  
  <pre><code>SDL GL Context Major Version: 2
  SDL GL Context Minor Version: 1
  SDL GL Context Profile: Unknown
  SDL GL Accelerated Visuals: True
  SDL GL Double Buffering: True
  SDL GL Depth Size: 24
  ----------------------------------------------------------------
  Graphics Successfully Initialized
  OpenGL Info
    Version: 4.4.0
    Vendor: NVIDIA Corporation
    Renderer: GeForce GT 650M/PCIe/SSE2
     Shading: 4.40 NVIDIA via Cg compiler
  ----------------------------------------------------------------
  </code></pre>
  
  <p>If I <strong>do</strong> set all the attributes I get:</p>
  
  <pre><code>SDL GL Context Major Version: 3
  SDL GL Context Minor Version: 1
  SDL GL Context Profile: Compatibility
  SDL GL Accelerated Visuals: True
  SDL GL Double Buffering: True
  SDL GL Depth Size: 32
  ----------------------------------------------------------------
  Graphics Successfully Initialized
  OpenGL Info
      Version: 1.1.0
       Vendor: Microsoft Corporation
     Renderer: GDI Generic
      Shading: (null)
  ----------------------------------------------------------------
  </code></pre>
  
  <p>Note how in the first case, SDL says I have a 2.1 context, while I actually get a 4.4. In the second case it says I have a 3.1 context (what I requested), when I actually have a 1.1.</p>
  
  <p>My current setup code (which follows what is outlined in <a href="http://www.opengl.org/wiki/Tutorial1%3a_Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL_%28C_/_SDL%29" rel="nofollow">this tutorial</a>). Error checking omitted for brevity.</p>
  
  <h2>SDL</h2>
  
  <pre><code>SDL_Init(SDL_INIT_VIDEO);
  
  // Request compatibility because GLEW doesn't play well with core contexts.
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); 
  SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32); 
  
  SDL_Window * window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
  
  SDL_GLContext context = SDL_GL_CreateContext(window);
  
  SDL_GL_MakeCurrent(window, context)
  
  // Print what the OS actually gave us for each value we requested.
  SDL_GL_GetAttribute(/* ... */);
  </code></pre>
  
  <h2>Directly followed by GLEW</h2>
  
  <pre><code>glewExperimental = GL_TRUE;
  GLenum result = glewInit();
  if(result != GLEW_OK)
  {
    // Handle &amp; print error
  }
  
  if(GLEW_VERSION_1_1)
  {
    printf("----------------------------------------------------------------\n");
    printf("Graphics Successfully Initialized\n");
    printf("OpenGL Info\n");
    printf("    Version: %s\n", glGetString(GL_VERSION));
    printf("     Vendor: %s\n", glGetString(GL_VENDOR));
    printf("   Renderer: %s\n", glGetString(GL_RENDERER));
    printf("    Shading: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
    printf("----------------------------------------------------------------\n");
  }
  else
  {
    printf("Unable to get any OpenGL version from GLEW!");
  }
  
  if(!GLEW_VERSION_3_1)
  {
    // Handle &amp; print error
  }
  </code></pre>