Solved

using colorkey in C# and Directx 9.0

Posted on 2003-10-25
3
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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