Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1980
  • Last Modified:

Alpha Blending using BitBlt's raster features and, is there such as thing as Hardware Rastering in DirectDraw?

Hi all.

I've read up on methods for alpha blending on DirectDraw in MSDN. The only methods they offer is to do it "by hand" which means to lock the surface, do a pixel blending directly onto the back buffer, before unlocking.

So therefore, my understanding is,
Alpha Blending is a mixture of 1 or more surfaces with less than 100% opacity.

So for example, I have a magenta surface RGB(255, 0, 255) at 50% opacity. RGB(127, 0, 127)
Then I have a white surface RGB(255, 255, 255) at 50% opacity. RGB(127, 127, 127)
Logically when I blend these 2 surface together I'll get BRIGHT MAGENTA right?
Technically it should be RGB(254, 127, 254) - a bit of pink I believe.

I was thinking, could I have used a simple raster operation to actually perform this blending?
which is, SRCAND? The only raster ops I can perform is by using the WIN32's bitBlt method which is rather ... slow...

Secondly, I often find Hardware Rastering in DirectDraw a pain. It doesn't seem to work at all. Can anybody explain why?

Lastly, please do not advise me to use OpenGL 3D or Direct3D for Alpha Blending. My level of games programming is still relatively low and I'm still at DirectDraw.

Thanks for your attention and guidance.


0
thrawn80
Asked:
thrawn80
  • 4
  • 3
1 Solution
 
davebytesCommented:
You'll find that the reason people push to the 3D solutions is that you don't do anything 'by hand' -- the hardware can do alpha blending intrinsically.

Additionally, DirectDraw interfaces while still maintained for backwards compatability, have basically been 'dead' since DX8 landed on the scene.  So there's less and less active sample code, sprite libraries, etc., that you can learn  from.

Alpha blending is mixing surfaces where alpha is a component of the mixing.  Unfortunately, the best guides to blending, and the algorithms, are all in 3D land.  They extensively cover the different types of blending operations provided natively in hardware.

What you've described is an ADD operation.  That's one approach.  You can also use multiply, subtract/diff, etc.  Also, typically it is the SOURCE ALPHA (the top layer) that is important to the blend calculation, though it depends upon what you want the result to be.  'Alpha blending' is a set of techniques and math/algorithms, but is open and flexible (for the most part)... If you want the result to be half of the two, then you take:
(SRC-RGB * SRC-ALPHA) + (DST-RGB * DEST-ALPHA)
If you want a standard textured-alpha-blend used to apply sprites over a background, you want a neat trick:
(SRC-RGB * SRC-ALPHA) + (DST-RGB * (1 - SRC-ALPHA))
Which uses the source alpha to determine how much of the destination color to 'allow through'.  When source alpha approaches zero, 1-SA approaches 1, thus the sprite pixels stop showing and the destination shows fully.

That help at all?

-d
0
 
thrawn80Author Commented:
Hmmm.... my approach to blending my surfaces would be

(SRC-RGB * SRC-ALPHA) + (DST-RGB)
SRC = blending surface
DST = back buffer
BitBlt seem to solve these problems in a simplier way.

if(any of the R, G or B exceeds 255, it'll remain at 255)

would that be simplier enough?

Yes, you are right. Nowadays most if not all of the computer users are using AGP, which would imply the availability of 3d acceleration. Therefore, writting a game in 2d using a 3d engine is quite a norm nowadays I believe.

0
 
davebytesCommented:
that's a "saturated add".  and a fine solution if it looks the way you want it to.  If BitBlt takes care of it using that, go for it. ;)

Yes, you'll find dozens of sprite engines (or starter kits) popping up now, since it's really trivial to do certain classes of 2D/sprite coding with 3D hardware.  Some scrolling systems don't work quite as well as old dedicated virtual video pages did, but then you get the advantage of lighting, blending, rotation, effects, all for 'free'. ;)

-d
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
thrawn80Author Commented:
Any suggestions on other ideas or methods?
0
 
thrawn80Author Commented:
Btw, regarding Hardware rastering... is it possible?

Can anyone shed some light on this topic?
0
 
davebytesCommented:
I seem to recall that DX5-7 came with DirectDraw-based 'sprite game' sample(s).  Have you tried running those on your machine?  Do they work?  They should have used accelerated methods, but in hindsight maybe they didn't...

The same (well, similar) samples should be available with the DX9 SDK, but they'll use the 'DirectX Graphics' (d3d merged with dd) approach to do it.

It is certainly worth taking a look at the DX9 SDK to see if you can 'grok' the basics of the equivalent sprite samples -- it's gotten much easier to do cool tricks, but yes you need to make the leap to understand just a little bit about the setup and organization of 3D data.  Old DirectDraw methods are being maintained for backwards-compatibility, but the 'top features' may not necessarily all be there.

-d
0
 
thrawn80Author Commented:
icic.. thanks.

The author of the book, Ernest Pazera once said that the Hardware Rastering features on DirectX 7 seemed a little sketchy and eccentric...

But then again, I'm seriously considering using a Direct3d to write 2d games....

Thanks again.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now