Jump to content
  • Advertisement
Sign in to follow this  
jwezorek

blitting with per-pixel alpha in Win32

This topic is 2547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a prototype of a puzzle game -- not really concerned with the graphics or the evetual target platform right now -- and am doing it as a native Win32 app. The game is tile-based. Right now I'm rendering the tiles with regular BitBlt's plus am using AlphaBlend with SourceConstantAlpha to implement a fade in and fade out effect on whole tiles. However, I was wondering if I wanted to render some stuff using per-pixel alpha, what would be the easiest way to do it? -- where by "easiest" I mean least work for me.

I know that AlphaBlend is supposed to support per-pixel alpha, but as far as I can tell I'd have to create the bitmaps I want to blit this way with CreateDibSection which allows access to the raw pixel data and multiply the RGB values by the alpha values myself. At least I think that's the way it works. I guess, I'm wondering if

(1) There's 3rd-party code somewhere that will take care of all the details and give me a couple of functions I can use e.g. LoadPremultipliedBmpfromPng(..) and BlitWithPerPixelAlpha(...).

(2) Can I get photoshop to export a BMP with premultiplied alpha?

(3) Also on other projects like this, I've used the FreeImage library to rescale and compose png's together. I see that there's a FreeImage_PremultiplyWithAlpha() so could I just use that, call the FreeImage-to-HBITMAP routine I posted in this old thread, call AlphaBlend, and have everything just work? Because that would be pretty easy, although I'd rather not introduce a dependency on FreeImage just for perpixel alpha if there is some code sitting around somewhere that I somehow missed in my googling.

Share this post


Link to post
Share on other sites
Advertisement
It's easy to access the raw pixel data to perform blends (which are mostly trivial mathematical operations), but you'll find that doing so will kill your performance in any high resolution/high framerate scenario. Use OpenGL or DirectX to render your scene instead.

Share this post


Link to post
Share on other sites
Not interested in re-writing this to DirectX. This game is going to end up on a mobile device -- I'm just developing it to Win32 now because, for me, that's the quickest way I can get something up, and I want to calibrate the game parameters and see if it is actually fun before comitting to a platform I'm not familiar with developing for (and before buying a Mac mini for development if I target the iPad, which is what I'm leaning towards)

Actually performing the alpha blends myself would be too slow, yes. That's not what I'm talking about. I'm talking about using the actual Win32 API call for doing alpha blends which is called AlphaBlend. The trouble with it is is that it's a pain to use, a pain to format the pixel data the way that that function wants it formatted. But as I understand it, it's a one time set up cost in which per-pixel math needs to be done, after that using AlphaBlend shouldn't be prohibitively slow i.e. I'm already using AlphaBlend(...) in this game, just not with per-pixel alpha.

Anyway the time I've put in to writing this post is probably greater than what it will take me to write the code on top of FreeImage. It's just that you see this question being asked all over the internet, e.g. StackOverflow, etc., but you never see someone just post the the two functions
[font="Lucida Console"]
HBITMAP LoadPremultipliedBmpfromPng( TCHAR* filename ) [/font]

and
[font="Lucida Console"]
void BlitWithPerPixelAlpha(HDC dst, int dstX, int dstY, int wd, int hgt, HDC src, int srcX, int srcY) [/font]

as an answer, and was wondering if someone has such code sitting around.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!