I'm trying to write a screen tinting and fading function that can be made nonblocking (obviously I can't start that yet since the blocking version doesn't work) and I've run into a problem.
The tinting code doesn't work half as well as I wanted it to. It works, it just... tints the screen a little bit, even with repeated applications, it doesn't fade completely to any colour. It's based on ages old code from AGS 2.1 or so, so likely I've got something terribly wrong. What Fadein and Fadeout are supposed to do is fade the screen to the colour completely. What tint is supposed to do is tint the screen a certain amount.
//backuppal is the original palette, unmolested by any palette functions.
//
static function PALgorithms::Tint(char R, char G, char B, char p) {
if (p > 63) p = 63;
int i;
while (i<256)
{
palette[ i].r = backuppal[ i].r + (R - backuppal[ i].r)*p/256;
palette[ i].g = backuppal[ i].g + (G - backuppal[ i].g)*p/256;
palette[ i].b = backuppal[ i].b + (B - backuppal[ i].b)*p/256;
i++;
}
UpdatePalette();
Wait(1);
}
static function PALgorithms::Fadeout(char R, char G, char B, char speed) {
if (speed>63) speed = 63;
int p=0;
while (p<=63)
{
PALgorithms.Tint (R,G,B,p);
p+=speed;
}
}
static function PALgorithms::Fadein (char R, char G, char B, char speed) {
if (speed>63) speed = 63;
int p=63;
while (p>=0) { PALgorithms.Tint (R,G,B,p); p-=speed; }
}
What's gone wrong?
I haven't really read the codes, but if you're going to gradually change the palette to a particular colour, should you be doing thing like this?
palette[ i].r = (backuppal[ i].r*(63-p) + R*p)/63;
(not tested, repeat for g and b channels)
Works like a dream! Now I have a fading function that doesn't pause the game! Thanks so much :)