Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DirectDraw SetColorKey problems

Posted on 1998-06-15
6
Medium Priority
?
938 Views
Last Modified: 2013-12-03
I recently modified my application to run at 640x480x16. Before, when it was using 8-bit graphics, transparency worked fine. However, now it does not, unless I use 0 as my source color key. Can someone provide some source or at least tell me what the heck is going on? Here's the code I use to change the color key:

bool DirectDrawSurface::SetColorKey(COLORREF color)
{
      DDCOLORKEY colorKey;

      colorKey.dwColorSpaceLowValue=color;
      colorKey.dwColorSpaceHighValue=colorKey.dwColorSpaceLowValue;
      if (Surface->SetColorKey(DDCKEY_SRCBLT,&colorKey)==DD_OK)
            return true;
      else return false;
}
0
Comment
Question by:dwatling
[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
  • 3
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 120 total points
ID: 1407885
You need to convert a RGB color to a pysical color. Check out the DirectX samples\misc\ddutil.h, ddutil.cpp, ddutil2.h, ddutil2.cpp. There are DDSetColorKey function and DDSetColorKey2 function. They use DDColorMatch and DDColorMatch2 functions to convert a RGB color to a pysical color first.

/*
 * DDColorMatch
 *
 * convert a RGB color to a pysical color.
 *
 * we do this by leting GDI SetPixel() do the color matching
 * then we lock the memory and see what it got mapped to.
 */
extern "C" DWORD DDColorMatch2(IDirectDrawSurface3 *pdds, COLORREF rgb)
{
    COLORREF rgbT;
    HDC hdc;
    DWORD dw = CLR_INVALID;
    DDSURFACEDESC ddsd;
    HRESULT hres;

    //
    //  use GDI SetPixel to color match for us
    //
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
      rgbT = GetPixel(hdc, 0, 0);             // save current pixel value
      SetPixel(hdc, 0, 0, rgb);               // set our value
      pdds->ReleaseDC(hdc);
    }

    //
    // now lock the surface so we can read back the converted color
    //
    ddsd.dwSize = sizeof(ddsd);
    while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
      ;

    if (hres == DD_OK)
    {
      dw  = *(DWORD *)ddsd.lpSurface;                     // get DWORD
      dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount)-1;  // mask it to bpp
      pdds->Unlock(NULL);
    }

    //
    //  now put the color that was there back.
    //
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
      SetPixel(hdc, 0, 0, rgbT);
      pdds->ReleaseDC(hdc);
    }

    return dw;
}

/*
 * DDSetColorKey
 *
 * set a color key for a surface, given a RGB.
 * if you pass CLR_INVALID as the color key, the pixel
 * in the upper-left corner will be used.
 */
extern "C" HRESULT DDSetColorKey2(IDirectDrawSurface3 *pdds, COLORREF rgb)
{
    DDCOLORKEY          ddck;

    ddck.dwColorSpaceLowValue  = DDColorMatch2(pdds, rgb);
    ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
    return pdds->SetColorKey(DDCKEY_SRCBLT, &ddck);
}

0
 

Author Comment

by:dwatling
ID: 1407886
Hmmn.. Seems to work only with pure black and pure white, not any color in between (eg. Hot pink). Oh well. Good enough. Thanks for your help.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1407887
Are you sure you specified a correct RGB value?
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

Author Comment

by:dwatling
ID: 1407888
I defined it as "RGB(255,0,192)". The only thing that I can think of is under 16-bit mode, this color doesn't exist, but a close match does and for some reason or another DDColorMatch doesn't get it.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1407889
You can use some graphics utilities to view its color palette and get the exact RGB values.
0
 

Author Comment

by:dwatling
ID: 1407890
I shall try that. Thanks.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

609 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