When your application starts, you must initialize SDL in order to call any SDL functions in your program. Call the function SDL_Init() to initialize SDL. This function takes one argument: flags that tell SDL the subsystems to initialize. There are five subsystems: timer, audio, video, CD_ROM, and joystick. Use the flag SDL_INIT_EVERYTHING to initialize all five subsystems.
int error; error = SDL_Init(SDL_INIT_EVERYTHING);
Setting the OpenGL Attributes
To use OpenGL in a SDL program, you must create a draw context. A draw context is where your program draws graphics. If you don’t create a draw context, your program will crash when it makes an OpenGL function call.
Before creating an OpenGL draw context with SDL, you must specify the attributes you want the context to have. Call the function SDL_GL_SetAttribute() to set an attribute. This function takes two arguments. The first argument is the name of the attribute, and the second argument is the desired value for the attribute.
The OpenGL attributes you can set generally involve the size of OpenGL’s frame, depth, stencil, and accumulation buffers. One attribute virtually every game needs is double buffering. When you use double buffering, drawing is done in an offscreen buffer and the image is moved from the offscreen buffer to the screen. Use the following call to use double buffering in OpenGL:
If you’re writing a 3D game, you must set the size of the depth buffer. The following code requests a 16-bit depth buffer:
On Mac OS X you don’t need to set the size of the frame buffer’s red, green, blue, and alpha components. If you create a draw context with 32 bit color, Mac OS X uses 8 bits for each of the four components. On other operating systems, you may need to set the size of the frame buffer’s components. The following code sets 8 bits of red, green, blue, and alpha:
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
Next (Creating the OpenGL Draw Context)
Previous (Setting Up SDL in Xcode)