Using SDL with OpenGL

I am writing this tutorial with the assumption that you have some knowledge about programming with opengl and sdl because this tutorial is about using the two together. Aside from that, i wont go in-depth with either, but i’ll try to explain as much i can bother to.

Why use opengl with sdl?

I recommend it for 2 reasons. Reason one is that sdl deals with all the crossplatform programming for you, so writing code with sdl will work on a range of other platforms, with very little modification if any at all. Plus, it comes with other libraries that will definitely come in handy, such as imaging and audio libraries. Secondly, its just plain easy to use. When your done reading this tutorial, compare using sdl with opengl to using the win32 api to create your window and handle events. Here is a painful example Dont get me wrong, its a very good tutorial, its where i started. But, you may not have the time and patience to go through all that, so using sdl is a waaaaaay better alternative. So, lets get started.

Lets get stared

I’m just gonna run through the setting up process. If you dont know how to setup sdl then check here That site has a variety of sdl tutorials that will come in handy as well.

First thing to do is to create a empty win32 project.

Secondly, link the appropriate libraries.

Thirdly, create a main.cpp file and start coding.

#include <sdl/sdl.h>
#include <windows.h> //needed on windows
#include <gl/gl.h>

include sdl.h and gl.h to use sdl and opengl respectively. If you’re programming for windows, include windows.h else you’ll get alot of “int APIENTRY” redefinition errors.

next is the main function. When using sdl be sure your main function’s header looks like this:

int main(int argc,char **argv)

and not this

 int main() 

else you’ll end up getting an error saying ” external symbol ‘_SDL_main” referenced in function _main”, or something similar depending on the compiler.

Next is the initialization of the variables to be used.

int width=800;
 int height=600;
 SDL_Event evt;
 bool done=false;

width and height defines the size of the screen. SDL_Event will be used in capturing events sent by dl. done will be used to decide if the application should be closed.

After this comes the initialization of sdl’s video subsystem, which is required to display anything, and the creation of  the window using SDL_SetVideoMode.

SDL_Surface *screen = SDL_SetVideoMode(width,height,32,SDL_DOUBLEBUF|SDL_OPENGL);

Now, initialize  opengl.

//setup projection matrix
//modelview matrix
//set clear color

next, enter the main loop, or the heartbeat, of the program. This will loop will run as long as done is equal to false. In this program, done is equal to true if we close the window; thats when sdl sends a SDL_QUIT event.

 //handle events

it is important that you handle sdl’s events, not doing so will cause you application to crash.
The next chunk of code render clears the screen and renders a triangle to it.

//clear screen
 glClear(GL_COLOR_BUFFER_BIT); //draw triangle //begin
 glVertex3f(500,400,0); //end
 glEnd(); SDL_GL_SwapBuffers();// equivalent to SwapBuffers( hDC );

I only cleared the color buffer since no depth is required for this tutorial. You’re supposed to be familiar with rendering a simple triangle in opengl. I didnt use a quad since they are unsupported in opengles and webgl so i’m getting you in the habit of using triangles and i’ll be showing how to setup opengl on those plaforms in later posts.

If you’ve used opengl with the win32 api before i’m guessing your familiar with SwapBuffers( hDC ) and you’re probably wondering why do i have

SDL_Flip(screen) after  SDL_GL_SwapBuffers(). The reason is simple, in sdl whenever you render or blit anything, it is done on the SDL_Surface created when the video mode is set (SDL_SetVideoMode). Then, displaying it to the window is done by calling SDL_Flip(screen). Whatever is rendered using opengl goes to the screen(SDL_Surface), then SDL_Flip draws it to the window.

You should have something looking like this:

Thats it really. SDL is a great library for making cross-platform games easily. Making a good 3d game is easy when you mix these 2 libraries. One example of a popular software that uses opengl with sdl is Blender3D. Check its application folder if you dont believe me.

I hope i made your life easier 😀


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s