Skip to content

SpriteAtlas

Michael edited this page Jan 30, 2024 · 5 revisions

GameMaker will atlas your sprites into texture pages for you when you build your game, but sometimes you might want to be able to do this at runtime, e.g. with sprites loaded from files or supplied by the user. Here's a bit of code to do that for you!

The algorithm currently isn't perfect in its efforts to minimize the amount of space needed, but I consider it good enough for everything I've needed it for. If I need a more robust version in the future I'll refine it further.

Relevant Links

It's on Itch:

API

It's pretty simple.

sprite_atlas_pack(sprite_array, padding, *stride, *force_po2)

Returns: array of structs (see description)

Parameter Type Description
sprite_array array of sprite assets An array of all of the sprites which you would like to pack. If a sprite has multiple subimages, they will all be packed.
padding number (integer) The amount of padding space between sprites (I recommend a value of 2)
stride number (integer) The precision at which the system will attempt to pack sprites; see below for an explanation. Optional; defaults to 4
force_po2 boolean Whether or not the output image should be a power of 2 in size. Optional; defaults to false

The only important function in the whole thing (unless you prefer to use the DLL version). Pass in an array of sprites, and the function will attempt to pack them into a rectangular atlas.

The stride value determines the precision at which the system will attempt to pack sprites. Lower values mean sprites may end up closer together, but will take much longer. Higher values will be faster, but may waste space.

This function will return a struct, which contains a few things.

Member Type Description
atlas sprite A new sprite containing all of the packed input sprites (will be approximately a square)
uvs array of structs An array containing metadata about each packed sprite

atlas will be the output sprite. This might be all you need, but you will probably want to know where on the atlas each sprite ended up. uvs contains an array of structs which contain relevant data about the location of each sprite.

Member Type Description
sprite sprite The original sprite
subimages number The subimage of the original sprite (in the case that the sprite had more than one)
x number The x coordinate on the texture atlas where the sprite is located (in pixel coordinates)
y number The y coordinate on the texture atlas where the sprite is located (in pixel coordinates)
w number The width of the sprite (in pixel coordinates)
h number The height of the sprite (in pixel coordinates)

sprite_atlas_pack_dll(sprite_array, padding, *borders, *force_po2)

The same function as above, but executed in a DLL. This should be much faster. Only works on Windows. The inputs and outputs are the same.

sprite_atlas_version()

Returns: n/a

Prints out a string showing the version number of the extension (both the DLL and the GML).

Clone this wiki locally