?
Solved

DirectDraw SetColorKey problems

Posted on 1998-06-15
6
Medium Priority
?
923 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

741 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