title: How to use OpenGL context in SDL2?
body: <p>My questions are</p>
  <ol>
  <li>How to use OpenGL context in SDL2?</li>
  <li>What library/libraries is/are needed to use OpenGL in SDL2?</li>
  </ol>
  <p>I am on Windows 10, vs2019.</p>
  <p>I am trying use OpenGL context in SDL2.
  I followed the tutorial in the below link.
  In the tutorial, their link to gl3.h is dead, 404.</p>
  <p><a href="https://www.khronos.org/opengl/wiki/Tutorial1:_Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL_(C_/_SDL)" rel="nofollow noreferrer">https://www.khronos.org/opengl/wiki/Tutorial1:_Creating_a_Cross_Platform_OpenGL_3.2_Context_in_SDL_(C_/_SDL)</a></p>
  <p>I tried to get a copy from their GitHub, and I don't think that the correct one.</p>
  <p><a href="https://github.com/KhronosGroup/OpenGL-Registry" rel="nofollow noreferrer">https://github.com/KhronosGroup/OpenGL-Registry</a></p>
  <p>I also tried glew, (glad, Core 3.0, no extension). glew doesn't work.</p>
  <p>glad kind of work, but I get an error</p>
  <pre><code>Exception thrown at 0x0000000000000000 in GLTest.exe: 0xC0000005: Access violation executing location 0x0000000000000000.
  </code></pre>
  <p>The error above is referring to the line</p>
  <pre><code>    glClear ( GL_COLOR_BUFFER_BIT ); &lt;&lt;&lt; this line
  
  </code></pre>
  <p>full code</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  /* If using gl3.h */
  /* Ensure we are using opengl's core profile only */
  #define GL3_PROTOTYPES 1
  //#include &lt;GL3/gl3.h&gt;
  
  #include &lt;glad/glad.h&gt;
  #include &lt;SDL.h&gt;
  #define PROGRAM_NAME &quot;Tutorial1&quot;
  
  /* A simple function that prints a message, the error code returned by SDL,
   * and quits the application */
  void sdldie(const char *msg)
  {
      printf(&quot;%s: %s\n&quot;, msg, SDL_GetError());
      SDL_Quit();
      exit(1);
  }
  
  
  void checkSDLError(int line = -1)
  {
  #ifndef NDEBUG
      const char *error = SDL_GetError();
      if (*error != '\0')
      {
          printf(&quot;SDL Error: %s\n&quot;, error);
          if (line != -1)
              printf(&quot; + line: %i\n&quot;, line);
          SDL_ClearError();
      }
  #endif
  }
   
  
  /* Our program's entry point */
  int main(int argc, char *argv[])
  {
      SDL_Window *mainwindow; /* Our window handle */
      SDL_GLContext maincontext; /* Our opengl context handle */
  
      if (SDL_Init(SDL_INIT_VIDEO) &lt; 0) /* Initialize SDL's Video subsystem */
          sdldie(&quot;Unable to initialize SDL&quot;); /* Or die on error */
  
      /* Request opengl 3.2 context.
       * SDL doesn't have the ability to choose which profile at this time of writing,
       * but it should default to the core profile */
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
      SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  
      /* Turn on double buffering with a 24bit Z buffer.
       * You may need to change this to 16 or 32 for your system */
      SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
      SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  
      /* Create our window centered at 512x512 resolution */
      mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
          512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
      if (!mainwindow) /* Die if creation failed */
          sdldie(&quot;Unable to create window&quot;);
  
      checkSDLError(__LINE__);
  
      /* Create our opengl context and attach it to our window */
      maincontext = SDL_GL_CreateContext(mainwindow);
      checkSDLError(__LINE__);
  
  
      /* This makes our buffer swap syncronized with the monitor's vertical refresh */
      SDL_GL_SetSwapInterval(1);
  
      /* Clear our buffer with a red background */
      glClearColor ( 1.0, 0.0, 0.0, 1.0 );
      glClear ( GL_COLOR_BUFFER_BIT );&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;Right Here
      /* Swap our back buffer to the front */
      SDL_GL_SwapWindow(mainwindow);
      /* Wait 2 seconds */
      SDL_Delay(2000);
  
      /* Same as above, but green */
      glClearColor ( 0.0, 1.0, 0.0, 1.0 );
      glClear ( GL_COLOR_BUFFER_BIT );
      SDL_GL_SwapWindow(mainwindow);
      SDL_Delay(2000);
  
      /* Same as above, but blue */
      glClearColor ( 0.0, 0.0, 1.0, 1.0 );
      glClear ( GL_COLOR_BUFFER_BIT );
      SDL_GL_SwapWindow(mainwindow);
      SDL_Delay(2000);
  
      /* Delete our opengl context, destroy our window, and shutdown SDL */
      SDL_GL_DeleteContext(maincontext);
      SDL_DestroyWindow(mainwindow);
      SDL_Quit();
  
      return 0;
  }
  </code></pre>