MODULE(kinda): UtilityModule 0.1

Started by Calin Leafshade, Sat 08/05/2010 12:01:28

Previous topic - Next topic

Calin Leafshade



I was fiddling around with some basic functions I use alot and I compiled them into this module.

It does not yet comply with the conventions outlined for modules but It will do as I fix it over time.

Some of the things it does:

Fades guis (blocking and non blocking)

Clamp for ints & floats (truncates the values to a min and max)

'Rotates' ints and floats
Basically this allows you to iterate through a series of values just by adding to it so the following code:

In pseudo:
Code: ags

Loop{
x = RotateInt(x + 1, 0, 5);
print x;
}


would produce 012345012345012345...
The function also contains an 'Inclusive' flag which can be used for things like degrees of a circle in which 360 = 0;

SineWave
This function produces a self-advancing Sine oscillation. You can set the frequency, amplitude and (if using multiple waves for example) the Phase. This can be used for panning audio or just moving stuff in an oscillatory pattern.

A modulus function for floats.

Max and Abs

Setposition and FaceDir for characters

Various ViewFrame stuff by Monkey.

Some things I plan to add include:

Move function for Guis,
Fade for Objects and Characters,
Min (I also need a better way of doing Max...)
Reciprocal Clamp,
Various audio functions for the new audio system.

If anyone has any suggestions for other things to add please feel free to suggest them


-------------
Download
http://www.thethoughtradar.com/AGS/UtilityModule.zip


xenogia

Sounds great Calin, definetly gonna check it out.

Dualnames

#2
Calin, may I suggest adding functions for scrolling listboxes via sliders? Looks very impressive. Especially the SineWave!

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=40584.0

The code works in normal(RickJ's) and reverse order(My code at the bottom of the page). It's not hard at all to make a function out of it.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Calin Leafshade

Hmm that seems really quite specific Dualnames.. but i guess I am going to add various gui control stuff so yeah maybe i'll add it for the sake of it.

Milo87

This is exactly what I'm looking for! I've been poking about for a module that lets me clamp values for a while now, so cheers :)

subspark

I suggesting support for fading GUI Controls for fancy, smooth highlight effects.

Cheers,
Sparky.

Calin Leafshade

#6
unfortunately gui controls don't have a transparency property.. so thats not possible.

It *is* possible to do it using GUIs but the module already contains the functionality for that (like what is seen in Eternally Us on the title screen).

tzachs

Nice job, Calin!

I suggest putting there the utility methods used by the tween module (copied&pasted from the module source):

Interpolate values (amount between 0 and 1):
Code: ags

int Lerp(int from, int to, float amount) {
  return FloatToInt(IntToFloat(from) + IntToFloat(to - from) * amount, eRoundUp);
}


Convert from seconds to game loops:
Code: ags

int SecondsToLoops(float timeInSeconds) {
  return FloatToInt(IntToFloat(GetGameSpeed()) * timeInSeconds, eRoundNearest);
}


Get R/G/B values from an AGS color:
Code: ags

int GetRFromColor(int color)
{
  float floatColor = IntToFloat(color);
  int result = FloatToInt(floatColor / 2048.0) * 8;  
  return result;
}

int GetGFromColor(int color)
{
  float floatColor = IntToFloat(color);
  int result = FloatToInt(( floatColor - IntToFloat(FloatToInt(floatColor / 2048.0) * 2048)) / 64.0) * 8;  
  return result;
}

int GetBFromColor(int color)
{  
  float floatColor = IntToFloat(color);
  
  float withoutR = floatColor - IntToFloat(FloatToInt(floatColor / 2048.0) * 2048);
  int withoutRInt = FloatToInt(withoutR);
  float withoutG = withoutR - IntToFloat(FloatToInt(withoutR / 64.0) * 64); 
  int withoutGInt = FloatToInt(withoutG);
  int result = withoutGInt * 8;
  if (result > 255)
  {
    result = (withoutGInt - 31) * 8 - 1;
  }
    
  return result;  
}


Also, I suggest a method to convert from room co-ordinates to screen co-ordinates and vice versa.

Calin Leafshade


Wonkyth

Good stuff.
The problem with many Modules is that there is alot of functions that will never get used, so this is ok!
"But with a ninja on your face, you live longer!"

Calin Leafshade

Yea agreed.

The main point of this module I suppose is as kind of a general utility that you always have in your game just by default.

Something with little to no overhead which just augments AGSs basic features and adds a few absent ones.

Wonkyth

With that in mind, be careful what you add to it.
"But with a ninja on your face, you live longer!"

Crimson Wizard

#12
Here's my little contribution. Discuss?
Code: ags

// String.IndexOfInstance extender function
// Seeks for Nth instance of given substring sample in the string, starting from the given character

int IndexOfInstance(this String*, String needle, int instance, int from)
{
  int i = from;
  int char_match;
  int instance_found;
  int found_from = -1;
  
  while (i < this.Length)
  {
    if (this.Chars[i] == needle.Chars[char_match])
    {
      if (found_from == -1) found_from = i;
      char_match++;
      if (char_match == needle.Length)
      {
        if (instance_found == instance)
        {
          return found_from;
        }
        
        instance_found++;        
        char_match = 0;
        found_from = -1;
      }
    }
    else
    {
      char_match = 0;
      found_from = -1;
    }
    
    i++;
  }
  
  return -1;
}


This may be useful IMO if you have a long (or potentially long) String with lots of instances of the same sample and do not want to spawn substrings to find the Nth instance.

Crimson Wizard

Quote from: Crimson Wizard on Sun 20/06/2010 08:26:11
Here's my little contribution.

I actually found there's a StringPlus module that has this kind of function (and more).
So I guess better not spawn copies of the same thing.

Calin Leafshade

Yea, I meant to mention that the StringPlus module kinda has string stuff covered but forgot.

in other news:

I've actually added a HSL to RGB and back again function for pixel effects but it hasnt been fully tested yet.. Will uploaded once tested.

Ryan Timothy B

#15
Hey Calin.

I figure this is probably the best place to post this since it is a Utility Module. I just discovered an easier way to find out if a number is odd or even. My old method was simply just: if (Number % 2).

But I imagine this works much faster:
Code: ags

bool odd(int Number)
{
  return (Number & 1);
}


Apparently it just checks the last bit. No idea what it's doing exactly, but I tested it in AGS and it does work. :P

Calin Leafshade

yup that works.. there are 10 kinds of people.. those who understand binary and those who dont.

since the last bit is the only binary bit with an odd value you can tell if the entire number is odd or even from it

SMF spam blocked by CleanTalk