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);
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.
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.
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;
}