Path: blob/master/SLICK_HOME/src/org/newdawn/slick/SpriteSheet.java
1456 views
package org.newdawn.slick;12import java.io.InputStream;34import org.newdawn.slick.opengl.Texture;56/**7* A sheet of sprites that can be drawn individually8*9* @author Kevin Glass10*/11public class SpriteSheet extends Image {12/** The width of a single element in pixels */13private int tw;14/** The height of a single element in pixels */15private int th;16/** The margin of the image */17private int margin = 0;18/** Subimages */19private Image[][] subImages;20/** The spacing between tiles */21private int spacing;22/** The target image for this sheet */23private Image target;2425/**26* Create a new sprite sheet based on a image location27*28* @param image The image to based the sheet of29* @param tw The width of the tiles on the sheet30* @param th The height of the tiles on the sheet31*/32public SpriteSheet(Image image,int tw,int th) {33super(image);3435this.target = image;36this.tw = tw;37this.th = th;3839// call init manually since constructing from an image will have previously initialised40// from incorrect values41initImpl();42}4344/**45* Create a new sprite sheet based on a image location46*47* @param image The image to based the sheet of48* @param tw The width of the tiles on the sheet49* @param th The height of the tiles on the sheet50* @param spacing The spacing between tiles51* @param margin The magrin around the tiles52*/53public SpriteSheet(Image image,int tw,int th,int spacing,int margin) {54super(image);5556this.target = image;57this.tw = tw;58this.th = th;59this.spacing = spacing;60this.margin = margin;6162// call init manually since constructing from an image will have previously initialised63// from incorrect values64initImpl();65}6667/**68* Create a new sprite sheet based on a image location69*70* @param image The image to based the sheet of71* @param tw The width of the tiles on the sheet72* @param th The height of the tiles on the sheet73* @param spacing The spacing between tiles74*/75public SpriteSheet(Image image,int tw,int th,int spacing) {76this(image,tw,th,spacing,0);77}7879/**80* Create a new sprite sheet based on a image location81*82* @param ref The location of the sprite sheet to load83* @param tw The width of the tiles on the sheet84* @param th The height of the tiles on the sheet85* @param spacing The spacing between tiles86* @throws SlickException Indicates a failure to load the image87*/88public SpriteSheet(String ref,int tw,int th, int spacing) throws SlickException {89this(ref,tw,th,null,spacing);90}9192/**93* Create a new sprite sheet based on a image location94*95* @param ref The location of the sprite sheet to load96* @param tw The width of the tiles on the sheet97* @param th The height of the tiles on the sheet98* @throws SlickException Indicates a failure to load the image99*/100public SpriteSheet(String ref,int tw,int th) throws SlickException {101this(ref,tw,th,null);102}103104/**105* Create a new sprite sheet based on a image location106*107* @param ref The location of the sprite sheet to load108* @param tw The width of the tiles on the sheet109* @param th The height of the tiles on the sheet110* @param col The colour to treat as transparent111* @throws SlickException Indicates a failure to load the image112*/113public SpriteSheet(String ref,int tw,int th, Color col) throws SlickException {114this(ref, tw, th, col, 0);115}116117/**118* Create a new sprite sheet based on a image location119*120* @param ref The location of the sprite sheet to load121* @param tw The width of the tiles on the sheet122* @param th The height of the tiles on the sheet123* @param col The colour to treat as transparent124* @param spacing The spacing between tiles125* @throws SlickException Indicates a failure to load the image126*/127public SpriteSheet(String ref,int tw,int th, Color col, int spacing) throws SlickException {128super(ref, false, FILTER_NEAREST, col);129130this.target = this;131this.tw = tw;132this.th = th;133this.spacing = spacing;134}135136/**137* Create a new sprite sheet based on a image location138*139* @param name The name to give to the image in the image cache140* @param ref The stream from which we can load the image141* @param tw The width of the tiles on the sheet142* @param th The height of the tiles on the sheet143* @throws SlickException Indicates a failure to load the image144*/145public SpriteSheet(String name, InputStream ref,int tw,int th) throws SlickException {146super(ref,name,false);147148this.target = this;149this.tw = tw;150this.th = th;151}152153/**154* @see org.newdawn.slick.Image#initImpl()155*/156protected void initImpl() {157if (subImages != null) {158return;159}160161int tilesAcross = ((getWidth()-(margin*2) - tw) / (tw + spacing)) + 1;162int tilesDown = ((getHeight()-(margin*2) - th) / (th + spacing)) + 1;163if ((getHeight() - th) % (th+spacing) != 0) {164tilesDown++;165}166167subImages = new Image[tilesAcross][tilesDown];168for (int x=0;x<tilesAcross;x++) {169for (int y=0;y<tilesDown;y++) {170subImages[x][y] = getSprite(x,y);171}172}173}174175/**176* Get the sub image cached in this sprite sheet177*178* @param x The x position in tiles of the image to get179* @param y The y position in tiles of the image to get180* @return The subimage at that location on the sheet181*/182public Image getSubImage(int x, int y) {183init();184185if ((x < 0) || (x >= subImages.length)) {186throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);187}188if ((y < 0) || (y >= subImages[0].length)) {189throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);190}191192return subImages[x][y];193}194195/**196* Get a sprite at a particular cell on the sprite sheet197*198* @param x The x position of the cell on the sprite sheet199* @param y The y position of the cell on the sprite sheet200* @return The single image from the sprite sheet201*/202public Image getSprite(int x, int y) {203target.init();204initImpl();205206if ((x < 0) || (x >= subImages.length)) {207throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);208}209if ((y < 0) || (y >= subImages[0].length)) {210throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);211}212213return target.getSubImage(x*(tw+spacing) + margin, y*(th+spacing) + margin,tw,th);214}215216/**217* Get the number of sprites across the sheet218*219* @return The number of sprites across the sheet220*/221public int getHorizontalCount() {222target.init();223initImpl();224225return subImages.length;226}227228/**229* Get the number of sprites down the sheet230*231* @return The number of sprite down the sheet232*/233public int getVerticalCount() {234target.init();235initImpl();236237return subImages[0].length;238}239240/**241* Render a sprite when this sprite sheet is in use.242*243* @see #startUse()244* @see #endUse()245*246* @param x The x position to render the sprite at247* @param y The y position to render the sprite at248* @param sx The x location of the cell to render249* @param sy The y location of the cell to render250*/251public void renderInUse(int x,int y,int sx,int sy) {252subImages[sx][sy].drawEmbedded(x, y, tw, th);253}254255/**256* @see org.newdawn.slick.Image#endUse()257*/258public void endUse() {259if (target == this) {260super.endUse();261return;262}263target.endUse();264}265266/**267* @see org.newdawn.slick.Image#startUse()268*/269public void startUse() {270if (target == this) {271super.startUse();272return;273}274target.startUse();275}276277/**278* @see org.newdawn.slick.Image#setTexture(org.newdawn.slick.opengl.Texture)279*/280public void setTexture(Texture texture) {281if (target == this) {282super.setTexture(texture);283return;284}285target.setTexture(texture);286}287}288289290