So, is there a quick-and-easy way to go about this?
Here’s what I have so far:
public TextureSheet(String path, int id, int tw, int th) {
try {
sheet = ImageIO.read(TextureSheet.class.getClassLoader().getResourceAsStream(path));
int rows = sheet.getHeight() / th;
int cols = sheet.getWidth() / tw;
BufferedImage[] sprites = new BufferedImage[rows * cols];
for (int x = 0; x < rows; x++) {
for (int y = 0; y < cols; y++) {
sprites[x * y] = sheet.getSubimage(x * tw, y * th, tw, th);
}
}
for (int i = 0; i < sprites.length; i++) {
BufferedImage image = sprites[i];
int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 24) & 0xFF)); // alpha
buffer.put((byte) ((pixel >> 16) & 0xFF)); // red
buffer.put((byte) ((pixel >> 8) & 0xFF)); // green
buffer.put((byte) (pixel & 0xFF)); // blue
}
}
buffer.flip();
//TODO Add code to convert ByteBuffer to OpenGL Texture
}
} catch (IOException e) {
e.printStackTrace();
}
}
I’ll keep looking into this on my own and will update this thread with the solution if I find one.
Thanks in advance,
| Nathan
p.s. For some reason the formatting gets messed up when I paste in from Eclipse; it looks perfect in there. Oh well.