Dynamically adjusting ambient sound volume

Started by TheMagician, Sun 13/06/2004 17:21:15

Previous topic - Next topic

strazer

QuoteI find it very hard to think of an algorithm to set the sound

Try this:

#define clifftop 135
#define cliffbottom 230

Repeatedly_execute(_always)
  SetChannelVolume(1, (cliffbottom-player.y)/((cliffbottom-clifftop)/255));

TheMagician

@Pumaman:
The improvements are greatly apreciated! Thanks!

@Strazer:

Thanks for your work with the algorithm. However there is a fatal exception when that line of script is run ingame (basically as soon as you enter the screen). It says:

"Invalid: Integer devide by zero" .... or something like that ... seems as if (cliffbottom-clifftop)/255)   returns zero? When I type it into my calculator the result is 0.33333  ... perhaps AGS turns that into 0 ?
However, I also tried using (cliffbottom-clifftop)/125) but still the same error message.

Any ideas about that?

Scorpiorus

Dividing (cliffbottom-clifftop) by 255 is equal to multiplying (cliffbottom-player.y) by it, with a couple of checks we have:

int volume = (255*(cliffbottom-player.y))/(cliffbottom-clifftop);

if (volume > 255) volume = 255;
else if (volume < 0) volume = 0;

SetChannelVolume(1, volume);

Gilbert

Right, to have best results for integer arimetic, always try to arrange the calculations such taht divisions are carried out in the very last steps.

TheMagician

Finally it is working!!!!! :D (and by the way ... completely customizable).

Thanks to all you programming gurus. Thank you very much!

One last thing that came up during this topic:
what is the difference between

int clifftop = 135;

and

#include clifftop 135   ? 

strazer

I'm glad it's working now. Thanks a lot, people. :)

  int clifftop = 135;

declares a variable. But since the value is unlikely to change and this way it takes up memory (insignificant, but still), you can use

  #define clifftop 135

which will replace all instances of "clifftop" in the code with "135" once you compile the game. I don't know the exact term, but I would call it a constant.

TheMagician

Thanks strazer!

And thanks again to everybody who contributed.

SMF spam blocked by CleanTalk