package com.tkraindesigns.gameutility; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.Log; /** * Spritesheet - holds a single bitmap which contains a sprite sheet. * Created by Brian on 7/5/2015. */ public class Spritesheet { /** * The bitmap holding the sprite sheet. */ private Bitmap bitmap; /** * Number of rows in the spritesheet */ private int rows; /** * Number of columns in the spritesheet */ private int columns; /** * Padding between columns in pixels */ private int xPadding; /** * Padding between rows in pixels */ private int yPadding; /** * Whitespace at top of sprite sheet, if any */ private int whiteTop; /** * Whitespace at bottom of spritesheet, if any */ private int whiteBottom; /** * Whitespace at right of spritesheet if any */ private int whiteRight; /** * Whitespace at left of spritesheet if any */ private int whiteLeft; /** *
A single bitmap holding a spritesheet arranged in rows and columns.
* Use this constructor when there is no padding or whitespace between tiles. * @param bmap The bitmap to use * @param cols Number of columns * @param rws Number of rows */ @SuppressWarnings("unused") public Spritesheet (Bitmap bmap, int cols, int rws) { super(); bitmap=bmap; Log.e("Spritesheet",String.valueOf(bitmap.getByteCount())); columns=cols; rows=rws; xPadding=0; yPadding=0; whiteTop=0; whiteBottom=0; whiteRight=0; whiteLeft=0; } /** *A single bitmap holding a spritesheet arranged in rows and columns
* Use this constructor when there is padding between tiles, but no whitespace around the tiles * @param bmap The bitmap to use * @param cols Number of columns * @param rws Number of rows. * @param xPad Padding in pixels between cells * @param yPad Padding in pixels between cells */ @SuppressWarnings("unused") public Spritesheet (Bitmap bmap, int cols, int rws, int xPad, int yPad) { super(); bitmap=bmap; columns=cols; rows=rws; xPadding=xPad; yPadding=yPad; whiteTop=0; whiteBottom=0; whiteRight=0; whiteLeft=0; } /** *A single bitmap holding a spritesheet arranged in rows and columns
* Use this constructor when there is padding between tiles, and whitespace around the tilesheet. * @param bmap The bitmap to use * @param cols Number of columns * @param rws Number of rows * @param xPad Padding in pixels between cells * @param yPad Padding in pixels between cells * @param wTop Whitespace on top of sheet * @param wBottom Whitespace on bottom of sheet * @param wRight Whitespace on right of sheet * @param wLeft Whitespace on left of sheet */ @SuppressWarnings("unused") public Spritesheet (Bitmap bmap, int cols, int rws, int xPad, int yPad, int wTop, int wBottom, int wRight, int wLeft) { super(); bitmap=bmap; columns=cols; rows=rws; xPadding=xPad; yPadding=yPad; whiteTop=wTop; whiteBottom=wBottom; whiteRight=wRight; whiteLeft=wLeft; } /** * @return The number of cells in the spritesheet (rows*columns) */ @SuppressWarnings("unused") public int size() { return rows*columns; } /** * Convenience function. This draws the sprite at the specified RectF on the provided canvas * @param canvas The canvas to draw on * @param frame Which cell to use * @param rect The destination RectF on the canvas * @param paint Paint to use with the function */ @SuppressWarnings("unused") public void draw(Canvas canvas, int frame, RectF rect, Paint paint) { canvas.drawBitmap(bitmap,fRect(frame),rect,null); } /** * Convenience function. This draws the sprite at the specified Rect on the provided canvas * @param canvas The {@link Canvas} to draw on * @param frame Which cell to use * @param rect The destination {@link RectF} on the canvas * @param paint {@link Paint} to use with the function */ @SuppressWarnings("unused") public void draw(Canvas canvas, int frame, Rect rect, Paint paint) { canvas.drawBitmap(bitmap, fRect(frame),rect, paint); } /** * Returns an individual frame from the spritesheet * @param frame Selected frame * @return Selected {@link Bitmap} */ @SuppressWarnings("unused") public Bitmap getFrame(int frame) { return Bitmap.createBitmap(bitmap,cellLeft(frame),cellTop(frame),cellwidth(),cellheight()); } private int whatrow(int frame) { return frame/columns; } private int whatcol(int frame) { return frame%columns; } private int cellwidth() { return (bitmap.getWidth()-whiteRight-whiteLeft- xPadding*columns)/columns; } private int cellheight() { return (bitmap.getHeight()-whiteTop-whiteBottom- yPadding*rows)/rows; } private int cellLeft(int frame) { return whiteLeft+(cellwidth()+xPadding)*whatcol(frame); } private int cellRight(int frame) { return cellLeft(frame)+cellwidth(); } private int cellTop(int frame) { return whiteTop+(cellheight()+yPadding)*whatrow(frame); } private int cellBottom(int frame) { return cellTop(frame)+cellheight(); } private Rect fRect(int frame) { return new Rect(cellLeft(frame),cellTop(frame),cellRight(frame),cellBottom(frame)); } }