/*
* Foogl (Friendly Object Oriented Game Library)
* Copyright (c) 2008 Wachirawut Thamviset.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package kku.cs.fgl;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;
/**
* �����ҧ�����ŵ��ҧ �红����� Sprite
* ����ҧ�֧ Sprite �����Ҿ���� SpriteCell
* @author Wachirawut Thamviset
*
*/
public class SpriteSheet {
/**
*
*/
private static final long serialVersionUID = 1L;
private Vector cells = new Vector();
/**
* �ӹǹ cell ��ǹ
*/
private int horizontalCount = 1;
/**
* �������ҧ�ͧ cell
*/
private int width;
/**
* �����٧�ͧ cell
*/
private int height;
private String name;
public SpriteSheet() {
}
/**
*
* @param image Image
* @param tw Tile Width
* @param th Tile Height
* @param spacing Tile Spacing
*/
public SpriteSheet(Image image, int tw, int th, int spacing) {
addSheet(image, tw, th, spacing);
}
public SpriteSheet(String img, int tw, int th) {
this(img, tw, th, null);
}
public SpriteSheet(String imgFile, int tw, int th, Color tcolor) {
try {
Image img = new Image(imgFile, tcolor);
addSheet(img, tw, th, 0);
} catch (SlickException e) {
}
}
public int add(Image srcImg) {
return add(srcImg, 0, 0, srcImg.getWidth(), srcImg.getHeight());
}
public int add(Image srcImg, int x1, int y1, int x2, int y2) {
SpriteCell f = new SpriteCell(srcImg, x1, y1, x2, y2);
cells.add(f);
width = Math.max(width, x2 - x1);
height = Math.max(height, y2 - y1);
return cells.size() - 1;
}
/**
* ���� cell ���� String "x,y,w,h"
*
* @param srcImg
* @param cellData
* @return
*/
public int add(Image srcImg, String cellData) {
int x1, y1, x2, y2;
String c[] = cellData.split(",");
x1 = Integer.parseInt(c[0]);
y1 = Integer.parseInt(c[1]);
x2 = x1 + Integer.parseInt(c[2]);
y2 = y1 + Integer.parseInt(c[3]);
return add(srcImg, x1, y1, x2, y2);
}
public void addSheet(Image image, int tw, int th, int spacing) {
horizontalCount = (image.getWidth() / (tw + spacing));
int verticalCount = (image.getHeight() / (th + spacing));
for (int y = 0; y < verticalCount; y++) {
for (int x = 0; x < horizontalCount; x++) {
int ix = x * (tw + spacing);
int iy = y * (th + spacing);
// System.out.println("add image "+ix+","+iy);
add(image, ix, iy, ix + tw, iy + th);
}
}
}
public int count() {
return cells.size();
}
public SpriteCell getCell(int cellNo) {
if(cellNo<0)return null;
if (cellNo < cells.size()) {
return (SpriteCell) cells.get(cellNo);
}
return null;
}
public int getHeight(int i) {
if (cells.size() > 0 && i < cells.size()) {
SpriteCell f = (SpriteCell) cells.get(i);
return f.getHeight();
}
return height;
}
public int getHorizontalCount() {
return horizontalCount;
}
public int getVerticalCount() {
return (int) Math.ceil(cells.size() / (double) horizontalCount);
}
public int getWidth(int i) {
if (i < cells.size()) {
SpriteCell f = (SpriteCell) cells.get(i);
return f.getWidth();
}
return width;
}
public SpriteCell[] getCells(){
SpriteCell c[]=new SpriteCell[count()];
cells.toArray(c);
return c;
}
/**
* Load �����Ũҡ text ��� �ٻẺ��� ��÷Ѵ�á�繪����Ҿ ��÷Ѵ������
* spritecell image x,y,w,h
*
* @param fileRef
*/
public void load(String fileRef) {
InputStream in = ResourceLoader.getResourceAsStream(fileRef);
if (in != null) {
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
String line = null;
try {
line = bin.readLine();
Image img = new Image(line);
while ((line = bin.readLine()) != null) {
line = line.trim();
if (line.length() > 0)
add(img, line);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void paint(Graphics g, int cellNo, int x, int y, boolean hflip,
boolean vflip, Color filter) {
if (cellNo >= 0 && cellNo < cells.size()) {
SpriteCell f = (SpriteCell) cells.get(cellNo);
f.paint(g, x, y, hflip, vflip, filter);
}
}
public void paint(Graphics g, int cellNo, int x, int y, Color filter) {
if (cellNo >= 0 && cellNo < cells.size()) {
SpriteCell f = (SpriteCell) cells.get(cellNo);
f.paint(g, x, y, filter);
}
}
public void paint(Graphics g, int cellX, int cellY, int x, int y,
Color filter) {
int cellNo = cellY * horizontalCount + cellX;
paint(g, cellNo, x, y, filter);
}
public void setHorizontalCount(int horizontalCount) {
if (horizontalCount <= 0)
horizontalCount = 1;
this.horizontalCount = horizontalCount;
}
}