1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import java.awt.image.BufferedImage; import java.io.InputStream; import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12;
import test.Test;
public class Spritesheet {
protected BufferedImage spritesheetImages; protected int spriteSize; protected int texturesIndex; protected int textureID; public int[] textures; public Spritesheet(InputStream input, int size) { this.spritesheetImages = getImageFromStream(input); if (spritesheetImages.getWidth() % 2 == 0 & spritesheetImages.getHeight() % 2 == 0){ System.out.println("Loaded a spritesheet that is a multiple of two. Continuing."); this.spriteSize = size; this.texturesIndex = ((spritesheetImages.getWidth() / spriteSize) * (spritesheetImages.getHeight() / spriteSize)); this.textureID = convertBufferedImageToID(spritesheetImages); this.textures = new int[texturesIndex]; this.loadAllPossibleImages(); }else{ System.err.println("Tried to load a spritesheet that is not a multiple of two!"); } } public void loadAllPossibleImages() { int currentIndex = 0; for(int i = spriteSize; i < spritesheetImages.getHeight(); i += spriteSize) { for(int j = 0; j < spritesheetImages.getWidth(); j += spriteSize) { textures[currentIndex] = convertBufferedImageToID(spritesheetImages.getSubimage(j, i, spriteSize, spriteSize)); System.out.println("Loaded sprite @ " + j + " & " + i + " & Width: " + spriteSize); currentIndex++; } } } public void drawSprite(float x, float y, int index) { GL11.glPushMatrix(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textures[index]); GL11.glTranslatef(x, y, 0); GL11.glTexCoord2f(0, 0); GL11.glVertex2f(0, 0); GL11.glTexCoord2f(1.0f, 0); GL11.glVertex2f(spriteSize, 0); GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex2f(spriteSize, spriteSize); GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex2f(0, spriteSize); GL11.glPopMatrix(); GL11.glBindTexture(GL11.GL_TEXTURE_2D, -1); } private int convertBufferedImageToID(BufferedImage image) { 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 >> 16) & 0xFF)); buffer.put((byte) ((pixel >> 8) & 0xFF)); buffer.put((byte) (pixel & 0xFF)); buffer.put((byte) ((pixel >> 24) & 0xFF)); } } buffer.flip(); int textureID = GL11.glGenTextures(); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); return textureID; } private BufferedImage getImageFromStream(InputStream input) { try{ return ImageIO.read(input); }catch(Exception ex){ System.out.println("Unable to load spritesheet from stream."); return null; } } } |