Beginner in Game Development: bitmap graphics 4

Source: Internet
Author: User
Color Key

When the Color Key copies a bitmap to another bitmap, all pixels are not displayed. For example, when you copy an genie (the objects in the game are generally called Genie) to a map (on the background), this genie bitmap is generally not a Sprite-shaped bitmap, it is usually a rectangle bitmap, which contains the genie you need (unless your genie is a rectangle robot ):

In the game, the map is displayed before the genie, so when the genie reaches the tree, there should be a corresponding blocked part, which will not be discussed first. Let's talk about it in the next section. Now, what's more important to us is that if the color key is not applied, this genie will always carry this black background box, which is absolutely intolerable.

To solve this problem, we use the source Color Key. This source Color Key tells you which colors of the sprite rectangle will not be copied (of course we will not copy the black one ). A Color Key consists of two values: A low color value and a high color value. When a Color Key is applied for use, the color between two values, including the two values, will not be copied. There is a structure in DirectX for processing it, called ddcolorkey. Let's take a look:

typedef struct _DDCOLORKEY{
DWORD dwColorSpaceLowValue;
DWORD dwColorSpaceHighValue;
} DDCOLORKEY, FAR* LPDDCOLORKEY;

I will not explain the simple structure. I will show you the effect after the Color Key is used. I use the high and low values of the Color Key to only include black in between them. Therefore, black is the only color that will not be copied.

Much better, isn't it? This is what we want! Now, before I tell you how to create and use a color key, I will talk about the target Color Key, although we do not often use it (we usually use the source Color Key ). Given that the source Color Key defines which color keys cannot be copied, the target Color Key defines which colors cannot be written (overwritten ). It sounds weird, isn't it? I feel the same way. For example, you will understand. If you want to copy a bitmap to B, it means to use a bitmap as the background. For example, for some reason, you need to copy a text box to an empty post-buffer, then copy the background image to the back buffer, but you cannot overwrite the previous text box. Therefore, only the black parts of the text box can be written into pixels in the Post-buffer.

I don't know when you need to deal with this situation, but you may actually use it (once you use it, you must tell me that I have never encountered this situation before ^_^ ). Now you know what color keys are. Let's see how to use them!

  Set Color Key

There are two ways to use the Color Key in DirectDraw. First, you can link a Color Key (or two, if you use both the source and target color keys) to the surface, and then define ddblt_keysrc, ddblt_keydest, ddbltfast_srccolorkey, or ddbltfast_destcolorkey when the bit is in, the specific Identifier is determined by which bit transfer function you use and which color key you use. Second, you can create a Color Key and transmit it to the bit block through the ddbltfx structure. When you constantly need to use the color key, I recommend the first method. Otherwise, if you accidentally want to use the Color Key, use the second method!

You can link the Color Key to a surface that has been created, or create a Color Key while creating the surface. I will detail both methods. Assume that you are working in 16-bit display mode and are in 565 pixel format. You need to use a source Color Key that only contains black in the back buffer. If your post-buffer has been created, you can simply create a ddcolorkey structure and pass it to the idirectdrawsurface7: setcolorkey () function, as shown below:

HRESULT SetColorKey(
  DWORD dwFlags,
  LPDDCOLORKEY lpDDColorKey
);

Remember to use the failed () macro to check the return value of this function to ensure that everything is in the plan. Function parameters are simple:

※Dword dwflags: Specifies the Color Key type used. The following three items will be used:

· Ddckey_colorspace: This structure contains a color range. If the structure contains a separate Color Key, this parameter is not set.

· Ddckey_destblt: This structure specifies the Color Key or color range as the target Color Key for bit block transfer operations.

· Ddckey_srcblt: Specifies the Color Key or color range as the color key used for overwriting.

※Lpddcolorkey lpddcolorkey: This is a pointer to the ddcolorkey structure.

That's all. You can define the bit block transfer flag according to the color key you need. Note that a Color Key is linked to the surface and does not mean that you must use it every time. If you only define the DDBLT-WAIT or ddbltfast_wait flag, the color key will be ignored. The following describes how to set the color key:

DDCOLORKEY ckey;
ckey.dwColorSpaceLowValue = RGB_16BIT565(0, 0, 0); // or we could just say '0'
ckey.dwColorSpaceHighValue = RGB_16BIT565(0, 0, 0);
if (FAILED(lpddsBack->SetColorKey(DDCKEY_SRCBLT, &ckey)))
{
  // error-handling code here
}

If you want to link a surface with a created Color Key, there are several things you need to do. First, when you define a valid member of the ddsurfacedesc2 structure, you need to make the dwflags member contain the ddsd_cksrcblt or ddsd_ckdestblt flag. The specific Identifier depends on which color key you want to use. Let's look back at the ddsurfacedesc2 structure, which contains two ddcolorkey structures. One is ddcdcksrcblt and the other is ddcdckdestblt. Fill in the appropriate structure to create the surface. You need to do so much! The following is an example code about the 640x480 off-screen surface:

// set up surface description structure
INIT_DXSTRUCT(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT;
ddsd.dwWidth = 640; // width of the surface
ddsd.dwHeight = 480; // and its height
ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = RGB_16BIT(0,0,0); // color key low value
ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = RGB_16BIT(0,0,0); // color key high value
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; // type of surface

// now create the surface
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsBack, NULL)))
{
// error-handling code here
}

The section about the color key ends here. Now we can proceed to the last part of this chapter-cut.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.