Solved

using colorkey in C# and Directx 9.0

Posted on 2003-10-25
3
489 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now