How they do alpha in DirectDraw? Help!

Started by
6 comments, last by bogdanontanu 23 years, 11 months ago
u all know Starcraft ...? anyway it has a lot o transparency/alfa blending but works on any 2d card and only req DDraw not D3D.... i try to make a game like that.. but cant do transparent units .. i try with Lock+add+Unlock ..it works but it is extra slow even on my Vodoo3 card I dont think it can be done like this But how the hell else? Please Help.....
obysoft
Advertisement
I think Starcraft runs in a paletted 8-bit mode (256 colors). This is why
the program runs fast, even with all the translucent menus that they put
on the screen. I think they use 50% blending of RGB(0,0,128) to get the
blue translucent menus and use 50% blending of RGB(128,128,128) for the
grey ones.

50% blending is simply taking two colors and dividing them by 2:
(A+B)/2

Which is fast.

thank u anonymous helper...

I know they use 8 bit with pallete
and i understand that this helps a lot at
pallete animations and low memory req....

but can u also eplain how they do all that cloaked units
can be 64 of them in one screen...which are not 50% i think
and all units have shadows on each other (b/w thought)

... and after all how do u do (color_a+color_b)/2
in palete mode
u must search a color in ur index to match the resulting color?

Thanks in advance


obysoft
The well known way to do "alpha" in 8 bit mode is to use "alpha tables":
Let''s say you want to implement semitransparent menus. I.e. everything under the menus looks 50% darker. You''ll need to create a table like this:

byte alphaTable[256];

for( i = 0; i < 256; i++ )
{
// r1-g1-b1: palette color

r1 = palette[0] / 2;
g1 = palette[1] / 2;<br> b1 = palette[2] / 2;<br><br> // r2-g2-b2: 50% darker than r1-g1-b1<br> // …it may not exist, so we have to<br> // find a closest match. It''s index is<br> // placed in alphaTable.<br> <br> r2 = r1 / 2;<br> g2 = g1 / 2;<br> b2 = b1 / 2;<br> <br> dmin = 256;<br> for( j = 0; j < 256; j++ )<br> {<br> r3 = palette[j][0];<br> g3 = palette[j][1];<br> b3 = palette[j][2];<br> dr = r3 - r2; if( dr < 0 ) dr = -dr;<br> dg = g3 - g2; if( dg < 0 ) dg = -dg;<br> db = b3 - b2; if( db < 0 ) db = -db;<br> d = ( dr + dg + db ) / 3;<br> if( d < dmin )<br> {<br> d = dmin;<br> alphaTable = j;<br> }<br> }<br>}<br><br>So now you can use alphaTable to perform super-fast alpha.<br>For example, here is a routine to draw a semi-transparent rect:<br><br>void DrawAlphaRect( int x1, int y1, int x2, int y2 )<br>{<br> int x, y;<br> byte pixel;<br><br> for( y = x1; y <= x2; y++ )<br> {<br> for( x = x1; x <= x2; x++ )<br> {<br> pixel = GetPixel( x, y );<br> SetPixel( alphaTable[pixel] );<br> }<br> }<br>}<br><br>Think about this:<br><br>* Palette should have 2 versions of every color: dark and light.<br>* You can have more than one "alphaTables" for different levels of alpha.<br>* r1 = palette[0] / 2;<br> g1 = palette[1] / 2;<br> b1 = palette[2] / 2;<br> …can be changed to something like…<br> r1 = palette[0] / 2;<br> g1 = 0;<br> b1 = 0;<br> …that will create "darken red" alpha effect.<br><br>———<br><br>Cheers!<br>Meduzza<br> </i>
ALL I can say....Meduzza...
is that YOU ARE GOOD....

Thanks a lot....

i was doing it in 16bit (565) but still i was wondering
how the hell did they do it in 8bits...
Now i Know ...thanks to You...
obysoft
One more little thing...
I do this game in pure 32bit Assembler

and even if i can read C++ i wonder why are u all
guys only use C...?
C its at least clumpsy not to say more..

anyway You write it more like pseudocode so even i can understand it....

dont flame on me ..please...
obysoft
People often stick with C because
(a) They are reluctant to give up a tool (in this case, a language) that has worked well for them in the past, and
(b) They learned from other people who are reluctant to give up that language

Personally, I prefer C++, and believe it''s a slightly better language. But if I''d learnt C first, I might well consider that the gains from moving over would be outweighed by learning to think within a totally different paradigm. I think people who learn C++ without learning C first have an advantage in that they don''t have to ''unlearn'' and ''relearn'' anything.
Another one of the reasons is because some parts of C++ are inherently slower (exception handling and such). C is more basic, but less robust. Assembly is even MORE basic, but even less robust.

NOTE: I''m not trying to start a C/C++ war, please don''t a debate.

This topic is closed to new replies.

Advertisement