Fade to Colour function

Started by Scavenger, Fri 12/09/2003 18:39:48

Previous topic - Next topic

Scavenger

Edit by strazer:

AGS v2.62 introduced the SetFadeColor function.




I was writing a custom function for fading into different colours, but when I tested it out it hung the game with the error:
500001 while loop iterations without a response. Or something. Anyway, here is the script I'm using:
short fadecolour;
function Fade (short colour) {
  short bleh;
  if (fadecolour == 0) {
    while (bleh < 256) { //store the palette in these arrays
      br[bleh] = palette[bleh].r;
      bg[bleh] = palette[bleh].g;
      bb[bleh] = palette[bleh].b;
      }
      }
   
  if (colour == NORM) {
    palette[bleh].r = br[bleh];
    palette[bleh].g = bg[bleh];
    palette[bleh].b = bb[bleh];
      }
     
  if (colour == RED) {
    while ((palette[bleh].r <63) && (palette[bleh].g >0) && (palette[bleh].b >0)) {
      if (palette[bleh].r <63) palette[bleh].r ++;
      if (palette[bleh].g >0) palette[bleh].g --;
      if (palette[bleh].b >0) palette[bleh].b --;
      if (bleh <256) bleh++;
      else bleh = 0;
      UpdatePalette();
      fadecolour = 1;
      }}
  if (colour == BLUE) {
    while ((palette[bleh].r >0) && (palette[bleh].g >0) && (palette[bleh].b <63)) {
      if (palette[bleh].r >0) palette[bleh].r --;
      if (palette[bleh].g >0) palette[bleh].g --;
      if (palette[bleh].b <63) palette[bleh].b ++;
      if (bleh <256) bleh++;
      else {bleh = 0;}
      UpdatePalette();
      fadecolour = 1;
      }}
  if (colour == GREEN) {
    while ((palette[bleh].r >0) && (palette[bleh].g <63) && (palette[bleh].b >0)) {
      if (palette[bleh].r >0) palette[bleh].r --;
      if (palette[bleh].g <63) palette[bleh].g ++;
      if (palette[bleh].b >0) palette[bleh].b --;
      if (bleh <256) bleh++;
      else {bleh = 0;}
      UpdatePalette();
      fadecolour = 1;
      }}
  if (colour == YELLOW) {
    while ((palette[bleh].r <63) && (palette[bleh].g <63) && (palette[bleh].b >0)) {
      if (palette[bleh].r <63) palette[bleh].r ++;
      if (palette[bleh].g <63) palette[bleh].g ++;
      if (palette[bleh].b >0) palette[bleh].b --;
      if (bleh <256) bleh++;
      else {bleh = 0;}
      UpdatePalette();
      fadecolour = 1;
      }}
  if (colour == PURPLE) {
    while ((palette[bleh].r <63) && (palette[bleh].g >0) && (palette[bleh].b <63)) {
      if (palette[bleh].r <63) palette[bleh].r ++;
      if (palette[bleh].g >0) palette[bleh].g --;
      if (palette[bleh].b <63) palette[bleh].b ++;
      if (bleh <256) bleh++;
      else bleh = 0;
      UpdatePalette();
      fadecolour = 1;
      }}
  if (colour == WHITE) {
    while ((palette[bleh].r <63) && (palette[bleh].g <63) && (palette[bleh].b <63)) {
      if (palette[bleh].r <63) palette[bleh].r ++;
      if (palette[bleh].g <63) palette[bleh].g ++;
      if (palette[bleh].b <63) palette[bleh].b ++;
      if (bleh <256) bleh++;
      else {bleh = 0;}
      UpdatePalette();
      fadecolour = 1;
      }}
}

I'm hoping to use this for 8bit screen tinting too, though I don't know how to do that. You know, colour balancing and stuff I'm a newbie at. :P

Scorpiorus

Quoteif (fadecolour == 0) {
while (bleh < 256) { //store the palette in these arrays
br[bleh] = palette[bleh].r;
bg[bleh] = palette[bleh].g;
bb[bleh] = palette[bleh].b;
bleh++; // ;)
}
}

