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

x
?
Solved

Alpha Blending

Posted on 2003-03-30
14
Medium Priority
?
891 Views
Last Modified: 2007-12-19
Hi,

I am writing a 2D RPG engine in VB with DirectDraw 7. I was wondering if there is any easy way to do Alpha Blending with certain tiles.

I have a custom format for my tiles, which is simply a bunch of Long's (ABGR) describing the 32x32 tile.

I would like to know how to do the alpha stuff, since DD7 doesn't support it.
0
Comment
Question by:pkmnfrk
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 4

Expert Comment

by:joachimc
ID: 8237926
I am not using DD7 any more but I am sure that alpha is supported in it. Isn't there the example with a background pic and something flying in circles on it. That is alpha blended I think
0
 

Author Comment

by:pkmnfrk
ID: 8241248
No. It says in the SDK quite clearly that "the alpha channel will be ignored" and "alpha blending is not supported in this version".
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8246616
But why are you using DirectDraw? It can't do anything you cannot do with Direct3D [or DirectXGraphics as it is known in DX8+], and the simplicity comes at a price, namely that DirectDraw isn't that well supported in modern graphics drivers anymore. The functionality is there, sure, but the performance just isn't since modern cards are more and more tweaked towards handling 3D data.

I would very much suggest saying good bye to the soon-to-be-forgotten DirectDraw interfaces and use Direct3D instead. You will have to learn about new concepts, but that's a Good Thing (tm) anyway.

.f
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:tinkledoomhammer
tinkledoomhammer earned 150 total points
ID: 8250164
If you aren't trying to make a high-end game, no acceleration is needed and dx7 will work fine.
Actually, mmx technology is what your situation calls for.

If a high alpha means opaque, then just.
1. Multiply the rgb values in each layer by the alpha.
2. Divide by the maximum alpha.
3. Add each layer together.

If you want a high alpha to mean transparent, then just divide each r,g, and b by the corresponding alpha.

This example assumes that your bitmaps are in 4 arrays:
source_a[layer,col,row]
source_r[layer,col,row]
etc

FOR x=1 to width
  FOR y=1 to height
    final_r[x,y]=0
    final_g[x,y]=0
    final_b[x,y]=0
    FOR z=1 to numberOfLayers
      final_r[x,y]=final_r+source_r[z,x,y]/source_a[z,x,y]
      final_g[x,y]=final_g+source_g[z,x,y]/source_a[z,x,y]
      ...
    ...
  ...
...

My basic is a little rusty, so I will appologize in advance for any errors.

There are a few optomizations that would make it a bit faster, but the internal VB optomizations should be more than sufficient. Code like that tends be compiled to do several pixels or the entire triplets with a single machine-language command. If you set it to comple for pentium pro, pentium 2, or later. If it there is a check box for MMX, use it.
0
 

Expert Comment

by:tinkledoomhammer
ID: 8250183
One more thing:
If you don't need a lot of different shades of transparency (no fade-ins or -outs) then a binary shift would speed it up a little.
0
 

Author Comment

by:pkmnfrk
ID: 8250314
Hmm, I'll try your suggestion and see what I can do. I never really thought about approaching it from that angle. I'll try it before I award the points, though.
0
 
LVL 8

Accepted Solution

by:
fl0yd earned 150 total points
ID: 8251938
While SIMD [MMX/SSE/...] would be the way to go for a software render kernel, it involves a steep learning curve. I doubt that you will be able to access those CPU features through Visual Basic calls to start with. Again, I would suggest using Direct3D. Check out this link http://www.directx4vb.com/ to see how easy it actually is. Except for the setup code you will hardly have to change anything in your current project. Follow the DirectX 8 links, to get 27 introductory tutorials. There's also a DX7 section, but I haven't looked into it. I may also provide helpful information to you.

.f
0
 
LVL 1

Expert Comment

by:red_sodium
ID: 8470395
When you say D3D can do everything DD can do and much more, which I truly believe, could i make a 2D sprite engine with it? And would I be able to use rotation, scaling, alphablending, etc, all in it? And how much would I need to know about maths?
0
 

Author Comment

by:pkmnfrk
ID: 8484237
I've tried D3D and for some reason, I can't get it to work. (I'm probably not setting the geometry correctly)

Anyway, I found an easy solution for my project: scrap the alpha for now. I'll do it later, after I have SOMETHING ready for the public. <sigh>
0
 

Expert Comment

by:BAGR
ID: 8860717
If there is no way to create fast alpha blending in DirectDraw, so why some games, for example Jazz Jackrabbit 2, have it? And why this game runs on Pentium 90 ???!!!  :P
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 8870875
BAGR:

* I never claimed there wasn't a way to do fast alpha blending -- SIMD instruction sets are damn fast
* While there may not be alphablending, there certainly is colorkeying-support in DirectDraw -- that's what sprites in general use
* Jazz Jackrabbit 2 is old -- back in those days Direct3D was a _lot_ more computationally expensive

Anything else you have to add?

.f
0
 
LVL 1

Expert Comment

by:red_sodium
ID: 8871544
I've now converted to D3D, and I can safely say that as long as you use LPD3DXSPRITEs, its great. Alpha blending is really easy too, you just set the Alpha channel when calling Draw function.
0
 

Expert Comment

by:CleanupPing
ID: 9464232
pkmnfrk:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 9464663
Question was fully answered, both in a software- as well as hardware-approach. I suggest splitting points between tinkledoomhammer and me [fl0yd].

.f
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question