Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class SpriteSheet {
    private String path;
    public final int SIZE;
    public int[] pixels;

public static SpriteSheet tiles = new SpriteSheet("/texture/spritesheet.png", 256);

public SpriteSheet(String path, int size) {
    this.path = path;
    this.SIZE = size;
    pixels = new int[SIZE * SIZE];
    load();
}

private void load() {
    try {
        BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
        int w = image.getWidth();
        int h = image.getHeight();
        image.getRGB(0, 0, w, h, pixels, 0, w);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

So I'm trying to load a SpriteSheet either using slick2d or lwjgl. Was wondering is this faster, than the method above. Also, how do you actually do a spritesheet class for lwjgl.

I'm just confused how you use Sprites in Slick2d. It looks like I need to create an image class and then have SpriteSheet class extend the image class.

http://slick.cokeandcode.com/javadoc/org/newdawn/slick/Image.html

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Was wondering is this faster, than the method above.

It would have been better for you to post the SpriteSheet.java class from slick2D so we could compare them. I couldn't find the actual code to compare sorry. However, I don't believe it would really matter because you are essentially only loading the spritesheet once :)

I'm just confused how you use Sprites in Slick2d. It looks like I need to create an image class and then have SpriteSheet class extend the image class.

Check out some tutorials on loading images with slick2D Slick2D Game Tutorial

As for your last statement. It seems as though you would like to create your own sprite sheet class? If so, Then you can use the same method I use to retrieve my sprites from my sprite sheet. It's simple and effective.

 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;

 import javax.imageio.ImageIO;

 public class SpriteSheet {

//Integers
public static int TILE_SIZE = 32;

//Strings
private static String path = "res/SpriteSheet.png";

//Images
private static BufferedImage spriteSheet;

public static BufferedImage getSprite(int xGrid, int yGrid) {
    if (spriteSheet == null) {
        try {
            spriteSheet = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return spriteSheet.getSubimage(xGrid * TILE_SIZE, yGrid * TILE_SIZE, TILE_SIZE, TILE_SIZE);
  }
}

Above is my SpriteSheet.java class. In this class I have 3 Variables. - an int for the size of my tiles on the sprite sheet image. - a String for the path of my sprite sheet image. - a BufferedImage which will hold my sprite sheet image.

Luckily for us BufferedImage has a getSubImage(x, y, width, height). We use this to retrieve our sprite from the sprite sheet.

The way we retrieve our intended sprite is by accessing the SpriteSheet class statically with:

 SpriteSheet.getSprite(0, 0);

This returns the sprite at the top left corner which is xGrid co-ordinate 0 and yGrid co-ordinate 0.

I hope you get something from this. If not then I just wasted 20 minutes of my life lol.

enjoy

share|improve this answer
    
@simplicity- If the above answer or any other answer you have received on one of your posts is relevant and solves your problem, then you need to click the accept answer button on the left hand side of the answer. If you fail to do this people will be hesitant to answer your questions because it doesn't show much feedback on the help they provided. Thanks -savlon –  Savlon Jan 20 '13 at 6:52
    
Thanks for your answer. I'm probably going to do it your way, as it seems a lot more easier than using slick2d to work this out. I was under the impression that using slick2d to mainpulate sprites like say you had a sprite and your moving it across the screen or having the sprite do some animation, then it would be faster if you use slick2d. –  pangaea Jan 20 '13 at 18:39
    
Yeh that's cool, it's pretty simple to make your own sprite retriever class as you can see... It's good to use slick2D. However, I think you are better off knowing what actually happens behind the scenes. You will broaden your knowledge of both the java language and game design if you write your own classes :) good luck with everything, cheers –  Savlon Jan 21 '13 at 2:12

Slick2D is made to be easy to use. As far as I know the constructor of Spritesheet allows for a string literal so you should be able to just say:

Spritesheet sheet = new Spritesheet("path/to/spritesheet.whatever");

All a spritesheet is anyway is an image divided into smaller sub images. You can load the seperate images from the spritesheet by using

Image sprite = sheet.getSprite( whateverXposition, whateverYposition);

and then

sprite.draw();

As for the decision between lwjgl or slick2d that doesn't really make sense. lwjgl is a wrapper for OpenGL, which is nothing more than an API for communicating with your graphics card. If you're looking to animate a sprite slick2d's solution is going to be much easier for you I think than lwjgl, though exploration is always nice.

Something like:

Spritesheet s = new Spritesheet("something.png");
while( animateSprite == true )
{
    Image currentSprite = s.getSprite( currentX, currentY );
    currentSprite.draw();
    ...blah blah exit loop statements and logic...
}

You can loop across the values from the spritesheet and draw the image.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.