-Cheers

Scavenger

Thankye! I got rid of that awful, awful error! But now a new problem has popped up. It won't run. I set it up to fade to red, then change back, and it does nothing. Zilch, zero. I'm sure it should work, but it isn't. Oh woe is me! Now I'll never prove you can get Tints in 8bit. :P I even tried putting a wait command in there, but it doesn't seem to be acknologing the conditionals in there.

Scorpiorus

#3
The problem in while ((palette[bleh].r <63) && (palette[bleh].g >0) && (palette[bleh].b >0)) statements. It quits the loop as soon as palette[bleh].r >=63 OR palette[bleh].g <=0 OR palette[bleh].b <=0.


Here is the script I wrote some time ago. I found it on my hard disk, so:

//Main Global Script

#define SLOTS_MAX 256
#define VALUE_MAX 63

struct CPalette {
 char R,G,B;
};

CPalette Palette[SLOTS_MAX];

function SavePalette() {
 int i;
 while (i<SLOTS_MAX) {
   Palette[ i].R = palette[ i].r;
   Palette[ i].G = palette[ i].g;
   Palette[ i].B = palette[ i].b;
   i++;
 }
}

function fade(char R, char G, char B, char p) {
 if (p > VALUE_MAX) p = VALUE_MAX;
 int i;
 while (i<SLOTS_MAX) {
   palette[ i].r = Palette[ i].R + (R - Palette[ i].R)*p/VALUE_MAX;
   palette[ i].g = Palette[ i].G + (G - Palette[ i].G)*p/VALUE_MAX;
   palette[ i].b = Palette[ i].B + (B - Palette[ i].B)*p/VALUE_MAX;
   i++;
 }
 UpdatePalette();
 Wait(1);
}


function fadeout(char R, char G, char B, char speed) {
if (speed>VALUE_MAX) speed = VALUE_MAX;
int p=0;
while (p<=VALUE_MAX) { fade(R,G,B,p); p+=speed; }
}
function fadein (char R, char G, char B, char speed) {
if (speed>VALUE_MAX) speed = VALUE_MAX;
int p=VALUE_MAX;
while (p>=0) { fade(R,G,B,p); p-=speed; }
}


function on_event (int event, int data) {
 if (event == ENTER_ROOM) SavePalette();
}


//Script Header

import function fade(char,char,char,char);
import function fadein(char,char,char,char);
import function fadeout(char,char,char,char);




There are three different functions to use:

function fade(r, g, b, opacity)

Mixes the screen and the R,G,B color using desired opacity factor.
r,g,b range [0..63]
opacity range [0..63]


function fadein(r, g, b, speed)

Fades in using the fade() function. speed range [1..63]


function fadeout(r, g, b, speed)

Fades out using the fade() function. speed range [1..63]



Hope it helps.

P.S. EDIT: to eliminate the remove of TXT-style.... :)

~Cheers

ThunderStorm

Now this is exactly the kind of stuff that does belong into a knowledge base, in my opinion. In the meantime, I printed it and will put it in a place where it (hopefully) won't get lost.

Great work, thanks for sharing it.
And just for reference: May I use it in my game?

Scavenger

You saved my life! Hehe, Thankye again. I think I shall put you in my credits. All hail! Without people like you to manipulate CJ's text scripting AGS games would be stuck in the stone age.

-Complicated Scripting-
Scorpiorus

Thanking you again.

Scorpiorus

Thanks, guys. I am glad you find it useful.

QuoteAnd just for reference: May I use it in my game?
Sure, Thunder. That's the reason I posted it for. ;)

QuoteYou saved my life! Hehe, Thankye again. I think I shall put you in my credits.
Thanks Dragon, I very appreciate that! :)

QuoteAll hail! Without people like you to manipulate CJ's text scripting AGS games would be stuck in the stone age.
BTW, in your original script you took the right approach. A bit of tuning and it would work like a charm. It's very important you scripted that thingy by yourself. Experience is invaluable. That proves AGS games would never stuck.

~Cheers

SMF spam blocked by CleanTalk