For example I have 128px wide sprite sheet, with 32x32 sprites on it. How do I get only one sprite ?
public static BufferedImage[][] divideImage2D(BufferedImage img,int divideWidth,int divideHeight) {
try {
int a=img.getWidth()/divideWidth,b=img.getHeight()/divideHeight;
BufferedImage[][] images=new BufferedImage[b][a];
for(int they=0;they<b*divideHeight;they+=divideHeight) {
for(int thex=0;thex<a*divideWidth;thex+=divideWidth) {
images[they/divideHeight][thex/divideWidth]=img.getSubimage(thex,they,divideWidth,divideHeight);
}}
return images;
}catch(Exception e) {e.printStackTrace();return null;}
}
public static BufferedImage[] divideImage(BufferedImage img,int divideWidth,int divideHeight) {
try {
int a=img.getWidth()/divideWidth,b=img.getHeight()/divideHeight;
BufferedImage[] images=new BufferedImage[a*b];
int i=0,thex=0;
for(int they=0;i<images.length;) {
if(i<a*b+1){
images[i]=img.getSubimage(thex,they,divideWidth,divideHeight);
i++;thex+=divideWidth;
if(thex==img.getWidth()) {
they+=divideHeight;
thex=0;
}}
}
return images;
}catch(Exception e) {e.printStackTrace();return null;}
}
This would work for regular java2D, but how to use BufferedImage in LWJGL ?
Not (really) related to LWJGL but the basic concept of sprite sheets is that you divide the image into each particular sprite (for example diving the sheet into 16x16 squares because your sprites are that size). Then you would load the texture regions into an array/list of some sort, and to access something you would just request an index of that array. Pretty intuitive, and it’s always good to know what’s going on in the background.
In LibGDX it’s super simple, goes something like this.
TextureRegion[][] tiles = TextureRegion.split(new Texture(Gdx.files.internal("tiles/sheet.png")), tileWidth, tileHeight);
...
TextureRegion foo = tiles[row][col]; // notice it's row/col: y first, from upper left!
foo.draw(batch, x,y);

In LibGDX it’s super simple, goes something like this.
TextureRegion[][] tiles = TextureRegion.split(new Texture(Gdx.files.internal("tiles/sheet.png")), tileWidth, tileHeight); ... TextureRegion foo = tiles[row][col]; // notice it's row/col: y first, from upper left! foo.draw(batch, x,y);
I made a wrapper class for that (mostly because I wanted to access tilesize, tilesacross and tilesdown without using the length parameter. also I can access it via [x][y]): SpriteSheet
I’ve never used a spritesheet arranged in rows and columns, I use packed images. You can use TexturePacker for that (and optionally whitespace strips and/or rotates for better packing). This tool is run offline and produces a large image containing all the smaller ones and a text file describing each small image location in the large image. You can use this directly with libgdx via the TextureAtlas class or write something to parse the text file if not using libgdx. The bin packing TexturePacker does is pretty sophisticated, but it is very easy to use. It is as easy or easier than arranging things in rows and columns. I usually have sprites as Photoshop or Illustrator layers, which makes aligning frames easy, and then export them to individual images with a script. The packer can be run as part of your game during development, so the packed images are always up to date. Then your workflow is just: save images, run app.