HomeMultimedia Page 4 - Using OpenGL with SDL for Game Programming
OpenGL with SDL in Action - 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.
The theory is over. It's now time to see some real action. The example application will render a rotating triangle. The includes will contain one more header file.
#include <SDL/SDL.h> #include <gl/gl.h>
gl.h contains function declarations necessary for working with OpenGL.
The video is initialized to 600x300 resolutions. And the hardware rendering mode is being used. This is done by the SDL_HWSURFACE flag. Hence OpenGL would write on the graphic card's memory instead of mapping it to software memory. After this step, we move into the territory of OpenGL.
To start working with OpenGL, the view port is initialized. Then the screen is cleared or rendered with the specified background color. Since the triangle would be rotating in 3D space, the depth has to be set and depth testing has to be enabled. If smooth shading is not used, then the edges would seem jagged. Hence the smooth shading model is used.
This completes the setting up of OpenGL parameters after SDL video initialization. Drawing and rotation is taken care of by the following code in bold:
The focus is brought to the point of origin by translating it. Then rotation function is provided with the theta value through which the triangle has to be rotated.
That's it. This is how SDL and OpenGL work together. Though this section covers the technical details of OpenGL, the complete steps for using OpenGL are not discussed. That will be the topic of the next discussion.