Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/PackedSpriteSheet.java
1456 views
1
package org.newdawn.slick;
2
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStreamReader;
6
import java.util.HashMap;
7
8
import org.newdawn.slick.util.Log;
9
import org.newdawn.slick.util.ResourceLoader;
10
11
/**
12
* A sprite sheet packed and defined by the Pacific Software Image Packer available
13
* from:
14
*
15
* http://homepage.ntlworld.com/config/imagepacker/
16
*
17
* @author kevin
18
*/
19
public class PackedSpriteSheet {
20
/** The image loaded for the sheet */
21
private Image image;
22
/** The base path where the image is expected to be found based on the original definition file */
23
private String basePath;
24
/** The section definitions */
25
private HashMap sections = new HashMap();
26
/** The filter used when loading the image */
27
private int filter = Image.FILTER_NEAREST;
28
29
/**
30
* Create a new packed sprite sheet based on a ImagePacker definition file
31
*
32
* @param def The location of the definition file to read
33
* @throws SlickException Indicates a failure to read the definition file
34
*/
35
public PackedSpriteSheet(String def) throws SlickException {
36
this(def, null);
37
}
38
39
/**
40
* Create a new packed sprite sheet based on a ImagePacker definition file
41
*
42
* @param def The location of the definition file to read
43
* @param trans The color to be treated as transparent
44
* @throws SlickException Indicates a failure to read the definition file
45
*/
46
public PackedSpriteSheet(String def, Color trans) throws SlickException {
47
def = def.replace('\\', '/');
48
basePath = def.substring(0,def.lastIndexOf("/")+1);
49
50
loadDefinition(def, trans);
51
}
52
53
/**
54
* Create a new packed sprite sheet based on a ImagePacker definition file
55
*
56
* @param def The location of the definition file to read
57
* @param filter The image filter to use when loading the packed sprite image
58
* @throws SlickException Indicates a failure to read the definition file
59
*/
60
public PackedSpriteSheet(String def, int filter) throws SlickException {
61
this(def, filter, null);
62
}
63
64
/**
65
* Create a new packed sprite sheet based on a ImagePacker definition file
66
*
67
* @param def The location of the definition file to read
68
* @param filter The image filter to use when loading the packed sprite image
69
* @param trans The color to be treated as transparent
70
* @throws SlickException Indicates a failure to read the definition file
71
*/
72
public PackedSpriteSheet(String def, int filter, Color trans) throws SlickException {
73
this.filter = filter;
74
75
def = def.replace('\\', '/');
76
basePath = def.substring(0,def.lastIndexOf("/")+1);
77
78
loadDefinition(def, trans);
79
}
80
81
/**
82
* Get the full image contaning all the sprites/sections
83
*
84
* @return The full image containing all the sprites/sections
85
*/
86
public Image getFullImage() {
87
return image;
88
}
89
90
/**
91
* Get a single named sprite from the sheet
92
*
93
* @param name The name of the sprite to retrieve
94
* @return The sprite requested (image of)
95
*/
96
public Image getSprite(String name) {
97
Section section = (Section) sections.get(name);
98
99
if (section == null) {
100
throw new RuntimeException("Unknown sprite from packed sheet: "+name);
101
}
102
103
return image.getSubImage(section.x, section.y, section.width, section.height);
104
}
105
106
/**
107
* Get a sprite sheet that has been packed into the greater image
108
*
109
* @param name The name of the sprite sheet to retrieve
110
* @return The sprite sheet from the packed sheet
111
*/
112
public SpriteSheet getSpriteSheet(String name) {
113
Image image = getSprite(name);
114
Section section = (Section) sections.get(name);
115
116
return new SpriteSheet(image, section.width / section.tilesx, section.height / section.tilesy);
117
}
118
119
/**
120
* Load the definition file and parse each of the sections
121
*
122
* @param def The location of the definitions file
123
* @param trans The color to be treated as transparent
124
* @throws SlickException Indicates a failure to read or parse the definitions file
125
* or referenced image.
126
*/
127
private void loadDefinition(String def, Color trans) throws SlickException {
128
BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResourceAsStream(def)));
129
130
try {
131
image = new Image(basePath+reader.readLine(), false, filter, trans);
132
while (reader.ready()) {
133
if (reader.readLine() == null) {
134
break;
135
}
136
137
Section sect = new Section(reader);
138
sections.put(sect.name, sect);
139
140
if (reader.readLine() == null) {
141
break;
142
}
143
}
144
} catch (Exception e) {
145
Log.error(e);
146
throw new SlickException("Failed to process definitions file - invalid format?", e);
147
}
148
}
149
150
/**
151
* A single section defined within the packed sheet
152
*
153
* @author kevin
154
*/
155
private class Section {
156
/** The x position of the section */
157
public int x;
158
/** The y position of the section */
159
public int y;
160
/** The width of the section */
161
public int width;
162
/** The height of the section */
163
public int height;
164
/** The number of sprites across this section */
165
public int tilesx;
166
/** The number of sprites down this section */
167
public int tilesy;
168
/** The name of this section */
169
public String name;
170
171
/**
172
* Create a new section by reading the stream provided
173
*
174
* @param reader The reader from which the definition can be read
175
* @throws IOException Indicates a failure toread the provided stream
176
*/
177
public Section(BufferedReader reader) throws IOException {
178
name = reader.readLine().trim();
179
180
x = Integer.parseInt(reader.readLine().trim());
181
y = Integer.parseInt(reader.readLine().trim());
182
width = Integer.parseInt(reader.readLine().trim());
183
height = Integer.parseInt(reader.readLine().trim());
184
tilesx = Integer.parseInt(reader.readLine().trim());
185
tilesy = Integer.parseInt(reader.readLine().trim());
186
reader.readLine().trim();
187
reader.readLine().trim();
188
189
tilesx = Math.max(1,tilesx);
190
tilesy = Math.max(1,tilesy);
191
}
192
}
193
}
194
195