Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » rendering a fading background

This thread is locked; no one can reply to it. rss feed Print
rendering a fading background
Frank Drebin
Member #2,987
December 2002
avatar

is there a way to create a background (every game cycle) that fades from one color to an other?
i think you have to write a loop for every pixel then but that would be damn slow...
or is there a better way for doing this?

Trent Gamblin
Member #261
April 2000
avatar

If you use 8 bit color you can cycle a palette.

HardTranceFan
Member #7,317
June 2006
avatar

Are blender functions of help here? Not sure on this as I haven't used them myself as yet.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

Frank Drebin
Member #2,987
December 2002
avatar

i'm not usgin 8bit mode but perhaps the blender-functions are what i'm looking for...
perhaps set_screen_blender ? anybody knows how to use them?

Archon
Member #4,195
January 2004
avatar

Quote:

is there a way to create a background (every game cycle) that fades from one color to an other?

I'd have to suggest OpenGL as you can easily glColor the corners of your background to whatever you like - and it's fast.

Otherwise, look into software blending.

Audric
Member #907
January 2001

I guess you mean a rainbow effect like:
turrican2-6.png
You can try to loop a vertical series of hline()

The speed of hline() is probably driver-dependant. If the result is not fast enough, pre-compute the rainbow on a BITMAP and blit it, hoping for a fast memory copy.

Frank Drebin
Member #2,987
December 2002
avatar

ok i'm not using opengl. so what about software blending?

[edit]
yes but no rainbow, the fading should be smooth!
ok i could precompute the image on a bitamp and then blit from that bitmap to the screen - but with maps of 1280*960 pixel (or even bigger) this would be a lot of memory...

GullRaDriel
Member #3,861
September 2003
avatar

Quote:

yes but no rainbow

1int y=0;
2 
3float red =0.0 , red_inc = 0.5 ;
4 
5BITMAP *backgroung;
6 
7background = create_bitmap( SCREEN_W , SCREEN_H );
8 
9for( y = 0 ; y < SCREEN_H ; y +=1 ){
10 
11red=red + inc;
12if( red > 255 ){
13red = 255;
14inc = -inc;
15}
16 
17hline( background, 0 , y , SCREEN_W , makecol( (int)red , 0 , 0 ) );
18}
19 
20blit( background , screen_buffer , 0 , 0 , 0 , 0 , SCREEN_W , SCREEN_H );

With this there will be no rainbow.

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

Audric
Member #907
January 2001

smooth? As long as you compute a different (theoric) color for each line, smoothness will ultimately depend on the screen's color depth. OpenGL will display exactly as many colors as software drawing, no more, no less.
Anyway, you can choose the color for each line yourself, to adapt to your color depth.

edit: Yep, I meant mathematical interpolation between two colors, like Gullradriel shows for #000000 to #FF0000. The Turrican screenshot I linked is a 256 color GIF, but the actual ingame rainbow has 12bit color depth.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

but with maps of 1280*960 pixel (or even bigger) this would be a lot of memory...

Is 4.5 MB a lot of memory? Not these days. Though just using hline to render the background over and over could possibly be even faster, as it's more cache-efficient.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Frank Drebin
Member #2,987
December 2002
avatar

ok i think a nice way would be to have a "precomputed" bitmap that is 10*MAP_HEIGHT and blit this for MAP_WIDTH/10 times.
this way i would save some memory.

Audric
Member #907
January 2001

You can even try to store it as a single line, 1x960, and stretch_blit() it across the whole screen's width.
This technique is sometimes used for web pages.

Dustin Dettmer
Member #3,935
October 2003
avatar

int col1, col2;
int steps = 60 * 5; // about 5 seconds long

for(int i = 0; i < steps; i++) {
 int col = makecol((getr(col1) * (steps - i) + getr(col2) * i) * 255 / steps,
            (getg(col1) * (steps - i) + getg(col2) * i) * 255 / steps,
            (getb(col1) * (steps - i) + getb(col2) * i) * 255 / steps);

 vsync();
 rect(screen, 0, 0, SCREEN_W, SCREEN_H, col);
}

Frank Drebin
Member #2,987
December 2002
avatar

Go to: