Simple Game Development with ClanLib 6.0Part 4Using OpenGL with ClanLib
In this tutorial, we'll be moving away from the Tic-Tac-Toe example and towards a few more exciting applications for ClanLib. The CL API gives us easy access to the OpenGL hardware acceleration present on most newer video cards. With OpenGL, we can create 2-D or 3-D games with fancy effects like alpha-blending and sprite scaling without the significant slowdowns that can happen when using CL_Display. Another bonus is that OpenGL, like ClanLib, is cross-platform. Direct3D is not.
Above is a screenshot from a ClanLib game that uses OpenGL. If you take a look at the larger version, you can see some of those fancy particle and blending effects in action. One of the things I am most often asked about is how to create good-looking exhaust and explosion effects - so in this part of the tutorial and the next we will build a particle system similar to the one used in the game, as well as covering the basics of how to intialize and use OpenGL with ClanLib. We'll start by creating an empty win32 Application and an empty CL framework which you can get from part 1. We'll be calling our main class Particles. (Note that from here on, I'll be focusing only on the important code - a complete listing is available in the workspace zip file at the bottom.) Fig 12.
Note the instance of Ship - our aim for this tutorial is to put an image of a ship on the screen and have it fly in a circle. Later we'll add the particles that make up the exhaust plume. First, though, we'll store the ship's properties in a struct (simply equivalent to a class without functions and all members public). Fig 13.
One of the ways ClanLib makes using OpenGL easier is that it can automatically load textures from supported image types. Normally, we would have to write our own methods for doing this or use an external library. But here, we simply store a CL_Texture pointer and call bind() when we want to use it in OpenGL. For users familiar to OGL, this is virtually equivalent to using glBind(...). ClanLib provides a convenient way of including different types of resources like images, sounds, or even numbers without compiling them into the code of your game. It does this by using a seperate resource file that is read in by the CL_ResourceManager class on startup (or whenever you need it to). A sample resource file looks like this: Fig 14.
Now if you decide that ship_targa.tga isn't the best file for your application, you can simply open up your resource file with any text editor and enter a different image filename. The resource name that we can use inside the application contains the section name followed by a forward slash, then the resource name, like this: "Graphics/ship" for the first graphic file. Sections can be nested just like a file system.
This is a slight deviation from CL on my part - though the API includes its own 2-D Vector class, I have included my own simple one. It provides a way of translating between polar and Cartesian coordinate systems, as I have found it more intuitive to store movements as a direction and magnitude (polar), rather than two magnitudes (Cartesian). It does contain an example of operator overloading, but that's outside the scope of this tutorial.
![]() This is our trusty rocket ship, a 32-Bit Targa file, with red indicating transparency. In Photoshop, creating the mask that marks certain areas transparent is as simple as making a selection, creating a new channel, and filling the selection on the new channel layer. In other editors it should be a similar process.
Before we can begin drawing things with OpenGL, we have to set up our screen viewport, projection and the various GL attributes like blending and clear color. This is what's going on in Fig. 19, and it should only be done once CL_SetupGL::init() has been called. For detailed explanations of the GL properties being set, see the OpenGL Red and Blue reference books or NeHe. Fig 19.
Now that GL is initialized the way we need it, we can add the main loop and the rest of the rocket code to the main() function. Fig 20.
Nothing excessively complicated here, if you're comfortable with OpenGL transformations and some basic trigonometry.
![]() That does it for the spinning rocket ship - for the complete source and Vector2 class, see the workspace zip file at the bottom. Next we'll be adding those shimmering particles and a scrolling starfield. If you've followed along this far, the next part really brings the fun stuff together and demonstrates just a small chunk of what OpenGL can do. |