Skip to content

Instantly share code, notes, and snippets.

@coreh
Created February 18, 2013 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreh/4980837 to your computer and use it in GitHub Desktop.
Save coreh/4980837 to your computer and use it in GitHub Desktop.
How to calculate sprite coordinates by index in a sprite-sheet with fixed-sized sprites.
// These are known beforehand
int imageWidth = 640;
int imageHeight = 480;
int tileWidth = 32;
int tileHeight = 32;
int tilesPerRow = imageWidth / tileWidth;
int index = 5;
int left = tileWidth * (index % tilesPerRow);
int top = tileHeight * (index / tilesPerRow);
int bottom = top + tileHeight;
int right = top + tileWidth;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment