With this function you can create a sprite from a previously initialised surface (the surface index ID value is returned when you create the surface using surface_create()). The x and y coordinates that you then input in the function should be relative to the (0,0) position of the surface (the top left corner) and not the game window (nor the view if you have one active). The width and height arguments are in pixels and define the width and height of the part of the surface to use.
Setting the "removeback" argument to true will remove a colour from the sprite, by checking the bottom left pixel of the sprite for the colour there and then using that as the colour to be removed. When "removeback" is enabled, the alpha channel of the surface is ignored, meaning all pixels other than the "background colour" ones become fully opaque.
If you choose the "removeback" option, you may also want GameMaker to smooth the edges of the sprite by setting the "smooth" argument to true. All this does is create a semi-transparent border around the edges of the sprite after it has had its background removed.
Finally you can also specify the x and y origin for the sprite. This is the point where the sprite is "fixed" onto the instance that uses it, and is always calculated as relative to the 0,0 top left corner of one sprite sub-image. So, for example, a sprite that is 32 x 32 pixels with these values set to (16,16) will have its origin in the center.
By default all new sprites have their bounding boxes calculated automatically (the exact bbox will depend on the size and transparency of the sprite), however you may wish to customise this, in which case you should also use the function sprite_collision_mask().
NOTE When you create a sprite in GameMaker with this method you must remember to remove it again (with sprite_delete()) when no longer needed, otherwise there is risk of a memory leak which will slow down and eventually crash your game.
NOTE This function will only work with surfaces that use the surface_rgba8unorm (default) surface format.
sprite_create_from_surface(index, x, y, w, h, removeback, smooth, xorig, yorig);
Argument | Type | Description |
---|---|---|
index | Surface | The index of the surface to create from. |
x | Real | The x position to copy from. |
y | Real | The y position to copy from. |
w | Real | The width of the area to be copied (from the x position). |
h | Real | The height of the area to be copied (from the y position). |
removeback | Boolean | Indicates whether to make all pixels with the background colour (left-bottom pixel) transparent. |
smooth | Boolean | Indicates whether to smooth the edges. |
xorig | Real | Indicates the x position of the origin in the sprite. |
yorig | Real | Indicates the y position of the origin in the sprite. |
var surf;
surf = surface_create(32, 32);
surface_set_target(surf);
draw_clear_alpha(c_black, 0);
draw_sprite(spr_Body, 0, 0, 0);
draw_sprite(spr_Clothes, 0, 0, 0);
draw_sprite(spr_Hair, 0, 0, 0);
spr_custom = sprite_create_from_surface(surf, 0, 0, 32, 32, true, true, 16, 16);
surface_reset_target();
surface_free(surf);
The above code creates a surface and stores its index in the local variable "surf". It then targets that surface, clears it and draws several sprites on top of each other. Finally it creates a new sprite from the composite image drawn on the surface and assigns its index to "spr_Custom" before freeing up the memory used by the surface.