Solved

using colorkey in C# and Directx 9.0

Posted on 2003-10-25
3
494 Views
Last Modified: 2013-12-08
how to use the ColorKey and also what are the tricks about using the Colorkey ,what i should be careful about the Draw method...or other SurfaceDescription or SurfaceCaps or anything else...
Thank You Very Much!!!
0
Comment
Question by:hk_lok_yu
3 Comments
 

Accepted Solution

by:
mikeant78 earned 50 total points
ID: 9629526
As far as I know DirectX 9 does not support "old-school" color-key in a true sence of the word, that is it does not compare source color values to determine transparency. Instead you have to fake color key through alpha testing (or alpha blending).

So what you need to do is pre-process your textures to incude a correct alpha channel (or at least an alpha bit) when you upload them to video memory.

Then you can enable alpha testing with the D3DRS_ALPHATESTENABLE render state. You should also set the alha test compare function, such as D3DCMP_GREATEREQUAL, and a refernce value. Pixels that do not meet the comparison to the refrence value criteria will be discarded.

Another (and better looking) alternative would be to use alpha blnding, although that may be a  bit more expensive depending on your needs.
0
 
LVL 5

Expert Comment

by:dcgames
ID: 10126824
You don't specify if you are using DirectDraw or Direct3D. It makes a difference.

I am using DirectX 9.0 and DirectDraw.  Under this scenario, if you specify a ColorKey, the color (or colors) described by the colorkey are treated as FULLY TRANSPARENT. The following code creates an off-screen DirectDraw surface ready to "Blit" into a BackBuffer which is then FLIPed onto the primary buffer.

  SurfaceCaps caps = new SurfaceCaps();
  caps.OffScreenPlain = true;
  SurfaceDescription desc = new SurfaceDescription(caps);
  ColorKey clrKey = new ColorKey();
  clrKey.ColorSpaceLowValue = 0;
  clrKey.ColorSpaceHighValue = 0;
  desc.SourceDraw = clrKey;
  surface = new Surface(bitmap,desc,dev);

Notice I set the color key to Low & High to 0. This are INT32 values. A setting of 0/0 means that the PURE BLACK color in the bitmap will be treated as FULLY TRANSPARENT when you blit it onto the destination surface as follows:

   dstSurface.DrawFast(
        dstX,   dstY,
        this.surface,
        srcRectangle,
        DrawFastFlags.Wait | DrawFastFlags.SourceColorKey );

The SourceColorKey flag specifies that the color key of the SOURCE surface (desc.SourceDraw value) be treated as the transparent color. Other combinations are valid.

The question I have been unable to answer is: How do I set the color key to a different color than black!

I tried
    clrKey.ColorSpaceValueLow =
    clrKey.ColorSpaceValueHigh = Color.FromArgb(255,255,133,255).ToArgb();

But it didn't work.

      
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question