Reputation: 11
I want to use sprite sheets in my game and with the research I have done found this piece of code.
BufferedImage bigImg = ImageIO.read(new File("sheet.png"));
// The above line throws an checked IOException which must be caught.
final int width = 10;
final int height = 10;
final int rows = 5;
final int cols = 5;
BufferedImage[] sprites = new BufferedImage[rows * cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sprites[(i * cols) + j] = bigImg.getSubimage(
i * width,
j * height,
width,
height
);
}
}
I understand how this snippet will turn the sprite sheet into an array, but how do I access this array. Is it just sprites[i];
?
Also will it be possible to bind the loaded sprite into an OpenGL texture with
int spritename = glgentextures;
{
sprites[i];
}
Thanks in advance.
Upvotes: 0
Views: 2322
Reputation: 7957
To access a certain image in sheet.png you can use sprite[rowNum*cols + colNum].
Upvotes: 1