I'm trying to load this spritesheet into an array of buffered image (each sprite into one BufferedImage) :
I opened this file on photoshop. The width is 500 and the height is 666. So according to my calculation, I need to loop 64 times (8 rows and 8 columns) and for each sprite, its width is 500/8 (62.5) and its height is 666/8(83.25). Since getSubImage accepts only int parameters, I was forced to put the width as 62 and the height a 83 (and I think this is why it truncates my images).
Here's the code to load the sprites (I put them in a JFrame to show you the results).
public class Test{
public static void main(String [] args){
BufferedImage[] sprites = null;
int width = 62;
int height = 83;
try {
BufferedImage buff = ImageIO.read(Test.class.getResourceAsStream("cyclop.gif"));
sprites = new BufferedImage[64];
int rows = 8;
int cols = 8;
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
sprites[(i * cols) + j] = buff.getSubimage(i * width, j * height, width, height);
}
}
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
for(BufferedImage bf : sprites)
frame.getContentPane().add(new JLabel(new ImageIcon(bf)));
frame.pack();
frame.setVisible(true);
}
}
Which outputs :
I'm a bit lost (first time I do this) on how I can load every sprite in a BufferedImage. Any ideas?
Here is a very quickly written (and probably dirty) C solution to locate self-containing, non-overlapping sprites. I used a PNG reader to get the data in RGBA format and checked only alpha.
It works like this:
minx,y
has to be increased by 1 andmaxx,y
decreased -- but that happens in the coordinates-to-size calculation.Sprite rectangles may not overlap! (They don't, in this image, but better make sure it happens nowhere else.)
The output list could be sorted on x,y coordinates for clarity; I'm going to leave that, and translating this to Java, to you. This program assumes there may be 64 or less images in a single sheet (
struct bounds_t[64]
); make sure it's as large as necessary.C-code (obvious PNG library structures and headers left out):
Basically the logic in you
for-loop
is wrong...You are multipling the
width
by the current row (i
) and theheight
by the current col (j
)It should be more like...
(I increased the height to row 95 for the example)
Now, obviously, you have an issue, as the sprites are different sizes, yea for you. I would suggest creating a simple look up file which contains the row/column as the key and the width/height of the cell, possibly even the x/y, so you can simply pick the sprite straight out, but that's up to you...