SDL Alpha Help

Started by
7 comments, last by kev000 16 years, 10 months ago
I decided this would best be placed in a separate thread. Can anyone give me some quick example code and an image that I can load using SDL_Image showing how to use the alpha in a png image. Thanks.
Advertisement
I believe that you set the alpha value IN the png, and using a lib like SDL_Image it should just automatically use the transparent value...


Basically PNGs have alpha channels, and it is my belief that if you use SDL_Image it should automatically load and use the alpha channel accordingly... test it out :)

So for whatever program you are using to create your pngs, look up how-to save alpha channels.
AfroFire | Brin"The only thing that interferes with my learning is my education."-Albert Einstein
thanks for the reply. does any have a correctly alpha-ized (as it where) png that I can test with. That would be a great help.

Uh, they aren't too tough to make. I suggest using Paint.NET for it. Just make a new PNG, select it's background, press delete then draw a line or something on it. Save, then load the file in your program. Viola! It'll be a line on a transparent background.
Guys, I'm desperate. I'm using ubuntu feisty to compile. I think it might be a bug in sdl_image or libpng12. The alpha values on the png's won't load! I'm sure the image's alpha work (the transparencies show up in gimp and a number of other programs).

Is there anyone that can confirm that sdl_image doesn't load the transparencies under ubuntu?

thanks



Quote:Original post by kev000
Guys, I'm desperate. I'm using ubuntu feisty to compile. I think it might be a bug in sdl_image or libpng12. The alpha values on the png's won't load! I'm sure the image's alpha work (the transparencies show up in gimp and a number of other programs).

Is there anyone that can confirm that sdl_image doesn't load the transparencies under ubuntu?

thanks


I don't have any experience with ubuntu, but I don't see why it wouldn't work.

It might be an idea to show us some code. Are you checking that the image has actually loaded? What do you do with the image after loading to blit it to the screen?

(And just a guess without seeing any code, but what happens if you use SDL_SetAlpha(surface,0,0); where surface is the SDL_Surface * that you loaded, before you blit it.)
The SDL_SetAlpha(tempsurf,0,0); does nothing. What is that function supposed to do anyway?

Here is some relevant code:

From the buttonclass::create():

SDL_Surface* loadedImage = NULL, *tempsurf = NULL;
loadedImage = IMG_Load( srcfile.c_str() );
if( loadedImage != NULL )
{
tempsurf=SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
else
{
sdlnerror::errorCreatingButton( srcfile.c_str() );
return -1;
}

Then tempsurf is passed into surfaceClass:

mysurface.init(tempsurf,area,0);
void surfaceClass::init(SDL_Surface *s, SDL_Rect a, int nalpha)
{
myarea = a;
if(s==NULL)
{
//sdlnerror::errorLoading( "Error loading surface, blitting disabled." );
visibility=0;
mysurface=NULL;
}
else
{
mysurface = s;
myarea.w = mysurface->w;
myarea.h = mysurface->h;
}
alpha=nalpha;
visibility=1;
}

The display function from surfaceClass::displayMe():

int surfaceClass::displayMe( SDL_Surface *target)
{
if(visibility)
{
if(mysurface != NULL)
SDL_BlitSurface(mysurface,NULL,target,&myarea);
//else nerror::out("surface is null cannot blit");
}
//ndebug::out("surface is not visible. not displaying",3);
//mysurface is the image surface, and target is the screen.
}

Am I doing anything that would make the png's alpha values nullified?
Use SDL_DisplayFormatAlpha(), not SDL_DisplayFormat().

Hope this helps.
bless you let_bound, you have made my day. >:D<

thanks,

This topic is closed to new replies.

Advertisement