Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Wyz on Fri 13/08/2010 19:45:46

Title: Useful script snippets
Post by: Wyz on Fri 13/08/2010 19:45:46
I don't know if there already exists a place to post useful pieces of script , if not then I'm creating it now. :)

A wrote a function that can turn any dynamic sprite into a graph:

void Graph(this DynamicSprite *, float value1, float value2, bool smooth)
{
 int y1 = (this.Height - 1) - FloatToInt(IntToFloat(this.Height - 1) * value1);
 int y2 = (this.Height - 1) - FloatToInt(IntToFloat(this.Height - 1) * value2);
 int y3 = ((y1 > y2) - (y1 < y2)) * smooth;
 
 this.Crop(1, 0, this.Width - 1, this.Height);
 this.ChangeCanvasSize(this.Width + 1, this.Height, 0, 0);
 
 DrawingSurface *surface = this.GetDrawingSurface();
 surface.DrawingColor = 15;
 surface.DrawLine(surface.Width - 1, y1, surface.Width - 1, y2 + y3);
 
 surface.Release();
}


It can be used like this:
(assuming that sprite is a dynamic sprite)

float last;

function room_RepExec()
{
 float current = IntToFloat(mouse.x) / IntToFloat(Room.Width);

 sprite.Graph(current, last, true);
 last = current;
}


You could also play with these:

 sprite.Graph(current, last, false);
 sprite.Graph(current, last, -1);

 sprite.Graph(current, 0.0, false);
 sprite.Graph(current, 1.0, false);

 sprite.Graph((current / 2.0) + 0.5, 0.5 - (last / 2.0), false);
Title: Re: Useful script snippets
Post by: Pumaman on Sun 15/08/2010 14:44:51
Thanks, what people usually do is create a script module with handy bits of script in, which then makes it easy for other people to use.
Title: Re: Useful script snippets
Post by: Wyz on Sat 28/08/2010 14:06:07
That's a good idea, I'll do so when I've collected a number of them so I can group them as something useful :D

In the meanwhile I'll post here so people can already use them.

Drawing extension: Brush/spray paint line


function BrushLine(this DrawingSurface *, int x1, int y1, int x2, int y2, int slot, float spread)
{
  x1 -= Game.SpriteWidth[slot] / 2;
  y1 -= Game.SpriteHeight[slot];
  x2 -= Game.SpriteWidth[slot] / 2;
  y2 -= Game.SpriteHeight[slot];
 
  float dx = IntToFloat(x2 - x1);
  float dy = IntToFloat(y2 - y1);
 
  float interval = Maths.Sqrt(dx*dx + dy*dy);
  interval = interval / (IntToFloat(Game.SpriteWidth[slot] + Game.SpriteHeight[slot]) / (spread * 2.0));
 
  if (interval < 1.0)
    interval = 1.0;
 
  float i = 0.0;
  float xoffset = dx / interval;
  float yoffset = dy / interval;
  float xx = IntToFloat(x1);
  float yy = IntToFloat(y1);
 
  while (i < interval)
  {
    this.DrawImage(FloatToInt(xx), FloatToInt(yy), slot, false);
    xx += xoffset;
    yy += yoffset;
    i++;
  }
}


Use like this:

DrawingSurface *surface; // assumed that it contains a valid surface

surface.BrushLine(10, 10, 100, 100, 33, 4.0);


The final parameter indicates the ratio of the sprite size and the core of the brush. 4.0 will usually do, but if your line has holes in them, increase this number.
Title: Re: Useful script snippets
Post by: monkey0506 on Sat 28/08/2010 19:58:10
bool IsModeEnabled(this Mouse*, CursorMode mode) {
 if (mode == this.Mode) return true;
 CursorMode prevMode = this.Mode;
 this.Mode = mode;
 if (this.Mode == prevMode) return false;
 this.Mode = prevMode;
 return true;
}