sprite_add_from_surface


Description

This function works in exactly the same way as sprite_create_from_surface only instead of creating a new sprite from the area of the indexed surface that you select, it adds the defined area of the surface as a new sub-image to a previously created sprite (this means that you cannot add it directly to a sprite from the resource tree, but rather only to one previously created from a surface, or one that has been duplicated from a resource sprite using sprite_duplicate).

It should be noted that the memory used when adding a sprite in this way will be larger than you may expect. This is because GameMaker Studio 2 will store the sprite as a texture page and it will also be stored in GPU memory, so the total memory will be larger than would be expected for an image file of the same sprite.


Syntax:

sprite_add_from_surface(index, surface, x, y, w, h, removeback, smooth);

Argument Description
index The index of the sprite to add the new image to.
surface The index of the surface from which the get the image.
x The x position to copy from.
y The y position to copy from.
w The width of the area to be copied (from the x position).
h The height of the area to be copied (from the y position).
removeback Indicates whether to make all pixels with the background colour (left-bottom pixel) transparent.
smooth Indicates whether to smooth the edges.


Returns:

N/A


Example:

spr_custom = sprite_create_from_surface(surf, 0, 0, 32, 32, true, true, 16, 16);
var i;
for (i = 1; i < 8; i +=1)
    {
    sprite_add_from_surface(spr_Custom, surf, i, 0, 32, 32, true, true, 16, 16);
    }

The above code creates a sprite from the surface indexed in the variable "surf", assigning its index to the variable "spr_Custom", and then uses a for loop to move across the surface and capture various sections which are added into the sprite as sub-images.