Solved

using colorkey in C# and Directx 9.0

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
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…
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.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

13 Experts available now in Live!

Get 1:1 Help Now