[an error occurred while processing this directive] [an error occurred while processing this directive]
Using SDL_Image with Opengl (c++)


What do games, edtiors, and movies have in common? Images of course! They all display images to the screen. Think about it, a game wouldn't really be much of a game if it didn't have images, also know as "textures," to set the mood.

For those of you out there just starting with Opengl, you may be wondering how to get textures into your program. Well, this article assumes you are also using SDL in your app. SDL (Simple Direct Media Layer) is good for creating applications that run on mutliple platforms. In fact, Space Smashout uses SDL! But enought ranting about SDL, I know what your really here for...

Lets say you have an SDL/Opengl application. Well, one observent person may notice there is an SDL library called SDL_Image. SDL_Image can load up textures of various file formts, including BMP, GIF, JPEG, PCX, PNG, TGA, just to name a few. The quesiton you may be asking now is: "Can I somehow use SDL_Image in conjunction with Opengl?" The answer is, "Yes, you can."

In fact, all you have to do is load up your SDL image into an SDL_Surface like you normally would, and use it as instructed below...

Yes, your thinking, now I can use SDL_Image with Opengl! So you start your app all happy-go-lucky and then, oh know, the texture look inverted!

That's right. For some odd reason SDL_Surfaces look inverted when used in Opengl.

Rats, foiled again! But wait, there is hope.

The code below will invert the SDL_Surface so it shows up correctly in your program.

Below shows how to use it.

Congratulations, you can use SDL_Image with Opengl now!

Get the source code.


[an error occurred while processing this directive]