![]() |
|
A question of transparency |
edfredcoder
Member #7,985
November 2006
![]() |
I'm struggling with transparency. I'm trying to draw a bitmap (that I do not load from a file), called fog, onto the screen, but it's not working! What I have is something like this: //Create the fog BITMAP *fog; fog=create_bitmap(640,480); clear_bitmap(fog); //Now randomly place a few circles for the fog for(int i=0;i<10;i++) circlefill(fog,rand()%640,rand()%480,rand()%50,makecol(255,255,255)); //Now draw the scene to the screen with out fog //... //Draw fog set_trans_blender(0,0,0,255); draw_trans_sprite(screen,fog,0,0); What I would like to happen is for the areas that are covered by the white circles to show completely, with a circle of color makecol(128,128,128) greying the pixels underneath, and the black areas of the fog to completely blot out whatever is under them, so you only see black. So far, I only get the fog covering everything on the screen. Any suggestions on how to do this? |
kazzmir
Member #1,786
December 2001
![]() |
Draw the circles with makecol(255,0,255) so they dont occlude whats beneath them. Then I guess you would have to set the alpha value of the grey circles to less than 255 so that they are translucent. |
edfredcoder
Member #7,985
November 2006
![]() |
It seems to work well; thank you for a solution. However, I'm still confused: what is special about the color makecol(255,0,255) that fixed it so that the circles worked? Also, if I wanted to have a translucent part of the fog, say, have the center of the circle be completely transparent and the other rims increasingly dimmer, how could I do that? And how do I set the alpha value of a pixel in a bitmap (right now I'm using 16 bit color)? |
Kikaru
Member #7,616
August 2006
![]() |
It is called "magic pink." It is defined in allegro as invisible. |
edfredcoder
Member #7,985
November 2006
![]() |
Oh, sorry. I should have thought of that. Does anyone know how to do the fog effect, but with not 100% transparent fog, but with the center all the way clear and the edges less clear? |
Kikaru
Member #7,616
August 2006
![]() |
Alpha blending. |
edfredcoder
Member #7,985
November 2006
![]() |
I'm not sure how to use alpha blending with Allegro. I think what I need is a 32-bit bitmap called fog, so I could do something like: BITMAP *fog=create_bitmap_ex(32,640,480); After that, I need some way of setting the alpha part of each pixel in the image, which I was unable to find in the Allegro manual (so please don't tell me to rtfm!). Then, I need to just blit the fog onto the image like normal, right? Or am I missing something here? |
Kikaru
Member #7,616
August 2006
![]() |
blit() does not take into account any alpha channel!
You can use it as you want. Basically, just use it like draw_sprite(), only give it the last parameter to tell it how much to blend the image into the background. |
edfredcoder
Member #7,985
November 2006
![]() |
Thanks! The code you posted is great, but as you said, it requires 32-bit color depth. I wrote my own just now, which accepts an 8-bit bitmap which contains the alpha part and anouther bitmap, as well as the position (x,y) of where to place the alpha part on the other (truecolor) bitmap; so far, it works. Here is my alpha-blending function:
Now the fog of war really does look like fog! |
Kikaru
Member #7,616
August 2006
![]() |
I wrote a whole header of cool functions, and I will probably release it sometime. Good that you solved you problem! |
|