0

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.

Voight_
  • 11
  • 3

1 Answers1

1

To access a certain image in sheet.png you can use sprite[rowNum*cols + colNum].

tibtof
  • 7,857
  • 1
  • 32
  • 49
  • Thanks, can I just use this call in the glgentextures as I showed? – Voight_ May 15 '12 at 17:05
  • Okay, I tried multiple ways but cannot get it to work, anyone got some code that I could read and learn how to do it properly, so I have some clue of how to do it. – Voight_ May 15 '12 at 21:20
  • Did you tried TextureIO.newTexture(sprite[i], false)? Also, be careful about the sprites dimensions, as this code works only for images split into 25 10x10px images. – tibtof May 16 '12 at 08:36