Changing the Transparent Color?

Hi,

AgateLib seems really cool. That said, I have a little question. When making sprites, I like to use magenta (programmer's pink) as the transparent color. I can't find how to set this as the transparent color when blitting.

Thanks in advance!
Cae

kanato
Posted - Tue, 04/14/2009 - 21:49

AgateLib doesn't have the concept of a color key that you see in an API like DirectDraw. This is mainly because no such concept exists in Direct3D or OpenGL. The best thing to do is to save your images in a format with an alpha channel, like png and use real transparency. This also has the advantage of allowing partial transparency.

What you can do is load the file in AgateLib and replace the your transparent color with a color with alpha transparency:

  1. Surface mysurface = new Surface("somefile.bmp");
  2.  
  3. PixelBuffer pixels = mysurface.ReadPixels();
  4.  
  5. // replace the magenta color with transparent black
  6. pixels.ReplaceColor(Color.FromArgb(255,0,255), Color.FromArgb(0,0,0,0));
  7.  
  8. mysurface.WritePixels(pixels);

Do that once when you first load the surface and areas intended to be transparent will be so when drawn. It will slow down loading somewhat, so if reasonable you should do the conversion manually ahead of time so the conversion doesn't have to be done each time images are loaded.

caeonosphere
Posted - Wed, 04/15/2009 - 07:38

Thanks so much for the prompt reply. Works great!

Tom