Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Swatter555

Direct Draw Source Color Keying

This topic is 5478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Color keying one color works fine, yet I cant seem to make a range of colors transparent. Ill list the function that creates the surface and sets the color key, then I will list the part of the code that makes the call. Thanks in advance.

LPDIRECTDRAWSURFACE7 LoadBmpOffSurface32(LPDIRECTDRAWSURFACE7 lpddsoff, int lred, int lgreen,int lblue,
          int hred, int hgreen,int hblue,
	  const char* bmp_file_path)
										
{
		
	//Locals

	BITMAP               bitmap24;               
        HBITMAP              hbitmap24;
	DDSURFACEDESC2       ddsd;
	
	
	//clear out ddsd

	DDRAW_INIT_STRUCT(ddsd);
    

    // load the bitmap

    hbitmap24 = (HBITMAP)LoadImage(NULL,bmp_file_path,IMAGE_BITMAP,
                                 0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);

    // return if we failed to load the bitmap

    if (FAILED(hbitmap24))
	{
        //If failure, Show MB

	    MessageBox(main_window_handle,
	    "FATAL ERROR:UNABLE TO LOAD BITMAP",
		"DirectX Error Message",
		 MB_OK);
         return(0);
	} //endif


    
	
	// get bitmap dimensions

    GetObject(hbitmap24,sizeof(BITMAP),&bitmap24);
    int surf_width =bitmap24.bmWidth;
    int surf_height=bitmap24.bmHeight;
	
    
	
	// create surface//////////////////////////

	///////////////////////////////////////////

    HRESULT ddrval;
    
    ZeroMemory(&ddsd,sizeof(ddsd));
    ddsd.dwSize = sizeof(DDSURFACEDESC2);
    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT |   DDSD_CKSRCBLT;
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
    ddsd.dwWidth = surf_width;
    ddsd.dwHeight = surf_height;
	
		    
	
    // attempt to create surface

    ddrval=lpdd->CreateSurface(&ddsd,&lpddsoff,NULL);

    
    // created ok?

    if (ddrval!=DD_OK) 
	{
        //If failure, Show MB

	    MessageBox(main_window_handle,
	    "FATAL ERROR:UNABLE TO CREATE OFFSCREEN SURFACE",
		"DirectX Error Message",
		 MB_OK);
         lpddsoff= NULL;
	} //end if

	
	
	else {

        				
	//get a DC for the surface

        HDC        hdc;
        lpddsoff->GetDC(&hdc);

        
	// generate a compatible DC

        HDC bit_dc=CreateCompatibleDC(hdc);

        
	// blit the interface to the surface

       SelectObject(bit_dc,hbitmap24);
       BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);

       
	// release the DCs

        lpddsoff->ReleaseDC(hdc);
        DeleteDC(bit_dc);

           
		
  // set up the color key

  DDCOLORKEY key;                
  key.dwColorSpaceLowValue = _RGB32BIT (255,lred,lgreen,lblue);
  key.dwColorSpaceHighValue= _RGB32BIT(255,hred,hgreen,hblue);

  lpddsoff->SetColorKey(DDCKEY_SRCBLT | DDCKEY_COLORSPACE,&key); 

      

    } //end else


    
    // unload bitmap

    DeleteObject(hbitmap24);


	
	return(lpddsoff); //return the surface


//End That Function


//Here is where I call the function:



       bmp_file_name = "graphics/tiles/CBORDER1.bmp";

        //loads bmp onto surface

	lpddsCBORDER1 = LoadBmpOffSurface32(lpddsCBORDER1,
			200,200,200,
                        255,255,255,
			bmp_file_name);

[edited by - Swatter555 on September 24, 2003 4:45:27 AM]

Share this post


Link to post
Share on other sites
Advertisement
Thats the first time I havent recieved a reply from the great people here. Could it be an error in another part of the code? I would greatly appreciate any help

Share this post


Link to post
Share on other sites
To make a range of colors transparent, just make your low values and high values different.

For example, to make the higher range of saturated green transparent, use a low value of 0,150,0 and a high value of 0,255,0.

Share this post


Link to post
Share on other sites
Thanks for the reply

Ya, I have the high value set to 255,255,255 and the low value set to 200,200,200. I am trying to get rid of all white and shades of grey. That is what you meant, right?

The alpha bit is automatically set to 255 in both cases, since I am using a 32 bit display.

I did catch your meaning right, didnt I?

Share this post


Link to post
Share on other sites
LOL, its in the above source code

I have a good book about direct draw, but it doesnt cover range color keying well. I am missing something, or I am screwing up some part of the process.

Share this post


Link to post
Share on other sites
Keep in mind that the colors between the low and hi range need to be exact.

*If you want to not show any grey, you need something different.( Since Grey is 1,1,1 2,2,2, 3,3,3 etc until white(255,255,255))

so, if your color low is 0,240,0 and your color high is 0,255,0 the color 1,250,0 will still show, while the color 0,250,0 will be transparent.

Understand?

Share this post


Link to post
Share on other sites
Ya, I do believe I understand the principal. I even checked the color ranges in my paint program, the unwanted colors were between 200,200,200 and 255,255,255.

Also, when I added the following flag, it stopped working period:

lpddsoff->SetColorKey(DDCKEY_SRCBLT | DDCKEY_COLORSPACE,&key);

I am steaking of the DDCKEY_COLORSPACE flag. I believe it tells the compiler to look for a range value.

I really appreciate the help and Im not trying to trivialize your assistance, maybe we arent on the same radio station. I can be very obtuse sometimes

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!