LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Silas on January 27, 2013, 04:17:16

Title: Is this a good way to load Sprites?
Post by: Silas on January 27, 2013, 04:17:16
I've been messing with the LWJGL Libraries for a few weeks now, And I've recently started a larger project, which is a 2D Tilebased game.
I have setup everything but one fatal thing, Which is the heart of the type of game: Tile textures. I've made a Spritesheet class, However it doesnt seem to work whenever I try to load a sprite that it loads. Does anybody have any idea why it doesn't seem to be working?
Please don't link me to any other utilities (Namely the Slick libraries...) because I want to see what can be done with LWJGL Itself.

My spritesheet class seems to do nothing.
Also, Is this an efficient way of loading textures? Or is there a more efficient way of doing it?

Heres my Spritesheet class:


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;

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);
this.spriteSize = size;
this.texturesIndex = ((spritesheetImages.getWidth() / spriteSize) * (spritesheetImages.getHeight() / spriteSize)); //Maximum textures
this.textureID = convertBufferedImageToID(spritesheetImages); //Gets ID of the spritesheet as a whole.
this.textures = new int[texturesIndex]; //Creates an int index the size of the possible sprites in the image.
this.loadAllPossibleImages();
}

public void loadAllPossibleImages()
{
/** Starts loading at the bottom left sprite. */
for(int i = spriteSize; i < spritesheetImages.getHeight(); i += spriteSize)
{
for(int j = 0; j < spritesheetImages.getWidth(); j += spriteSize)
{
int currentIndex = 0; currentIndex++;
textures[currentIndex] = convertBufferedImageToID(spritesheetImages.getSubimage(j, i, spriteSize, spriteSize));
}
}
/** Ends loading at the top right sprite. */
}

public void drawSprite(float x, float y, int index)
{
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_TEXTURE_2D);
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.glDisable(GL11.GL_TEXTURE_2D);
GL11.glPopMatrix();
}

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); //4 for RGBA, 3 for RGB
       
        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.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;
}
}
}

Title: Re: Is this a good way to load Sprites?
Post by: Daslee on January 27, 2013, 09:44:42
Use this: http://www.youtube.com/watch?v=Xj_qZ3Cw08Q (http://www.youtube.com/watch?v=Xj_qZ3Cw08Q)