title: How to get OpenGL-ES working on Raspberry Pi with SDL2?
body: <p>I am trying to get OpenGL-ES working on a Raspberry Pi, but so far no luck. I compiled SDL 2.0.3 from source with this, as the version in Rasbian is missing Raspberry Pi support:</p>
  
  <pre><code>./configure --prefix=/home/pi/run/SDL2-2.0.3/ \
     --disable-video-x11 \
     --disable-pulseaudio \
     --disable-esd \
     --disable-video-opengl
  </code></pre>
  
  <p>The code below should create a OpenGL context and clear the screen to red. When I run the code, the Raspberry Pi is switching video modes, but the screen is turning black instead of red and the calls to <code>glGetString(GL_VERSION)</code> and Co. return <code>NULL</code> which would indicate that something is wrong with the GL context creation.</p>
  
  <pre><code>#include &lt;SDL.h&gt;
  #include &lt;SDL_opengles2.h&gt;
  #include &lt;iostream&gt;
  
  void print_gl_string(GLenum name)
  {
    const GLubyte* ret = glGetString(name);
    if (ret == 0)
    {
      std::cerr &lt;&lt; "error getting string: " &lt;&lt; name &lt;&lt; std::endl;
    }
    else
    {
      std::cerr &lt;&lt; name &lt;&lt; ": " &lt;&lt; ret &lt;&lt; std::endl;
    }
  }
  
  void set_gl_attribute(SDL_GLattr attr, int value)
  {
    if (SDL_GL_SetAttribute(attr, value) != 0)
    {
      std::cerr &lt;&lt; "SDL_GL_SetAttribute(" &lt;&lt; attr &lt;&lt; ", " &lt;&lt; value &lt;&lt; ") failed: " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
    }
  }
  
  int main()
  {
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
      std::cerr &lt;&lt; "SDL_Init() failed: " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
      exit(EXIT_FAILURE);
    }
  
    SDL_DisplayMode videomode;
    if (SDL_GetCurrentDisplayMode (0, &amp;videomode) != 0)
    {
      std::cerr &lt;&lt; "Error getting current display mode: " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
      exit(EXIT_FAILURE);
    }
  
    std::cout &lt;&lt; "Current screen mode: " &lt;&lt; videomode.w &lt;&lt; "x" &lt;&lt; videomode.h &lt;&lt; std::endl;
  
    set_gl_attribute(SDL_GL_RED_SIZE, 5);
    set_gl_attribute(SDL_GL_GREEN_SIZE, 6);
    set_gl_attribute(SDL_GL_BLUE_SIZE, 5);
    //set_gl_attribute(SDL_GL_DEPTH_SIZE, 8);
    set_gl_attribute(SDL_GL_DOUBLEBUFFER, 1);
  
    set_gl_attribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    set_gl_attribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    set_gl_attribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
  
    SDL_Window* window = SDL_CreateWindow("Minimal SDL2 Example",
                                          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                          720, 576,
                                          SDL_WINDOW_OPENGL);
    if (!window)
    {
      std::cerr &lt;&lt; "Could not create window: " &lt;&lt; SDL_GetError() &lt;&lt; std::endl;
      exit(EXIT_FAILURE);
    }
  
    SDL_GLContext gl_context = SDL_GL_CreateContext(window);
  
    print_gl_string(GL_RENDERER);
    print_gl_string(GL_SHADING_LANGUAGE_VERSION);
    print_gl_string(GL_VERSION);
    print_gl_string(GL_EXTENSIONS);
  
    glClearColor(1.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapWindow(window);
  
    SDL_Delay(5000);
  
    SDL_GL_DeleteContext(gl_context);
    SDL_DestroyWindow(window);
    SDL_Quit();
  
    return 0;
  }
  </code></pre>