Skip to main content
GameDev.net gamedev.net
🔒 Locked

Screen fading to black

Started by ursus Jan 14, 2007 at 1:01 PM 9 replies 2.1k views
Original Post
ursus
ursus
Hi Guys, I was wondering if it's possible to achieve 'screen fading to black' effect using pure Win32, without any external libraries. The only thing that comes to my mind for the moment would be to run through every pixel on the screen and decrease its RGB values but this sounds like time and resources consuming process. I'd appreciate any suggestions. Many thanks
TheAdmiral
TheAdmiral
That's how it's done, on some level or other. In software, you can do little better than grab the screen buffer, iterate over it with a darken pass, and copy the buffer back. Lather, rinse, repeat; ideally in a time-independent manner. While necessarily slower than the hardware equivalent, this should look very convincing if using pure GDI.

Just what 'screen' are you planning on fading to black? If it's just your window's DC, everything should be straightforward. Otherwise, be prepared to make some nasty hacks.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Aardvajk
Aardvajk
If you want to speed up pixel work with Win32, you may want to have a look at GetDIBits and SetDIBits.

GetDIBits allows you to convert a bitmap into a system memory array of bytes in the format R,G,B,Reserved. You can work on these far faster than the pixels of a bitmap directly, then use SetDIBits to dump the result back into the bitmap.

Far, far faster than accessing pixels one at a time directly through a device context if you have to modify lots of pixels.

HTH
ursus
ursus
GetDIBits and SetDIBits sound promissing.

Thank you guys for help!
Daniel Miller
Daniel Miller
Couldn't you draw a black rectangle on the screen with decreasing transparency?
ursus
ursus
Quote:
Original post by Daniel Miller
Couldn't you draw a black rectangle on the screen with decreasing transparency?


I'm not sure how. This is why I asked in the first place :)
Daniel Miller
Daniel Miller
I wish I could help, I don't really know win32.
Evil Steve
Evil Steve
Quote:
Original post by Daniel Miller
Couldn't you draw a black rectangle on the screen with decreasing transparency?
You could, if plain Win32 supported transparancy, which it doesn't.
Aardvajk
Aardvajk
Weirdly though, Win32 does have AnimateWindow, which supports alpha blending when showing and hiding a window.

I guess you could write a sort of hackish solution with a combination of this and a borderless black window. This is just a theory.

[EDIT] Actually, no. Apparently you can only apply AW_BLEND to a top level window.
Todo
Todo
There are several options here, which one you chose depends on several factors:

1) DIY. By far the most portable option would be through the use of device independent bitmaps, and working directly at the pixel level. Get a pointer to the bitmap bits (through the aforementioned GetDIBits or even more CreateDIBitmap/BitBlt GDI functions) and implement an blend function. If only used to perform a fade-out, this can be implemented as fast as a bit shift per channel per pixel (or even faster, perhaps making use of modern instruction sets). There's a multitude of information on generic alpha blending here on Gamedev.net.

2) Use the (SLOW!) AlphaBlend GDI function. Be warned though that it requires some preprocessing on your behalf. Also, this function exists only as of Windows 2000 (and some late editions of Windows 98).

3) Use logical operators. At the expense of quality, you could use the AND operator for example, to quickly fade out an image.

I guess more solutions exist, but these can get you along pretty far.

NOTE: 2 proves the existence of a blending operator in the API, albeit a terribly slow one for large images.
ursus
ursus
Thank you very much for your answers. This is getting even more interesting!

I knew pros have some aces up their sleeves

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.