GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-03-23 14:03:41

paul23
Member
Registered: 2007-10-17
Posts: 110

sprite_create_from_sheet()

This little (header/argument list much larger than the code lol) script makes it possible to load "sprite-sheets" instead of "1d" strips you can now use a "2d" sheet: also it allows you to only load a part from the sheet.

Expand/*
**  usage:
**    sprite_create_from_sheet(file,n,nrow,sprite_width,sprite_height,
**                             horcelloff,vercelloff,horpixoff,verpixoff,
**                             horseperation,verseperation,
**                             precise,transparent,smooth,xoffset,yoffset)
**    Basically it's a combination from sprite_add() and the strip-importer
**  
**  arguments:
**    file           File to load from (not animated)
**    n              Number of images
**    nrow           Number of images/row
**    sprite_width   Width of the sprite
**    sprite_height  Height of the sprite
**    horcelloff     Number of cells to skip horizontally
**    vercelloff     Number of cells to skip vertically
**    horpixoff      Number of pixels to skip horizontally
**    verpixoff      Number of pixels to skip vertically
**    horseperation  Horizontal seperation between images (in pixels)
**    verseperation  Vertical seperation between images (in pixels)
**    precise        Wether precise collision is on/off
**    transparant    Wether the image is (partially) transparant
**    smooth         Wether the edges should be smoothed
**    xoffset        X-offset of the origin in the sprite
**    yoffset        Y-offset of the origin in the sprite
**    
**  retruns:
**    sprite         New sprite index
**
**  Notes:
**    Preload is set to false
*/

var f,n,nrow,sprw,sprh,hcoff,vcoff,hpoff,vpoff,hsep,vsep;
var prec,trans,smooth,pre,xoff,yoff;
var tspr, newspr, X, Y, maxX, begX;
f     = argument0;
n     = argument1;
nrow  = argument2;
sprw  = argument3;
sprh  = argument4;
hcoff = argument5;
vcoff = argument6;
hpoff = argument7;
vpoff = argument8;
hsep  = argument9;
vsep  = argument10
prec  = argument11;
trans = argument12;
smooth= argument13;
xoff  = argument14;
yoff  = argument15;

tspr  = sprite_add(f,1,false,false,false,true,0,0);
if (tspr == -1) {
    return -1;
}
draw_sprite(tspr,0,0,0);
begX = hcoff*sprw+hpoff;
X= begX;
Y=vcoff*sprh+vpoff;
maxX = begX+nrow*(sprw+hsep);
newspr = sprite_create_from_screen(X,Y,sprw,sprh,
                                    prec,trans,smooth,false,xoff,yoff);
                                             //Preload is set to false
repeat (n-1) {
    X+= sprw+hsep;
    if (X>=maxX) {
        X = begX;
        Y+= sprh+vsep;
    }
    sprite_add_from_screen(newspr,X,Y,sprw,sprh);
}
sprite_delete(tspr);
return newspr;

What I tried to achieve was a combination of both "sprite_add" and the build in "create sprite from strip" (from inside the sprite editor).

It has 1 drawback: preload is hard-coded set to false. The reason for this is simply a GM limit: you can't pass more than 16 arguments to a script >.<. - I chose preload since that's from what I heard least important. Another thing I could've done is remove either horpixoff or horcelloff.. (both are actually the same, but just another way of saying it): but since those are also there in the strip-editor.

Also a small "bug" (it's a feature really!) is that for example you have a sprite sheet with a seperation of "1", and you want to start at the second line, you ALSO have to set the vertical pixel offset to 1. - This is not a bug, but a feature: I purposely let that stay there since the strip-editor behaves exactly the same!

What do you think about my 2 choices - I tried to mimic the editor so it's more user friendly..

Small update: added a version which can handle images with alpha channels:

Expand/*
**  usage:
**    sprite_create_from_sheet_alpha(file,n,nrow,sprite_width,sprite_height,
**                                   horcelloff,vercelloff,horpixoff,verpixoff,
**                                   horseperation,verseperation,
**                                   precise,smooth,xoffset,yoffset)
**    Basically it's a combination from sprite_add_alpha() and the strip-importer
**  
**  arguments:
**    file           File to load from (not animated)
**    n              Number of images
**    nrow           Number of images/row
**    sprite_width   Width of the sprite
**    sprite_height  Height of the sprite
**    horcelloff     Number of cells to skip horizontally
**    vercelloff     Number of cells to skip vertically
**    horpixoff      Number of pixels to skip horizontally
**    verpixoff      Number of pixels to skip vertically
**    horseperation  Horizontal seperation between images (in pixels)
**    verseperation  Vertical seperation between images (in pixels)
**    precise        Wether precise collision is on/off
**    smooth         Wether the edges should be smoothed
**    xoffset        X-offset of the origin in the sprite
**    yoffset        Y-offset of the origin in the sprite
**    
**  retruns:
**    sprite         New sprite index
**
**  Notes:
**    Preload is set to false
*/

var f,n,nrow,sprw,sprh,hcoff,vcoff,hpoff,vpoff,hsep,vsep;
var prec,trans,smooth,pre,xoff,yoff;
var tspr, tsurf, newspr, X, Y, dX, dY;
f     = argument0;
n     = argument1;
nrow  = argument2;
sprw  = argument3;
sprh  = argument4;
hcoff = argument5;
vcoff = argument6;
hpoff = argument7;
vpoff = argument8;
hsep  = argument9;
vsep  = argument10
prec  = argument11;
smooth= argument12;
xoff  = argument13;
yoff  = argument14;

tspr  = sprite_add_alpha(f,1,false,true,0,0);
if (tspr == -1) {
    return -1;
}

X= 0;
Y= 0;
dX = nrow*(sprw+hsep);
dY = ceil(n/nrow)*(sprh+hsep);

tsurf = surface_create(dX,dY);
if (tsurf == -1) {
    return -1;
}
surface_set_target(tsurf);
    draw_clear_alpha(c_white,1);
    draw_set_blend_mode_ext(bm_zero,bm_src_color);
    draw_sprite(tspr,0,-hcoff*sprw-hpoff,-vcoff*sprh-vpoff);
    draw_set_blend_mode(bm_normal);
surface_reset_target();

newspr = sprite_create_from_surface(tsurf,X,Y,sprw,sprh,
                                    prec,false,smooth,false,xoff,yoff);
                                             //Preload is set to false
repeat (n-1) {
    X+= sprw+hsep;
    if (X>=dX) {
        X = 0;
        Y+= sprh+vsep;
    }
    sprite_add_from_surface(newspr,tsurf,X,Y,sprw,sprh);
}
surface_free(tsurf);
sprite_delete(tspr);

return newspr;

Last edited by paul23 (2009-03-23 19:23:46)

Offline

Board footer

Powered by FluxBB