Solved

DirectDraw SetColorKey problems

Posted on 1998-06-15
6
849 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
  • 3
  • 3
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 30 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video discusses moving either the default database or any database to a new volume.

758 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

21 Experts available now in Live!

Get 1:1 Help Now