[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

using colorkey in C# and Directx 9.0

Posted on 2003-10-25
3
Medium Priority
?
505 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 100 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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