Creating an OpenGL Context in SDL 2

June 18th, 2013

Filed under: OpenGL, SDL | 5 comments

SDL 2.0 has been released, which changes the way you create OpenGL contexts. This post details the changes so you can convert your old SDL/OpenGL code to SDL 2. If you are new to SDL and OpenGL, you should also read my SDL and OpenGL article, as that article covers some topics that aren’t covered here, such as initializing SDL.

Data Types

To create an OpenGL context with SDL 2, you need to be aware of two new SDL data types: SDL_Window and SDL_GLContext. As the type names indicate, SDL_WIndow is a window, and SDL_GLContext is an OpenGL context. To create an OpenGL context with SDL 2, you must create a window, then create a context. The code to do this comes later, but for now, what you need to know is that your game needs variables to store the window and OpenGL context.

SDL_Window* mainWindow;
SDL_GLContext mainGLContext;

Window Flags

When you create an SDL window, you must supply flags that describe the type of window you want to create. The following are common flags to use:

  • SDL_WINDOW_OPENGL creates an OpenGL window. This flag is mandatory if you want to use OpenGL with SDL 2.
  • SDL_WINDOW_FULLSCREEN creates a fullscreen window.
  • SDL_WINDOW_RESIZABLE creates a resizable window.
  • SDL_WINDOW_SHOWN makes the window visible when the game loads.

Use the bitwise OR operator (|) to combine multiple flags.

Creating the Window and OpenGL Context

Call the function SDL_CreateWindow() to create a window. The function takes the following arguments:

  • The window title.
  • The x position of the window.
  • The y position of the window. If you don’t need the window to be in a specific position, you can supply the value SDL_WINDOWPOS_UNDEFINED for the x and y position.
  • The width of the window.
  • The height of the window.
  • The window flags that I covered in the previous section.

After creating the window, call the function SDL_GL_CreateContext() to create the OpenGL context. Supply the SDL window you created by calling SDL_CreateWindow(). The following code creates a 1024 by 768 pixel window and OpenGL context:

Uint32 flags = SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL;
int width = 1024;
int height = 768;

mainWindow = SDL_CreateWindow("SDL2 OpenGL Example", SDL_WINDOWPOS_UNDEFINED,
	SDL_WINDOWPOS_UNDEFINED, width, height, flags);
mainGLContext = SDL_GL_CreateContext(mainWindow);

Double Buffering

Most OpenGL games use double buffered drawing. When using double buffering, OpenGL erases the old scene and draws the new scene offscreen so the player doesn’t see the erasing and redrawing. After drawing the scene offscreen, swap the buffers to make the newly drawn scene appear on the screen. Call the function SDL_GL_SwapWindow() in your scene drawing code to swap the buffers in SDL 2, supplying the SDL window you created.

SDL_GL_SwapWindow(mainWindow);

Cleaning Up

Call the function SDL_GL_DeleteContext() to delete the OpenGL context you created. Call the function SDL_DestroyWindow() to destroy the SDL window you created.

SDL_GL_DeleteContext(mainGLContext);
SDL_DestroyWindow(mainWindow);

Source Code

I have a small sample program on Github that creates an OpenGL context and draws a rectangle. Look at the sdl2 branch for the SDL 2 code.

 

Tags:


5 thoughts on “Creating an OpenGL Context in SDL 2

  1. paulo says:

    and how i draw thing on the screen? SDL_BlitSurface?

    • Mark Szymczyk says:

      Paulo,

      My post covers creating an OpenGL context. In this case you would use OpenGL to draw.

      I use OpenGL to draw, not SDL, so I can’t tell you what SDL functions to use to draw to the screen.

  2. Lars Pensjö says:

    You are using legacy fixed function pipeline in your example source code. This may lead to problems for programmers that don’t understand the difference. And this is just the type of programmers this article is addressing.

    • Mark Szymczyk says:

      Lars,

      I wanted a simple example to demonstrate how to get started with SDL2 and OpenGL. My example has three lines of OpenGL code. Adding shader code would only complicate things. Anybody writing a game with SDL and OpenGL would rip out my code and add their own OpenGL code.

      You are welcome to reply to this with a link to your simple SDL2 example code that uses shaders and the programmable pipeline. I am sure my readers would benefit from your code that shows the modern way to use OpenGL.

  3. Kirill says:

    Thank you for this tutorial!

Leave a Reply to Mark Szymczyk Cancel reply

Your email address will not be published. Required fields are marked *