openGL and SDL question (loading and using sprites)

KidTsunami
Unregistered
 
Post: #1
I am pretty new to both openGL and SDL and I am having a hard time figuring out how to load a sprite through SDL that I can use with openGL for my 2D game engine? Any ideas?
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #2
Basically, I sidestepped the image loading functions from SDL completely.

You can still use SDL for input, threads, and changing video modes, but get the image loading code from somewhere else. I used the targa loader from the Nehe site. Basically, you load it right into an OpenGL texture. Remember, when you use OpenGL and SDL together most of the graphics (blitting, etc.) are handled by OpenGL.

Check out this tutorial and others at nehe.gamedev.net You want to look at the bottom of the page and download the Linux/SDL code. Remember, SDL code is cross-platform, so you can compile it fine in OS X. Make a SDL project, remove the main and replace it with the downloaded tutorial file. Just remember to change the GL includes to:

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

That's the beauty of SDL. Smile

I had good luck with this tutorial, and it has a couple of good ideas I hadn't seen before in the code.

I believe you can take the raw data from the SDL_image loaders and stick them into a texture as well, but I haven't looked into this in detail.

Aaron

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #3
Yeah, I just downloaded SDL_image, I was planning on using those to load in my sprites, what do you think is the best file format for sprites?

Also do you think I should use a different loader than SDL_image?

I'm sorry but I don't know what targa is, I'm just guessing its a graphic file type that I don't know.
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #4
yep, targa is another graphics file format. It's nice because you can have an alpha channel for varying levels of transparency in your pixels (for things like anti-aliased edges.)

Being able to use images with an alpha channel easily and efficiently is one of the great benefits of using OpenGL.

If you use the code in that tutorial for loading a targa you won't need to mess with the SDL_Image stuff right away.

Another file format to look into eventually is PNG.

If you plan on using SDL_Image, you can do a google search for "SDL_Image OpenGL" or maybe get on the SDL mailing list and ask there. (Actually, I did a quick google search just now and here is a promising page. )

I'm currently using the targa loading code from that NeHe tutorial.

If you end up using SDL_Image I'm curious how it turns out.

Aaron

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #5
I'm really interested in this targa format, how do you make a targa image. If this targa stuff is easy enough, I'll just avoid SDL_image altogether.

BTW, I had completely forgot about transparency effects. I just realized that my textures would all be white blocks with the sprite inside of them. So, yeah, I definitly wanna use targa...
Quote this message in a reply
Member
Posts: 233
Joined: 2003.05
Post: #6
You can save images as Targas from most graphics editing programs. Photoshop, of course, is the preferred app for this type of thing.

One way to make smooth edged sprites is to start with a transparent layer and just draw on it. The pen lines are automatically anti-aliased for you that way. It's easier to see if you put a white background layer underneath and then delete it before you save as a Targa.

You want to make sure you save WITH the alpha channel.

You use blending to make the transparency work.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

"Pay no attention to that man behind the curtain." - Wizard of Oz
Quote this message in a reply
KidTsunami
Unregistered
 
Post: #7
Is that the only way to make a texture in openGL have transparency? Like is there a similar thing to the way copy bits has a backround color?

I can't imagine a targa file would be the only way to realistically load sprites into openGL...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
Image format has nothing to do with OpenGL...

If you want transparency, you need an alpha channel. That pretty much limits you to PNG, TGA and TIF.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES Loading images techy 4 3,814 Dec 24, 2009 02:01 PM
Last Post: techy
  OpenGL Point Sprites Talyn 10 13,154 Jan 18, 2009 05:55 PM
Last Post: Talyn
  OpenGL Extension Loading othello 5 5,122 Aug 14, 2008 01:59 PM
Last Post: OneSadCookie
  OpenGL Texture Loading &amp; Sprites corporatenewt 2 11,016 Jan 30, 2008 12:39 PM
Last Post: ynda20
  Loading and using textures with alpha in OpenGL with Cocoa corporatenewt 4 5,918 Dec 8, 2007 02:06 PM
Last Post: Malarkey