GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2007-11-11 02:01:52

EyeGuy
Member
Registered: 2007-10-18
Posts: 19

draw_sprite_tiled_area[_ext] - slight improvement

I don't know if this is the proper place since it's my own script and the changes are somewhat minor and the scripts kinda out-dated since surfaces have been introduced, but this has always been a real old script of mine that's I thought I could improve/understand better.  Also, I got xscale/yscale working on the _ext version, though scaled down tiled images don't look the smoothest.

/*
**  Usage:
**      draw_sprite_tiled_area(sprite,subimg,x,y,x1,y2,x2,y2)
**
**  Arguments:
**      sprite  the sprite to be drawn
**      subimg  the sub image of the sprite to be drawn
**      x,y     the offset of the tiled area, as defined by a point
**              the sprite will/would be drawn
**      x1,y1   top left corner of the rectangle defining the area
**      x2,y2   bottom right corner of the area
**
**  Notes:
**      (x,y) doesn't have to be in the area, but if it is, then some
**          drawn sprite will have it's origin at this point
**
**  GMLscripts.com
*/
{
    var sprite,subimg,xx,yy,x1,y1,x2,y2;
    sprite = argument0;
    subimg = argument1;
    xx = argument2 + sprite_get_xoffset(sprite);
    yy = argument3 + sprite_get_yoffset(sprite);
    x1 = min(argument4,argument6);
    y1 = min(argument5,argument7);
    x2 = max(argument4,argument6);
    y2 = max(argument5,argument7);

    var sw,sh,i,j,jj,left,top,width,height,X,Y;
    sw = sprite_get_width(sprite);
    sh = sprite_get_height(sprite);

    i = x1 - (((x1 - xx) mod sw + sw) mod sw);
    j = y1 - (((y1 - yy) mod sh + sh) mod sh);
    jj = j;

    for(i=i; i<=x2; i+=sw) {
        for(j=j; j<=y2; j+=sh) {
            if(i <= x1) left = x1-i;
            else left = 0;
            X = i+left;

            if(j <= y1) top = y1-j;
            else top = 0;
            Y = j+top;

            if(x2 <= i+sw) width = 1+x2-i-left;
            else width = sw-left;

            if(y2 <= j+sh) height = 1+y2-j-top;
            else height = sh-top;

            draw_sprite_part(sprite,subimg,left,top,width,height,X,Y);
        }
        j = jj;
    }
}

Changes:
-automatically detects the lowest and highest points so you don't have to worry about it
-adds in the offset of the sprite to the x/y offset because draw_sprite_part ignores the offset
-the bulky i = ... and j = ... formula is replaced by one very similar to the angle_distance formula which is smaller and faster
-some redundant math in the if(...) width/height = .... lines gotten rid of

/*
**  Usage:
**      draw_sprite_tiled_area_ext(sprite,subimg,x,y,x1,y2,x2,y2,xscale,yscale,rotation,color,alpha)
**
**  Arguments:
**      sprite  the sprite to be drawn
**      subimg  the sub image of the sprite to be drawn
**      x,y     the offset of the tiled area, as defined by a point
**              the sprite will/would be drawn
**      x1,y1   top left corner of the rectangle defining the area
**      x2,y2   bottom right corner of the area
**      xs,ys   the xscale and yscale
**      color   the color mask that you want to blend the sprite with
**      alpha   the alpha value to draw it at
**
**  Notes:
**      (x,y) doesn't have to be in the area, but if it is, then some
**      drawn sprite will have it's origin at this point
**
**  GMLscripts.com
*/
{
    var sprite,subimg,xx,yy,x1,y1,x2,y2,xs,ys;
    sprite = argument0;
    subimg = argument1;
    xx = argument2 + sprite_get_xoffset(sprite);
    yy = argument3 + sprite_get_yoffset(sprite);
    x1 = min(argument4,argument6);
    y1 = min(argument5,argument7);
    x2 = max(argument4,argument6);
    y2 = max(argument5,argument7);
    xs = argument8;
    ys = argument9;
    if(xs == 0) xs = 1;
    if(ys == 0) ys = 1;

    var sw,sh,i,j,jj,left,top,width,height,X,Y;
    sw = sprite_get_width(sprite)*xs;
    sh = sprite_get_height(sprite)*ys;

    i = x1 - (((x1 - xx) mod sw + sw) mod sw);
    j = y1 - (((y1 - yy) mod sh + sh) mod sh);
    jj = j;

    for(i=i; i<=x2; i+=sw) {
        for(j=j ;j<=y2; j+=sh) {

            if(i <= x1) left = (x1-i);
            else left = 0;
            X = i+left;

            if(j <= y1)top = (y1-j);
            else top = 0;
            Y = j+top;

            if(x2 <= i+sw) width = 1+x2-i-left;
            else width = sw-left;

            if(y2 <= j+sh) height = 1+y2-j-top;
            else height = sh-top;

            draw_sprite_part_ext(sprite,subimg,left/xs,top/ys,width/xs,height/ys,X,Y,xs,ys,argument10,argument11);
        }
        j = jj;
    }
}

Changes:
-same as above
-slight changes to add in xscale and yscale

Offline

#2 2007-11-13 16:08:03

xot
Administrator
Registered: 2007-08-18
Posts: 1,201

Re: draw_sprite_tiled_area[_ext] - slight improvement

This the perfect place for this. Thanks for the update, I'll update the site as soon as I am able to.


Abusing forum power since 1986.

Offline

#3 2007-11-14 14:08:02

Quimp
Member
Registered: 2007-10-09
Posts: 15

Re: draw_sprite_tiled_area[_ext] - slight improvement

**  Notes:
**      x1 MUST be less than x2, and y1 less than y2
**      (x,y) doesn't have to be in the area, but if it is, then some
**          drawn sprite will have it's origin at this point

You mean its, not "it is" shortened as it's.

Last edited by Quimp (2007-11-14 14:09:14)

Offline

Board footer

Powered by FluxBB