Saturday, June 20, 2015

slick - SpriteSheet in slick2d or lwjgl


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



Answer




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


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...