SDL Drawing Surfaces

Let's analyse our SDL example program to see how it works.

The first thing we need to do when doing graphics with SDL, is to obtain a drawing surface. This is about the only negotiation we need to do with SDL, and fortunately, it is very simple to do.

In order to obtain a drawing surface, we need to give SDL some information about what we'd like, and then it does its best to match that for us. In particular, we need to tell it what horizontal and vertical size we'd like the surface to be, what colour bit depth we'd like, whether we want fullscreen or not (the default is a drawing surface inside a window) and whether we want a hardware or software surface.

In this tutorial, we are going to use full screen mode, and to support the greatest number of video cards, we are going to work in 640x480 mode. For now we'll work with a colour depth of 32 bits. This allows-na for 2^32 = a bazillion colours, which will be more than enough for us. Some typical display sizes that we could have used are 640x480, 800x600, 1024x768, 1280x1024, and some typical colour depths are 8 bit, 16 bit, 24 bit and 32 bit, though not every display adapter/screen supports each combination of these.

Hardware surfaces are much faster than software surfaces in general, so we'll specify that we want a hardware surface by using the SDL_Hws-naURFACE flag when we request our SDL drawing surface.

Here are the lines of code that are required to initialise SDL and request a drawing surface:

#define WIDTH 640
#define HEIGHT 480
#define DEPTH 32

int main(int argc, char* argv[])
  SDL_Surface *screen; 

  return 0;

The first line inside our main program simply declares a pointer to an internal SDL type, called SDL_SURFACE. The SDL_SetVideoMode function returns a pointer to such a surface, and it is this that we write to to draw on the screen.

In our actual example program, we add a few additional lines to the above, to test whether the SDL_Init and SDL_SetVideoMode function calls were successful or not. The first returns a negative integer if it fails. The second returns zero if it fails. In particular, we call SDL_Quit() if the second function call fails, to have SDL deactivate gracefully.