简体   繁体   中英

Reading images from a sprite sheet Java

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.

要访问sheet.png中的特定图像,可以使用sprite [rowNum * cols + colNum]。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM