Maintaining transparency when concatenating transparent pngs

I have a set of png images that have a transparency/alpha channel. i want to
concatenated
these images into an SDL surface then use that surface in my program. I have
already written a small program that does indeed load the png files into
individual surfaces
then adds those surfaces to a final strip surface. The problem im having is
the transparency
channel is not working correctly.

the following code loads the png files:

std::ostringstream os;
int width, height;
width = 128;
height = 128*18;
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

strip = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,rmask, gmask, 

bmask, amask);

if(strip == NULL)
{
    assert("ERROR: Creating a surface failed");
    return false;
}

for(int i = 0; i < 11 ; i++)
{
	os << "./assets/strip" << i+1 << ".png";
	strips[i] =  IMG_Load(os.str().c_str());
	if (!strips[i])
	{
		assert("Surface loading failed!");
		assert(IMG_GetError());
		return false;
	}
	os.str("");
}


SDL_Rect dest,src;
src.x = 0;
src.y = 0;
src.h = 128;
src.w = 128;

dest.x = 0;
dest.h = 128;
dest.w = 128;

SDL_Surface *post;

for(int i = 0; i < 11 ; i++)
{
	post = SDL_DisplayFormatAlpha(strips[i]);
	dest.y = i*128;
	SDL_BlitSurface(post,&src,strip, &dest);
	SDL_FreeSurface(post);
}

the following displays the strip image:

void game::DrawIMG(SDL_Surface *img, int sx, int sy, int dx, int dy, int w,
int h)
{
SDL_Rect dest;
dest.x = dx;
dest.y = dy;

SDL_Rect src;
src.x = sx;
src.y = sy;
src.w = w;
src.h = h;

SDL_BlitSurface(img, &src, screen, &dest);
}

void game::DrawScroll_Vertical(float scroll)
{
int screen_w = 128;
int screen_h = 128*3;

int image_w = 128;
int image_h = 11*128;
//printf("%f %d\n",scroll,image_h-screen_h);

if(scroll>image_h-screen_h)
{
DrawIMG(strip,0,(int)(scroll), 120,60, 128,(int)((image_h)-scroll));
DrawIMG(strip,0,0, 120,(int)(((image_h)-scroll)+60),
128,(int)(screen_h-((image_h)-scroll)));
}
else
{
DrawIMG(strip,0,(int)(scroll), 120,60, screen_w,screen_h);
}
}

// Draw the scene
void game::DrawScene()
{
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen,0,white);
DrawScroll_Vertical(scroll);
SDL_Flip(screen);
}

If i have left any detail out or I need to provide more detailed explanation
of what I am
attempting please let me know.

Thanks
Black_13_________________________________________________________________
The MSN Entertainment Guide to Golden Globes is here. Get all the scoop.
http://tv.msn.com/tv/globes2007/?icid=nctagline2

You have to disable alpha blending (SDL_SetAlpha()) when you’re doing
the “concatenation” blitting, to get the alpha channel data copied
into the destination surface, rather than applied during the blit.

…and, then you need to enable alpha blending on the resulting
surface for the hopefully preserved alpha channel to have any effect.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Wednesday 10 January 2007 19:24, james osburn wrote:

I have a set of png images that have a transparency/alpha channel. i
want to concatenated
these images into an SDL surface then use that surface in my
program. I have
already written a small program that does indeed load the png files
into individual surfaces
then adds those surfaces to a final strip surface. The problem im
having is the transparency
channel is not working correctly.

(includesSDL-announce)"
SDL-announce)"
transparentpngs

I have a set of png images that have a transparency/alpha channel. i
want to concatenated
these images into an SDL surface then use that surface in my
program. I have
already written a small program that does indeed load the png files
into individual surfaces
then adds those surfaces to a final strip surface. The problem im
having is the transparency
channel is not working correctly.

You have to disable alpha blending (SDL_SetAlpha()) when you’re doing
the “concatenation” blitting, to get the alpha channel data copied
into the destination surface, rather than applied during the blit.

…and, then you need to enable alpha blending on the resulting
surface for the hopefully preserved alpha channel to have any effect.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --’


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

here is what i came up with:
void game::DrawScroll_Vertical(float scroll)
{
int screen_w = 128;
int screen_h = 128*3;

int image_w = 128;
int image_h = 11*128;
if(scroll>image_h-screen_h)
{
	DrawIMG(strip,0,(int)(scroll), 120,60, 128,(int)((image_h)-scroll));
	DrawIMG(strip,0,0, 120,(int)(((image_h)-scroll)+60), 

128,(int)(screen_h-((image_h)-scroll)));
}
else
{
DrawIMG(strip,0,(int)(scroll), 120,60, screen_w,screen_h);
}
}

// Draw the scene
void game::DrawScene()
{

DrawIMG(back,0,0);
//!!!!!!!!!!!!!!!!!!!!!
SDL_SetAlpha(strip, SDL_SRCALPHA, 255);
SDL_BlitSurface(strip, NULL, screen, NULL);

SDL_Flip(screen);

}

bool game::initialize()
{

// Initalize the SDL Video, Audio and Joystick subsystems.
if(SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
{
	printf("Unable to init SDL: %s\n", SDL_GetError());
	return false;
}
atexit(SDL_Quit);

// Now we get ourself a 640x480x32 fullscreen surface...
screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE/*|SDL_FULLSCREEN*/);
if ( screen == NULL )
{
	printf("Unable to set 800x600 video: %s\n", SDL_GetError());
	false;
}

back    = ImageLoad("assets/gnu_tux.bmp");


std::ostringstream os;


int width, height;
width = 128;
height = 128;


Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

strip = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,rmask, gmask, 

bmask, amask);

if(strip == NULL)
{
    assert("ERROR: Creating a surface failed");
    return false;
}

symbol =  IMG_Load("./assets/strip1.png");
SDL_SetAlpha(symbol, 0, 255);

SDL_BlitSurface(symbol,NULL,strip,NULL);

sdlgt=0;
dt=0;
                     // the previous frame
gameover=0;          // Is the game over?
td=0;
td2=0;          // Used when checking the time difference
                     // (how much time has passed since last frame)

paused=0;             // Is the game paused or not

scroll=0;           // How much has the screen scrolled

return true;

}

void game::DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}>From: David Olofson

Reply-To: "A list for developers using the SDL library.
To: "A list for developers using the SDL library. (includes
Subject: Re: [SDL] Maintaining transparency when concatenating
Date: Wed, 10 Jan 2007 22:01:28 +0100
On Wednesday 10 January 2007 19:24, james osburn wrote:


Dave vs. Carl: The Insignificant Championship Series. ?Who will win?
http://clk.atdmt.com/MSN/go/msnnkwsp0070000001msn/direct/01/?href=http://davevscarl.spaces.live.com/?icid=T001MSN38C07001