Sign in to follow this  

Need help spotting an error =)

This topic is 4733 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

I was doing the programs from GameTutorials.com and one of them taught you how to write a TransparentBlit function. Fair enough, I read it, understood it (I think) and wrote my own based on theirs. It didn't work at first, so I went through and change some things, it still doens't work. I even tried changing BYTE to unsinged char but I think they are the same anyway... Here is mine
void cpp::TransparentBlit(HDC dest_dc, int xPos, int yPos, int img_w, int img_h, 
	 					 		  	  HDC src_dc, int xStart, int yStart, UINT transparency)
{
    BYTE *img_bits;
    BYTE *back_bits;
    BITMAPINFO bmp_info = {0};
    HBITMAP img_bmp, img2_bmp, old_bmp, old2_bmp;
    HDC cmp_dc, cmp2_dc;
    
    bmp_info.bmiHeader.biSize 		 = sizeof(BITMAPINFOHEADER);
    bmp_info.bmiHeader.biCompression = BI_RGB;
    bmp_info.bmiHeader.biHeight		 = img_h;
    bmp_info.bmiHeader.biWidth		 = img_w;
    bmp_info.bmiHeader.biBitCount    = 24;
    bmp_info.bmiHeader.biClrUsed	 = 0;
    bmp_info.bmiHeader.biPlanes 	 = 1;
    
    img_bmp  = CreateDIBSection(dest_dc, &bmp_info, DIB_RGB_COLORS, (void**)&img_bits, 0, 0);
    img2_bmp = CreateDIBSection(dest_dc, &bmp_info, DIB_RGB_COLORS, (void**)&back_bits, 0, 0);
    
    cmp_dc  = CreateCompatibleDC(dest_dc);
    old_bmp = (HBITMAP)SelectObject(cmp_dc, img_bmp);
    
    cmp2_dc  = CreateCompatibleDC(src_dc);
    old2_bmp = (HBITMAP)SelectObject(cmp2_dc, img2_bmp);
    
    BitBlt(cmp_dc, 0, 0, img_w, img_h, dest_dc, xPos, yPos, SRCCOPY);
    BitBlt(cmp2_dc, 0, 0, img_w, img_h, src_dc, xPos, yPos, SRCCOPY);
    
    for (int i = 0; i < img_w * img_h * 3; i += 3)
    {
	 	if ((back_bits[i]     != GetBValue(transparency)) ||
            (back_bits[i + 1] != GetGValue(transparency)) ||
            (back_bits[i + 2] != GetRValue(transparency)))
       {
	   		img_bits[i]     = back_bits[i];
	   		img_bits[i + 1] = back_bits[i + 1];
	   		img_bits[i + 2] = back_bits[i + 2];
       }
    }
    
    BitBlt(dest_dc, xPos, yPos, img_w, img_h, cmp_dc, 0, 0, SRCCOPY);
    
    SelectObject(cmp_dc, old_bmp);
    SelectObject(cmp2_dc, old2_bmp);
    
    DeleteObject(img_bmp);
    DeleteObject(img2_bmp);
    
    DeleteDC(cmp_dc);
    DeleteDC(cmp2_dc);
}
GT's
void TransparentBlit(HDC hdc, int destX, int destY, int destWidth, int destHeight, 
					 HDC hdc2, int srcX, int srcY, UINT tranparency)
{
	unsigned char* pImageBits;
	unsigned char* pBackBits;
	BITMAPINFO bmBitmapInfo = {0};
	HBITMAP hBitmap, hBitmap2, hOldBitmap, hOldBitmap2;
	HDC compHDC;
	HDC compHDC2;
	
	// Fill in our BitmapInfo structure (we want a 24 bit image)
	bmBitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmBitmapInfo.bmiHeader.biCompression = BI_RGB;
	bmBitmapInfo.bmiHeader.biHeight = destHeight;
	bmBitmapInfo.bmiHeader.biWidth = destWidth;	
	bmBitmapInfo.bmiHeader.biBitCount = 24;
	bmBitmapInfo.bmiHeader.biClrUsed = 0;
	bmBitmapInfo.bmiHeader.biPlanes = 1;

	// Create 2 DIB Sections.  One for the Front Buffer and one for the BackBuffer
	hBitmap  = CreateDIBSection(hdc,  &bmBitmapInfo, DIB_RGB_COLORS,(void **)&pImageBits, 0,0);	
	hBitmap2  = CreateDIBSection(hdc,  &bmBitmapInfo, DIB_RGB_COLORS,(void **)&pBackBits, 0,0);

	// Create a compatible DC for the front buffer and Select our Dib Section into it
	compHDC = CreateCompatibleDC(hdc);
	hOldBitmap = (HBITMAP)SelectObject(compHDC, hBitmap);
	
	// Create a compatible DC for the back buffer and Select our Dib Section into it
	compHDC2 = CreateCompatibleDC(hdc2);
	hOldBitmap2 = (HBITMAP)SelectObject(compHDC2, hBitmap2);

	// Blit the front buffer to our compatible DC that will hold the destination bits
	BitBlt(compHDC, 0, 0, destWidth, destHeight, hdc, destX, destY, SRCCOPY);

	// Blit the back buffer to our compatible DC that will hold the source bits
	BitBlt(compHDC2, 0, 0, destWidth, destHeight, hdc2, srcX, srcY, SRCCOPY);

	// Loop through the 24 bit image (Times 3 for R G and B)
	for(int i = 0; i < destHeight * destWidth * 3; i += 3)
	{
		// Check if the current pixel being examined isn't the transparent color
		// Remember, the image bits are stored (Blue, Green, Red), not (Red, Green, Blue)
		// We use the system macros to abstract the R G B data
		if((pBackBits[i]   != GetBValue(tranparency)) || 
		   (pBackBits[i+1] != GetGValue(tranparency)) || 
		   (pBackBits[i+2] != GetRValue(tranparency)))
		{
			// Assign the desired pixel to the foreground
			pImageBits[i]     = pBackBits[i];
			pImageBits[i + 1] = pBackBits[i + 1];
			pImageBits[i + 2] = pBackBits[i + 2];
		}
	}  

	
	// Blit the transparent image to the front buffer (Voila!)
	BitBlt(hdc, destX, destY, destWidth, destHeight, compHDC, 0, 0, SRCCOPY);

	// Cleanup the monochrome bitmaps
	SelectObject(compHDC, hOldBitmap);
	SelectObject(compHDC2, hOldBitmap2);	

	// Free GDI Resources
	DeleteObject(hBitmap);
	DeleteObject(hBitmap2);
	DeleteDC(compHDC);
	DeleteDC(compHDC2);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
After a quick scan of both codes i have spotted the filling that might be causing the problem

your code:
BitBlt(cmp_dc, 0, 0, img_w, img_h, dest_dc, xPos, yPos, SRCCOPY);
BitBlt(cmp2_dc, 0, 0, img_w, img_h, src_dc, xPos, yPos, SRCCOPY);

GT code:
// Blit the front buffer to our compatible DC that will hold the destination bits
BitBlt(compHDC, 0, 0, destWidth, destHeight, hdc, destX, destY, SRCCOPY);

// Blit the back buffer to our compatible DC that will hold the source bits
BitBlt(compHDC2, 0, 0, destWidth, destHeight, hdc2, srcX, srcY, SRCCOPY);

The difference i can see is that you have your 2nd bitblt with the same xy pos's and the gt 2nd bitblt has a xy src.

Just a thought, that this could be the problem. If i am wrong then I am sorry to point this out.

Share this post


Link to post
Share on other sites
Thank you, you are right, and it makes complete sense because I would be trying to copy stuff where tehre probably was nothing. =/

Thanks again pal =)

Share this post


Link to post
Share on other sites

This topic is 4733 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this