HomeMultimedia Page 3 - Using OpenGL with SDL for Game Programming
Initialization: Bringing OpenGL into the Picture - Multimedia
In the world of gaming, SDL provides the entire necessary infrastructure. This would have become clear from previous articles in this series. Infrastructure is to a game what a skeleton is to a human body. But without muscles, no locomotion is possible. So working with the body analogy, SDL provides the skeletal structure to build the game whereas the flesh, blood and skin are provided by 2D and 3D graphics libraries.
In SDL all the sub-systems are initialized via SDL_Init(). OpenGL, being a part of the graphics subsystem, is not directly initialized in this manner. For initializing OpenGL, these three steps should be followed:
Set OpenGL Attributes
Specify use of Double Buffering
Set the Video Mode
Of these, the second one is optional as it is used only when double buffering is a requirement. Let's have a detailed look at all of them.
Setting the OpenGL Attributes
Before initializing the video, it is better to set up the OpenGL attributes. These attributes are passed to OpenGL via SDL calling the SDL_GL_SetAttribute() function. The parameters are the OpenGL attribute and their values. The most common attributes passed to this function are:
1. SDL_GL_RED_SIZE:
It sets the size of the red component of the frame buffer. The value is in bits. The most commonly used value is 5. Similar parameters exist for blue and green components which are SDL_GL_BLUE_SIZE and SDL_GL_GREEN_SIZE respectively. To set green component to a bit value of 4 the code would be:
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );
2. SDL_GL_BUFFER_SIZE:
To set the size of the frame buffer this attribute is passed with the required Buffer size in bits. It is greater than or equal to the combined value i.e. sum of the red, green, blue and alpha components. If the requirement is 24 bit color depth and an alpha channel of 32 bits then each color component must be given the size value of 8 and the frame buffer must be given a size of 32. In code it would be:
This attribute controls the size of the depth buffer or Z buffer. Normally graphics cards provide 16-bit or 24-bit depth. If the value is set higher than what is available, the operation will fail. To make it more clear, check out the following code:
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
4. SDL_GL_ACCUM_RED_SIZE:
To set the size of the red component's accumulation buffer, this attribute is used.
The value is specified in bits. SDL_GL_ACCUM_BLUE_SIZE, SDL_GL_ACCUM_GREEN_SIZE controls the size of blue and green components' accumulation buffer. In code it would be:
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,5);
You are probably wondering right now whether these attributes could be set after initializing the video mode. The answer is no. The reason is that these settings have to be initialized before invoking and configuring the video mode. Next we have to set up the video mode.
Setting up double buffering
This aspect must also to be covered before setting up the video mode as this attribute goes as a flag parameter to the SDL_GL_SetAttribute. The attribute is SDL_GL_DOUBLEBUFFER and the value is either 1 or 0. The point to be kept in mind is that when working in conjunction with OpenGL, the flag specifying the double buffer must be passed as an attribute to the SDL_GL_SetAttribute function and not to the SDL_Set_VideoMode(). In code this would be:
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
would set the double buffering to one state.
Setting the Video Mode
Once the OpenGL attributes are set, setting the video mode is similar to the setting of video mode as described in previous tutorials. For more details have a look at the second article in this series. The only difference comes in flags being sent to the SDL_Set_VideoMode(). Apart from other required flags the SDL_OPENGL would also be set i.e.
int flags=0; flags= SDL_OPENGL | SDL_FULLSCREEN;
Setting the SDL_OPENGL flag is a must.
That covers all the required steps, Now let's see OpenGL at play.