TextureLoadingColorKey

Introduction

The GraphicsOptions' TextureLoadingColorKey allows you to set the color to ignore when loading .bmp graphics. The TextureLoadingColorKey value is only applied to .bmp images and not to other images. The reason for this is because other image formats either:

  • Have lossy compression so exact colors are not preserved and color keys are not an effective way to create transparency.

  • Include an alpha channel which include the ability to have partial transparency.

Code Example

bool ifUsingColorKey = true;

if (ifUsingColorKey)
{
    // Set the color key to Magenta:
    FlatRedBallServices.GraphicsOptions.TextureLoadingColorKey =
        Microsoft.Xna.Framework.Graphics.Color.Magenta;
}

// Create a Sprite
SpriteManager.AddSprite("redballWithMagenta.bmp");

ColorKey and Texture caching

You may be wondering why the above tutorial requires a recompile and a change of the bool instead of simply creating two Sprites with a change in the TextureLoadingColorKey between the creation of the two Sprites. The reason for this is because FlatRedBallServices caches. Therefore an actual load is only performed the first time that the redball.bmp graphic is referenced. Every subsequent time, the texture reference to the cached texture data is returned. The exception to this is if different content managers are used.

Last updated