Thread: Transparency

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Transparency

    Hey, I was just wondering, 'cuz I only know of 2 ways of doing transparent bitmaps: BitBlt() using a mask, or some other crazy, confusing, and buggy stuff. So anyways, i really REALLY don't wanna use the crazy buggy way (because that's just bad), but it's gonna be hell trying to make a mask...

    So anyways, does anybody know of a way to do transparency without a mask, or without having to directly test each image bit?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Copy the background to a temporary dc, with the last param of BitBlt set to SRCCOPY. BitBlt the bitmap onto the background, again with SRCCOPY. Finally, copy the temp dc to the background with SRCAND.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Smile

    Thanks, I'll try that
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    that won't work exactly unless the background already has "white" on the opaque spots. Plus the source bitmap must have "black" on the transparent spots. But you've probably found that out by now

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    nop, i haven't found out yet I was busy playing counter-strike. But the background has to have white on the opaque spots?... that kinda sucks. Oh well, thanks anyways. I guess I'll just have to start drawing masks...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    you could programatically generate the masks if you wanted to. run through the DIB and turn your transparent color into black while turning everything else into white.

    Plus, the methodology that we've said so far isn't quite right...

    black opaque area on the mask...
    SRCINVERT to background turns opaque area black

    white opaque area on the mask....
    SRCINVERT to source to turn background black

    SRCINVERT from source to background will be transparent.

    SRCINVERT is your friend, actually its just an XOR operation. I think that should work.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    hmm, I probably did something wrong, but it didn't work... it gave me an illegal operation. And then, whenever 2 *transparent* images crossed paths, they looked funny. This is my code, it's the best I could do...
    Code:
    void TransBlt(HDC hDC, int x, int y, int width, int height, HDC hDC2, int srcX, int srcY)
    {
    	unsigned char* imageBits;
    
    	HDC compDC; // this is for the mask
    	HDC compDC2; //don't feel like muckin up the HDC passed to this, so make another one
    	HBITMAP bitmap, oldBitmap, bitmap2, oldBitmap2;
    
    	compDC = CreateCompatibleDC(hDC2); //dunno, should this be compatible with hDC instead?
    	compDC2 = CreateCompatibleDC(hDC2);
    
    	BITMAPINFO bi;
    	memset(&bi, 0, sizeof(bi));
    
    	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    	bi.bmiHeader.biBitCount = 24;
    	bi.bmiHeader.biClrUsed = 0;
    	bi.bmiHeader.biWidth = width;
    	bi.bmiHeader.biHeight = height;
    	bi.bmiHeader.biCompression = BI_RGB;
    	bi.bmiHeader.biPlanes = 1;
    
    	bitmap = CreateDIBSection(hDC2, &bi, DIB_RGB_COLORS, (void**)&imageBits, 0, 0);
    	oldBitmap = (HBITMAP)SelectObject(compDC, bitmap);
    	bitmap2 = CreateCompatibleBitmap(hDC2, width, height);
    	oldBitmap2 = (HBITMAP)SelectObject(compDC, bitmap2);
    
    	BitBlt(compDC, 0, 0, width, height, hDC2, srcX, srcY, SRCCOPY);
    	
    	for(int i = 0; i < width * height * 3; i += 3)
    	{
    		if(imageBits[i] != 0  || imageBits[i + 1] != 0 || imageBits[i + 2] != 0)
    		{     //If it's not black, make it white
    			imageBits[i] = 255;
    			imageBits[i + 1] = 255;
    			imageBits[i + 2] = 255;
    		}
    	}
    
    	BitBlt(hDC, x, y, width, height, compDC, srcX, srcY, SRCINVERT); //blit the mask to the destination
    	BitBlt(compDC2, 0, 0, width, height, hDC2, srcX, srcY, SRCCOPY); //copy the image to hDC2
    	BitBlt(compDC2, 0, 0, width, height, compDC, srcX, srcY, SRCINVERT); //blit the mask to hDC2
    	BitBlt(hDC, x, y, width, height, compDC2, srcX, srcY, SRCINVERT); //blit hDC2 to the destination
    
    	SelectObject(compDC, oldBitmap);
    	SelectObject(compDC2, oldBitmap2);
    	DeleteObject(bitmap2);
    	DeleteObject(bitmap);
    	DeleteDC(compDC);
    	DeleteDC(compDC2);
    }
    Could someone point out what's wrong? (well, if everything's wrong, don't because that would take a long time ).
    Last edited by Hunter2; 07-25-2002 at 04:00 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    have you tried blitting the incremental steps? it would help to blit the mask to the screen to see if it came out ok. Then try blitting the results of each step to the screen to see if they are what you expect. Figure out where it went wrong first
    always looking, make an offer. get me out of this place.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    I think I steered you wrong... let's modify the original

    black opaque area on the mask...
    SRCAND to background turns opaque area black

    white opaque area on the mask....
    SRCAND to source to turn background black

    SRCINVERT from source to background will be transparent.


    sorry about that. SRCAND with white gives you what you had. SRCAND with black gives you black
    always looking, make an offer. get me out of this place.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Humm... it still crashes, but now you can only see the things if the background behind them is white...

    I found something from a tutorial that might work though:
    -------------------------------------------------------------------------
    Mask
    -white for transparent, black for opaque

    Image
    -Black for transparent, other for opaque

    Procedure
    -Create mask
    -Blit mask to background using SRCAND
    -Blit image to background using SRCPAINT
    ------------------------------------------------------------------------

    The tutorial had 2 bitmaps though, not 1 bitmap and 1 generated mask. I'll try this, and if I have problems, I'll post again
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    well the crash has nothing to do with the blits. Maybe your mask creation. But I'm too lazy to go and find some code that I have somewhere on this computer that does this. how does that feel to know that I have the function handy and won't give it to you? just kidding.
    always looking, make an offer. get me out of this place.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Alrighty, it seems to be working decently, as long as I can get past the odd occasional slowing down of performance and the not-so-great transparency... Thanks to everyone for getting me thinking about using DIB's
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    the transparency should be perfect if it was done right. What's wrong with it exactly?
    always looking, make an offer. get me out of this place.

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Well... here's the list:

    1) I have a ship flying around; it's got a little black area at the top.

    2) I also have a bunch of aliens flying around; they have a tiny black area at the top-righthand corner, as well as red speckles along the left of the image and blue along the right. There are also blue speckles along the lower-lefthand of the alien (not the entire image, just the opaque part) and red along the lower-righthand... I had the same problem using my old TransBlt() function (minus the black area).

    3) I have 3 weapons to cycle through... I shoot one weapon, and it goes fine (speedwise, though I suspect there's a black thingee too). Each of the other 2 work too. Then I cycle back to the first, and shoot again; suddenly, KABLAMMY, and it slows down (though not by much). After a while, it speeds up again. Cycle to the second; suddenly, KBLAMMY! And so it goes on and on. If I change the code so that only one of the weapons uses TransBlt (the others use BitBlt instead), it won't slow down. The aliens/ship use TransBlt too.

    If you want, you can take a look at the entire program down below. It's called "Space Invaders 2" (yes, uninspired).

    And BTW, yes I know that DirectX is better than GDI for making games - I'm just trying to get game-making basics as well as GDI skills down before moving on to more advanced stuff.
    Last edited by Hunter2; 07-25-2002 at 08:39 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    *This is the zip
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-17-2008, 04:00 PM
  2. Transparency thing
    By Gordon in forum Windows Programming
    Replies: 2
    Last Post: 04-23-2008, 10:34 AM
  3. Nonclient & client area transparency
    By TheDan in forum Windows Programming
    Replies: 0
    Last Post: 03-27-2007, 02:19 PM
  4. How do you print on a transparency ?
    By Zeeshan in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-25-2003, 02:29 PM
  5. GIF Transparency
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 04-20-2002, 05:46 PM