mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-12 14:51:51 +09:00
added sources for Slick
Former-commit-id: 1647fa32ef6894bd7db44f741f07c2f4dcdf9054 Former-commit-id: 0e5810dcfbe1fd59b13e7cabe9f1e93c5542da2d
This commit is contained in:
65
lib/slick-source/org/newdawn/slick/XMLPackedSheet.java
Normal file
65
lib/slick-source/org/newdawn/slick/XMLPackedSheet.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package org.newdawn.slick;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* A sprite sheet based on an XML descriptor generated from the simple slick tool
|
||||
*
|
||||
* @author kevin
|
||||
*/
|
||||
public class XMLPackedSheet {
|
||||
/** The full sheet image */
|
||||
private Image image;
|
||||
/** The sprites stored on the image */
|
||||
private HashMap sprites = new HashMap();
|
||||
|
||||
/**
|
||||
* Create a new XML packed sheet from the XML output by the slick tool
|
||||
*
|
||||
* @param imageRef The reference to the image
|
||||
* @param xmlRef The reference to the XML
|
||||
* @throws SlickException Indicates a failure to parse the XML or read the image
|
||||
*/
|
||||
public XMLPackedSheet(String imageRef, String xmlRef) throws SlickException
|
||||
{
|
||||
image = new Image(imageRef, false, Image.FILTER_NEAREST);
|
||||
|
||||
try {
|
||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
||||
Document doc = builder.parse(ResourceLoader.getResourceAsStream(xmlRef));
|
||||
|
||||
NodeList list = doc.getElementsByTagName("sprite");
|
||||
for (int i=0;i<list.getLength();i++) {
|
||||
Element element = (Element) list.item(i);
|
||||
|
||||
String name = element.getAttribute("name");
|
||||
int x = Integer.parseInt(element.getAttribute("x"));
|
||||
int y = Integer.parseInt(element.getAttribute("y"));
|
||||
int width = Integer.parseInt(element.getAttribute("width"));
|
||||
int height = Integer.parseInt(element.getAttribute("height"));
|
||||
|
||||
sprites.put(name, image.getSubImage(x,y,width,height));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new SlickException("Failed to parse sprite sheet XML", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a sprite by it's given name
|
||||
*
|
||||
* @param name The name of the sprite to retrieve
|
||||
* @return The sprite from the sheet or null if the name isn't used in this sheet
|
||||
*/
|
||||
public Image getSprite(String name) {
|
||||
return (Image) sprites.get(name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user