Best way to deal with offsets in texture atlases?

Monkey Forums/Monkey Programming/Best way to deal with offsets in texture atlases?

Grey Alien(Posted 2014) [#1]
Hi all, I'm using TexturePacker to make texture atlases and it has a trim function that can drastically reduce the overall sprite sheet size.

The texture atlas will have recorded offset x and y to tell me how much is trimmed off sprite.

So I'm wondering what the best way to handle drawing an image that I've grabbed from the sprite sheet.

I could:

a) Modify the handle of the image with the offset so that when I draw at x,y it's the same as if I drew the untrimmed image. I often midhandle my sprites (and rotate around that point) so would need to make sure that still works (any ideas how to use the offset correctly for midhandled sprites?) Sometimes though I just use a handle of 0,0 so I'm guessing I'd set the handle to be negative based on the offset. Sometimes I use a handle somewhere in the sprite so I'm guessing I subtract the offset from that handle? ... Phew, I think that's it.

b) record the offset x,y along with each image and adjust the drawing x,y accordingly when I draw them.

Seems like a) is probably the best way as it's using existing internal stuff rather than adding complexity.


ziggy(Posted 2014) [#2]
I would go for A.


dawlane(Posted 2014) [#3]
Option A) Set the image handle before rendering the sprite with DrawImageRect.
Store sprite animation frames in an array. This can double up for use in a collision detection routine.
atlasimage.SetHandle(sourceX+offsetX, sourceY+offsetY)
DrawImage(atlasimage, posX, posY, spritesourceX[frame], spritesourceY[frame], spritesourceWidth[frame], spritesourceHeight[frame])

Edit: You may be able to handle rotated sources with the over loaded version of DrawImageRect. If not try pushing the matrix translating to the posX, posY, rotating the matrix.


Grey Alien(Posted 2014) [#4]
OK thx. Heads up that the libGDX format in TexturePacker is bugged. The offset values are sometimes out by a couple of pixels. Switching to xml format which seems to be correct. Sigh.