dOOd, because you have locked yourself into a packed pixel world, your solution
is precalculation + memory hogging! Yes, you could render that fading to each
frame in real time, but that would go too slow on todays superior computing
systems.
Here is some 16 bit fading code I wrote a long time ago, before I was
enlightened and moved to 32bpp, the PROFESSIONAL pixel mode.
- Precalculate a fade lookup table. You’re going to need maybe 500k just for
the lookup table, but hey, what’s 1MB of memory in the world today? The more
fade steps, the better the look of the fade out / in. hell, with my system you
can even fade to green, white, etc etc. You can even in 16 bpp make
VIRTUAL palettes! It’s that cool?
/* This will give you 8 fade steps */
typedef struct Fade_Block {
Fade_Table tables[8];
} Fade_Block;
/* Contains the 8 fade steps */
typedef struct Fade_Table {
Uint16 table[0x10000];
} Fade_Table;
/* Screen gives you the format (16 bits – yuck), Fade_Block is the allocated
memory you are going to store the fade table in, and denominator is the number
of fade steps you want to precalcuate for, don’t specify too many! */
void
Calculate_Fade_Table (SDL_Surface * screen, Fade_Block * fb, Uint16 denominator)
{
Uint16 r, g, b;
Uint16 x;
Uint32 y;
Uint16 rr, gg, bb;
Uint8 table = denominator;
float precalc;
while (–table)
{
precalc = ((float) table) / ((float) denominator);
for (x = 0xFFFF; x > 0; x--)
{
RGB_FROM_PIXEL (x, screen->format, rr, gg, bb);
r = rr * precalc;
g = gg * precalc;
b = bb * precalc;
PIXEL_FROM_RGB (fb->tables[table].table[x], screen->format, r, g, b);
}
}
}
- Now you say, but how do I fade out?
THE ANSWER: My loser fade out 16.
/* Screen is the surface you want to apply the fade to, original is a copy of
what the normal screen looks like, neither faded in or out, but just normal. We
need it to apply fade-in / out steps to. NOTE: original and surface must be the
same size and have the same dimensions */
Fade_Out_16 (SDL_Surface * screen, Uint16 * original, Fade_Block * fb)
{
Uint8 x = 15;
Uint16 *p;
Uint32 size;
Uint16 *t;
while (x–)
{
p = screen->pixels;
t = original;
size = screen->w * screen->h;
while (size--)
{
*p = fb->tables[x].table[*t];
p++;
t++;
}
SDL_Delay (25);
SDL_UpdateRect (screen, 0, 0, 0, 0);
}
}
- Lastly, fading back in
Fade_In_16 (SDL_Surface * screen, Uint16 * original, Fade_Block * fb)
{
Uint8 x = 0;
Uint16 *p;
Uint32 size;
Uint16 *t;
while (x != 16)
{
p = screen->pixels;
t = original;
size = screen->w * screen->h;
while (size--)
{
*p = fb->tables[x].table[*t];
p++;
t++;
}
SDL_Delay (25);
SDL_UpdateRect (screen, 0, 0, 0, 0);
x++;
}
}
Ehh…well…I wrote this code a long time ago, not really well written, but it
worked for me fine, use it if possible…you are welcome to any more questions
but I advise you to leave the 16 bit world…it’s no fun. At least that’s what
I found out.
Paul Lowe
spazz at ulink.net