Reading images from a sprite sheet Java

て烟熏妆下的殇ゞ 提交于 2019-12-25 01:23:47

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10604824/reading-images-from-a-sprite-sheet-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!