XNA Creators Club Online
Page 1 of 1 (13 items)
Sort Posts: Previous Next

transparency key availability in BMP content

Last post 10-14-2008 1:40 PM by jocemig. 12 replies.
  • 08-17-2008 9:31 PM

    transparency key availability in BMP content

    First post in here yay :D

     
    I've got some sprites in the BMP format. I added it to the content and set the color key color to the respective color to be transparent in the BMP file. Now when I draw the sprite, the color won't be transparent. I don't think it's a code thing, so I believe it's cuz of the BMP format. Could that be?
     

    It's important to not modify the sprite sheets itself.

     

    I set the color key color in the properties of the content file to 0;0;0;255. 0;0;0 is the black I want to make transparent. I just don't know what the A parameter, 255 in my case, stands for.

    Just to be sure, this is my code I'm using:

            protected override void LoadContent() 
            { 
                spriteBatch = new SpriteBatch(GraphicsDevice); 
     
                myTexture = Content.Load<Texture2D>("davis_0"); 
            } 
            protected override void Draw(GameTime gameTime) 
            { 
                graphics.GraphicsDevice.Clear(Color.CornflowerBlue); 
     
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend); 
                spriteBatch.Draw(myTexture, spritePosition,new Rectangle(frame*80,0, 80,80), Color.White); 
                spriteBatch.End(); 
     
                base.Draw(gameTime); 
            } 

    Can anyone tell me what I'm doing wrong?

  • 08-18-2008 12:07 PM In reply to

    Re: transparency key availability in BMP content

    I don't see you setting the colour key you want to use. You may have mistaken the third parameter of the spriteBatch object Draw method which is for tinting the sprite. By shading the sprite white, your applying no specific colour to tint resulting in no colour changes. I know the default color key is magneta (hot pink: R255, G:0, B:255) but I allways use the PNG file format for the alpha channel over BMP so I'm unsure how you would go about changing the colour key (if it's possible).
  • 08-18-2008 1:28 PM In reply to

    Re: transparency key availability in BMP content

    Uhm yeah, I thought so that the third parameter of the Draw-method just tints the sprite. I messed around with that value a bit already.

    I set the color in the properties of the content file, so it's not really in the code..my mistake. The specified color is 0;0;0;255 and I know it's black. But it's the color I get when I take the respective color in paint from the BMP file.

    I would take PNGs too, but BMPs are just the way it is, and I don't really wanna change it. The transparency key is imho one of the most basic functions when it comes to spriting. So I guess there has to be a way.

  • 08-18-2008 11:07 PM In reply to

    Re: transparency key availability in BMP content

    Well, I didn't find anything on setting the colour key other than using masks but thats not what you asked for so I whipped up a method that can be used to set the alpha of a specified colour to zero for you:

    // Sets the colour key to be used for a Texture2D object
    protected Texture2D SetColourKey(Texture2D texture, Color colourKey)
    {
         // Make sure the texture has been loaded
         if (texture != null)
         {
             // Get the untouched pixel data
             Color[ pixels = new Color[texture.Height * texture.Width];
             texture.GetData<Color>(pixels);

             // Loop through all rows for the current texture
             for (int i = 0; i < texture.Height; i++)
             {
                 // Iterate through all the columns in the current row
                 for (int j = 0; j < texture.Width; j++)
                 {
                     // Work out what pixel we are working with
                     int offset = ((i * texture.Width) + j);

                     // Set the alpha to zero (transparent) if the colour
                     // of the pixel is the colour we specified as the
                     // colour key
                     if (pixels[offset] == colourKey)
                         pixels[offset] = new Color(0, 0, 0, 0);
                 }
             }

             // Apply the changes to the textures pixel data
             texture.SetData<Color>(pixels);
         }

    // Return the results
    return texture;
    }


    Just call it after loading your texture like so:

    testTexture = Content.Load("test");
    testTexture = SetColourKey(testTexture, Color.Black);


    It work pretty well but it doesn't keep track of the last colour key, so if you set the colour key to black, then later on change the colour key to green, both colours will be used as the colour key. It can serve as a base for you, if you want to take it further you might want to extend the Texture2D class to implement colour keys. Good luck =]

    EDIT: There may be a few typos due to the forums removing certain characters, I think the forum needs a source tag for posting code and keeping it formatted =]

  • 08-18-2008 11:17 PM In reply to

    Re: transparency key availability in BMP content

    The parameter in SpriteBatch.Draw sets the tinting; not the transparent color.

    You also don't need to write your own method to do this; it's already built into the content pipeline (which means it will do it at compile time instead of load-time making your game boot up faster). Simply select the image in your project and view the properties. You'll see that you can set the color to be made transparent as well as a switch to turn that feature on and off:

  • 08-18-2008 11:36 PM In reply to

    Re: transparency key availability in BMP content

    Ah, thanks for pointing that out Nick, I havn't done much work with the content pipeline so I didn't know about being able to set properties. I'm assuming properties differ with each content processor that is used?
  • 08-19-2008 11:33 AM In reply to

    Re: transparency key availability in BMP content

    It doesn't work in my case. As I already wrote before, I tried these properties [Color Key Color R, G, B, and A], but nothing happened. Is the Build Action "Compile" by default? Maybe that's my problem..but I don't really think so. The Enable-property below is set to "True".
  • 08-19-2008 3:24 PM In reply to

    Re: transparency key availability in BMP content

    Hend0re:
    I'm assuming properties differ with each content processor that is used?

    That's correct. Every ContentProcessor has a different set of properties, and every asset can set different values for those properties.

    Eli Tayrien - XNA Framework Developer
  • 08-19-2008 3:26 PM In reply to

    Re: transparency key availability in BMP content

    YoYoCJ:
    It doesn't work in my case. As I already wrote before, I tried these properties [Color Key Color R, G, B, and A], but nothing happened. Is the Build Action "Compile" by default? Maybe that's my problem..but I don't really think so. The Enable-property below is set to "True".

    Sounds like your content project is set up correctly. I'd open up your image in a paint tool that supports alpha, and make absolutely sure that the pixels that you want to color key out are the color you expect, including alpha.

    Eli Tayrien - XNA Framework Developer
  • 08-29-2008 11:36 PM In reply to

    Re: transparency key availability in BMP content

    I can't explain it, but now, after I rebooted my computer, it ran just how it should. I didn't change anything in the code or the program structure, and the specified color won't be drawn.

    So I guess if someone else has the problem that the color key color property doesn't work how it should, he should just try restarting VS or rebooting the computer.

    Thanks for you help anyways.

     

    //EDIT

    Here is my final statement:

    XNA 2.0 does not support transparency via color key color in BMPs, but XNA 3 does!

  • 10-09-2008 4:33 PM In reply to

    Re: transparency key availability in BMP content

    Oh hai!
    Explnation : The VS loaded the bitmap to its memory, or coded it down to into some precomplile file or stored it in cache files or hell knows maybe pushed it to the dehpts of hell  'casue vs too lazy to build the whoole project when u press run  ( ... and its much faster this way ^_^' ). The problem with that , iz  it doesnt realizes u have modified any othere file than game1.cs or whatewhere u call it, sooo u must rebuild it from menu and taddaaa it will work ...
    The strange thing iz when i tryed to modify the properties of color key ... i get an error :

    Warning    1    There was a problem setting the ColorKeyColor property of TextureProcessor to the value "255; 255; 255; 255": Invalid string format. Expected a string in the format "R, G, B, A".    C:\Documents and Settings\Paul\Dokumentumok\Progy\XNA\TEST\Transparency\Transtest\Transtest\Content\RedCircle.bmp    Transtest

     

    .. but it works if i dont modify it ....  man i hate c# -.- ...


  • 10-09-2008 4:39 PM In reply to

    Re: transparency key availability in BMP content

    The error tells you exactly what the problem is. You used semicolons to separate the components and it wants you to use commas.