Path: blob/master/SLICK_HOME/src/org/newdawn/slick/PackedSpriteSheet.java
1456 views
package org.newdawn.slick;12import java.io.BufferedReader;3import java.io.IOException;4import java.io.InputStreamReader;5import java.util.HashMap;67import org.newdawn.slick.util.Log;8import org.newdawn.slick.util.ResourceLoader;910/**11* A sprite sheet packed and defined by the Pacific Software Image Packer available12* from:13*14* http://homepage.ntlworld.com/config/imagepacker/15*16* @author kevin17*/18public class PackedSpriteSheet {19/** The image loaded for the sheet */20private Image image;21/** The base path where the image is expected to be found based on the original definition file */22private String basePath;23/** The section definitions */24private HashMap sections = new HashMap();25/** The filter used when loading the image */26private int filter = Image.FILTER_NEAREST;2728/**29* Create a new packed sprite sheet based on a ImagePacker definition file30*31* @param def The location of the definition file to read32* @throws SlickException Indicates a failure to read the definition file33*/34public PackedSpriteSheet(String def) throws SlickException {35this(def, null);36}3738/**39* Create a new packed sprite sheet based on a ImagePacker definition file40*41* @param def The location of the definition file to read42* @param trans The color to be treated as transparent43* @throws SlickException Indicates a failure to read the definition file44*/45public PackedSpriteSheet(String def, Color trans) throws SlickException {46def = def.replace('\\', '/');47basePath = def.substring(0,def.lastIndexOf("/")+1);4849loadDefinition(def, trans);50}5152/**53* Create a new packed sprite sheet based on a ImagePacker definition file54*55* @param def The location of the definition file to read56* @param filter The image filter to use when loading the packed sprite image57* @throws SlickException Indicates a failure to read the definition file58*/59public PackedSpriteSheet(String def, int filter) throws SlickException {60this(def, filter, null);61}6263/**64* Create a new packed sprite sheet based on a ImagePacker definition file65*66* @param def The location of the definition file to read67* @param filter The image filter to use when loading the packed sprite image68* @param trans The color to be treated as transparent69* @throws SlickException Indicates a failure to read the definition file70*/71public PackedSpriteSheet(String def, int filter, Color trans) throws SlickException {72this.filter = filter;7374def = def.replace('\\', '/');75basePath = def.substring(0,def.lastIndexOf("/")+1);7677loadDefinition(def, trans);78}7980/**81* Get the full image contaning all the sprites/sections82*83* @return The full image containing all the sprites/sections84*/85public Image getFullImage() {86return image;87}8889/**90* Get a single named sprite from the sheet91*92* @param name The name of the sprite to retrieve93* @return The sprite requested (image of)94*/95public Image getSprite(String name) {96Section section = (Section) sections.get(name);9798if (section == null) {99throw new RuntimeException("Unknown sprite from packed sheet: "+name);100}101102return image.getSubImage(section.x, section.y, section.width, section.height);103}104105/**106* Get a sprite sheet that has been packed into the greater image107*108* @param name The name of the sprite sheet to retrieve109* @return The sprite sheet from the packed sheet110*/111public SpriteSheet getSpriteSheet(String name) {112Image image = getSprite(name);113Section section = (Section) sections.get(name);114115return new SpriteSheet(image, section.width / section.tilesx, section.height / section.tilesy);116}117118/**119* Load the definition file and parse each of the sections120*121* @param def The location of the definitions file122* @param trans The color to be treated as transparent123* @throws SlickException Indicates a failure to read or parse the definitions file124* or referenced image.125*/126private void loadDefinition(String def, Color trans) throws SlickException {127BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResourceAsStream(def)));128129try {130image = new Image(basePath+reader.readLine(), false, filter, trans);131while (reader.ready()) {132if (reader.readLine() == null) {133break;134}135136Section sect = new Section(reader);137sections.put(sect.name, sect);138139if (reader.readLine() == null) {140break;141}142}143} catch (Exception e) {144Log.error(e);145throw new SlickException("Failed to process definitions file - invalid format?", e);146}147}148149/**150* A single section defined within the packed sheet151*152* @author kevin153*/154private class Section {155/** The x position of the section */156public int x;157/** The y position of the section */158public int y;159/** The width of the section */160public int width;161/** The height of the section */162public int height;163/** The number of sprites across this section */164public int tilesx;165/** The number of sprites down this section */166public int tilesy;167/** The name of this section */168public String name;169170/**171* Create a new section by reading the stream provided172*173* @param reader The reader from which the definition can be read174* @throws IOException Indicates a failure toread the provided stream175*/176public Section(BufferedReader reader) throws IOException {177name = reader.readLine().trim();178179x = Integer.parseInt(reader.readLine().trim());180y = Integer.parseInt(reader.readLine().trim());181width = Integer.parseInt(reader.readLine().trim());182height = Integer.parseInt(reader.readLine().trim());183tilesx = Integer.parseInt(reader.readLine().trim());184tilesy = Integer.parseInt(reader.readLine().trim());185reader.readLine().trim();186reader.readLine().trim();187188tilesx = Math.max(1,tilesx);189tilesy = Math.max(1,tilesy);190}191}192}193194195