Search code examples
javaslick2dsprite-sheet

Using a sprite sheet with different sized sprites in Slick2D


I searched through the API for SpriteSheet, but I couldn't find anything on how to make a sprite sheet with different sized sprites.

The sprite sheet that I'm using has a row of 16x16px tiles, a row of 24x24px tiles under it, a row of 8x8px under that, and so on.

Originally, not using Slick2D, I used BufferedImage.getSubimage() to obtain each sprite from a temporary BufferedImage of the sprite sheet. Is there a similar method here that I can use?


Solution

  • I don't believe there is a way to do a direct sub-image in the current version of the API, at least at the time of this writing.

    However, there are three possible options that I can think of (in addition to the option of just adding said method calls yourself - it's open source after all):

    1. You could instantiate several SpriteSheet objects from the same source Image, one for each Sprite size, if you really want to keep them in the same source file.
    2. You could take the Image instance, and call getSubImage on it to split the Image into three images, one for each size (24x24, 16x16, and so on). Then, from those sub-images, instantiate SpriteSheets.
    3. You could split the source file into separate files based on the size. That is, have your 24x24 sprite cells in one file, your 16x16 in another file, and so on.