Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Fading

Credits go to gnolam for helping out!
This thread is locked; no one can reply to it. rss feed Print
Fading
Neil Black
Member #7,867
October 2006
avatar

How can I do a fade in or fade out when I'm not using a palette? Also, how can I do a day/night cycle where the screen slowly darkens to night when I'm not using a palette?

gnolam
Member #2,030
March 2002
avatar

The easiest way is to draw a transparent black rectangle over the screen (buffer).

int alpha = 128; //arbitrary value for demonstration purposes

drawing_mode(DRAW_MODE_TRANS);
set_trans_blender(0, 0, 0, alpha);

rectfill(buffer, 0, 0, buffer->w, buffer->h, makecol(0, 0, 0));

blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

Quote:

Also, how can I do a day/night cycle where the screen slowly darkens to night when I'm not using a palette?

Depends on what kind of effect you're after. For a simple "everything turns darker" effect, see the code snippet above.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Neil Black
Member #7,867
October 2006
avatar

Okay, in 16 bit mode I thought there wasn't an alpha value. I should have mentioned that I'm using a 16 bit color depth. Will that still work?

And the day/night cycle needs to fade to night, where the colors are darker and seem more bluish, and then back to day. I could have separate tiles for day and night, but there would be problems during the transition. Whatever I do has to be fast enough to run during game play.

Kauhiz
Member #4,798
July 2004

Quote:

Okay, in 16 bit mode I thought there wasn't an alpha value.

There isn't, that's why you have to supply it separately. In 32bpp you could have the alpha values stored in the bitmap.

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

ImLeftFooted
Member #3,935
October 2003
avatar

const int cycles = 60 * 3;
for(int i = 0; i <= cycles; i++) {
 conf.beginScene();
 drawGame();
 Bitmap::rect(0, 0, 640, 480, Tint(1, 1, 1, float(i) / cycles);
 conf.flip();
}

Neil Black
Member #7,867
October 2006
avatar

okay, dustin dettmer, you have several things I've never seen before in that code snippet, like Tint(...). is that an allegro function or just something you wrote that does what gnolam said? I also don't know what conf.beginScene() or
conf.flip() do. But I do understand the general idea... I think. I'd better do some experimenting.

GullRaDriel
Member #3,861
September 2003
avatar

It looks like openlayer thing.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Neil Black
Member #7,867
October 2006
avatar

Openlayer? oh crap, more things-I-don't-understand-but-need-to! I thought I was through with that after I learned to tie my own shoes when I was 17 ;)

ImLeftFooted
Member #3,935
October 2003
avatar

Its actually from the Thing library. Which will soon be merged with OL (aka once I get some free time).

Steve Terry
Member #1,989
March 2002
avatar

In 16-bit you can basically do a fast fade by packing two bytes into a 32-bit register. Here is the code I used:

1void Fade16(BITMAP *src, BITMAP *dest, int frame, int x, int y){
2 unsigned long* read;
3 unsigned long write;
4 register unsigned long c1, c2;
5 BITMAP *tmp = create_bitmap_ex(16, src->w, src->h);
6 int i, j;
7 int wid = src->w;
8 acquire_bitmap(tmp);
9 for(j = 0; j < src->h; ++j){
10 read = (unsigned long*)src->line[j];
11 write = bmp_write_line(tmp, j + y);
12 for (i = wid; i; i-=2){
13 // get two pixels;
14 c1 = *read++;
15 c2 = c1 & 0x7E0F81F;
16 c1 &= 0xF81F07E0;
17 // c1 / 32 * frame... then mask out the color components...
18 c1 = (((c1 >> 5) * frame) & 0xF81F07E0);
19 // c2 * frame / 32... then mask out the color components...
20 c2 = (((c2 * frame) >> 5) & 0x07E0F81F);
21 bmp_write32(write + x, ( c1 | c2 ));
22 write+=4;
23 }
24 }
25 bmp_unwrite_line(tmp);
26 release_bitmap(tmp);
27 blit(tmp, dest, 0, 0, x, y, tmp->w, tmp->h);
28 destroy_bitmap(tmp);
29}

Well maybe not... I think I had a better function that didn't require a temporary bitmap... but meh?

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Neil Walker
Member #210
April 2000
avatar

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Neil Black
Member #7,867
October 2006
avatar

Gnolam's method seems to be the easiest, so I tried it first. Works perfectly! Thanks guys

Go to: