Jump to content

  • Log In with Google      Sign In   
  • Create Account

Problem with Alpha blitting in SDL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 randomZ   Members   -  Reputation: 163

Like
Likes
Like

Posted 04 October 2003 - 05:23 AM

Hi, I''m trying to create a text rendering system in SDL (C++, mingw, Dev-C++) the following way: - I have a character map which contains all characters I need - I have a font definition file which contains the coordinates of the characters within the map 1. I (obviously) load the character map (which is in a PNG with alpha channel) into an SDL_Surface this way:
  SDL_Surface* surface1 = IMG_Load(filename);
  SDL_Surface* fontImage = SDL_DisplayFormatAlpha(surface1);
  SDL_FreeSurface(surface1);
 
When I blit this to the screen, it looks perfect, with correct transparencies and everything. 2. I have the function renderText():
  SDL_Surface* surface1 = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32,
    0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
  SDL_Surface* target = SDL_DisplayFormat(surface1);
  SDL_FreeSurface(surface1);

  //For every character...
    SDL_BlitSurface(fontImage, &srcrct, target, &dstrct);

  return target;
 
The rectangles in the blit command are correct, I''ve checked that. This is where things get messed up. When I use SDL_DisplayFormat() as in the example, I don''t get an alpha channel, but my text is rendered white on black. If I use SDL_DisplayFormatAlpha(), nothing is rendered at all! What could be the cause? --- Just trying to be helpful. Sebastian Beschke Just some student from Germany http://mitglied.lycos.de/xplosiff

Sponsor:

#2 Sabbac   Members   -  Reputation: 122

Like
Likes
Like

Posted 04 October 2003 - 09:56 AM

I ran into the same problem.

When you blit RGBA->RBGA the destinations alpha channel doesn''t change. It acts like a mask. If all the alphas are zero, then the mask is all zero and nothing gets blitted.

I ended up writinga function to copy from one surface to another that copies the alpha with it (reguardless of the destinations alpha).

Here is the code. It makes a lot of assumptions because it is specialized to what I needed. For example it will only write starting at 0,0 of the dest, assumes 32bits. I know that it fails under some situations so beware

Oh yeah, If you find a better way would you toss it my way also?


void br_blit(SDL_Surface* src, SDL_Rect sr, SDL_Surface* dst)
{
// always copies to 0, 0 of the dest

int numPixelsOnLine = sr.w;
int bytesToMove = numPixelsOnLine * 4;
unsigned char* srcPixels = static_cast<unsigned char*>(src->pixels);
unsigned char* dstPixels = static_cast<unsigned char*>(dst->pixels);

for (int y=0; y<sr.h; y++)
{
unsigned char* destP = &dstPixels[y*bytesToMove];
unsigned char* srcP = &srcPixels[((sr.y+y)*src->w*4)+sr.x*4];
memcpy(destP, srcP, bytesToMove);
}
}


#3 randomZ   Members   -  Reputation: 163

Like
Likes
Like

Posted 09 October 2003 - 08:01 AM

Hi,

I've asked this question on the SDL mailing list and got the following:

- This info from Sean Ridenour:

What happens in professional compositing
programs when using the standard over operation is this:
1. If the foreground image is premultiplied, go to step 3
2. Multiply the foreground image against its own alpha channel
3. Invert the foreground image's alpha channel
4. Multiply the background image (all channels) by the foreground image's newly inverted alpha channel.
5. Revert the foreground image's alpha channel to its original state
6. Add the foreground image to the background image (all channels)

- The pygame source code contains an alpha blit function.
clicky
(Thanks Pete Shinners)

EDIT: Sabbac: I've tried to send you an email, but it didn't work

---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff

[edited by - randomZ on October 9, 2003 3:10:07 PM]

#4 Sabbac   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 October 2003 - 08:49 AM

Thanks for the info.

And thanks for reminding me to update that stale email address

#5 randomZ   Members   -  Reputation: 163

Like
Likes
Like

Posted 09 October 2003 - 09:04 AM

No prob


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS