Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/SpriteSheet.java
1456 views
1
package org.newdawn.slick;
2
3
import java.io.InputStream;
4
5
import org.newdawn.slick.opengl.Texture;
6
7
/**
8
* A sheet of sprites that can be drawn individually
9
*
10
* @author Kevin Glass
11
*/
12
public class SpriteSheet extends Image {
13
/** The width of a single element in pixels */
14
private int tw;
15
/** The height of a single element in pixels */
16
private int th;
17
/** The margin of the image */
18
private int margin = 0;
19
/** Subimages */
20
private Image[][] subImages;
21
/** The spacing between tiles */
22
private int spacing;
23
/** The target image for this sheet */
24
private Image target;
25
26
/**
27
* Create a new sprite sheet based on a image location
28
*
29
* @param image The image to based the sheet of
30
* @param tw The width of the tiles on the sheet
31
* @param th The height of the tiles on the sheet
32
*/
33
public SpriteSheet(Image image,int tw,int th) {
34
super(image);
35
36
this.target = image;
37
this.tw = tw;
38
this.th = th;
39
40
// call init manually since constructing from an image will have previously initialised
41
// from incorrect values
42
initImpl();
43
}
44
45
/**
46
* Create a new sprite sheet based on a image location
47
*
48
* @param image The image to based the sheet of
49
* @param tw The width of the tiles on the sheet
50
* @param th The height of the tiles on the sheet
51
* @param spacing The spacing between tiles
52
* @param margin The magrin around the tiles
53
*/
54
public SpriteSheet(Image image,int tw,int th,int spacing,int margin) {
55
super(image);
56
57
this.target = image;
58
this.tw = tw;
59
this.th = th;
60
this.spacing = spacing;
61
this.margin = margin;
62
63
// call init manually since constructing from an image will have previously initialised
64
// from incorrect values
65
initImpl();
66
}
67
68
/**
69
* Create a new sprite sheet based on a image location
70
*
71
* @param image The image to based the sheet of
72
* @param tw The width of the tiles on the sheet
73
* @param th The height of the tiles on the sheet
74
* @param spacing The spacing between tiles
75
*/
76
public SpriteSheet(Image image,int tw,int th,int spacing) {
77
this(image,tw,th,spacing,0);
78
}
79
80
/**
81
* Create a new sprite sheet based on a image location
82
*
83
* @param ref The location of the sprite sheet to load
84
* @param tw The width of the tiles on the sheet
85
* @param th The height of the tiles on the sheet
86
* @param spacing The spacing between tiles
87
* @throws SlickException Indicates a failure to load the image
88
*/
89
public SpriteSheet(String ref,int tw,int th, int spacing) throws SlickException {
90
this(ref,tw,th,null,spacing);
91
}
92
93
/**
94
* Create a new sprite sheet based on a image location
95
*
96
* @param ref The location of the sprite sheet to load
97
* @param tw The width of the tiles on the sheet
98
* @param th The height of the tiles on the sheet
99
* @throws SlickException Indicates a failure to load the image
100
*/
101
public SpriteSheet(String ref,int tw,int th) throws SlickException {
102
this(ref,tw,th,null);
103
}
104
105
/**
106
* Create a new sprite sheet based on a image location
107
*
108
* @param ref The location of the sprite sheet to load
109
* @param tw The width of the tiles on the sheet
110
* @param th The height of the tiles on the sheet
111
* @param col The colour to treat as transparent
112
* @throws SlickException Indicates a failure to load the image
113
*/
114
public SpriteSheet(String ref,int tw,int th, Color col) throws SlickException {
115
this(ref, tw, th, col, 0);
116
}
117
118
/**
119
* Create a new sprite sheet based on a image location
120
*
121
* @param ref The location of the sprite sheet to load
122
* @param tw The width of the tiles on the sheet
123
* @param th The height of the tiles on the sheet
124
* @param col The colour to treat as transparent
125
* @param spacing The spacing between tiles
126
* @throws SlickException Indicates a failure to load the image
127
*/
128
public SpriteSheet(String ref,int tw,int th, Color col, int spacing) throws SlickException {
129
super(ref, false, FILTER_NEAREST, col);
130
131
this.target = this;
132
this.tw = tw;
133
this.th = th;
134
this.spacing = spacing;
135
}
136
137
/**
138
* Create a new sprite sheet based on a image location
139
*
140
* @param name The name to give to the image in the image cache
141
* @param ref The stream from which we can load the image
142
* @param tw The width of the tiles on the sheet
143
* @param th The height of the tiles on the sheet
144
* @throws SlickException Indicates a failure to load the image
145
*/
146
public SpriteSheet(String name, InputStream ref,int tw,int th) throws SlickException {
147
super(ref,name,false);
148
149
this.target = this;
150
this.tw = tw;
151
this.th = th;
152
}
153
154
/**
155
* @see org.newdawn.slick.Image#initImpl()
156
*/
157
protected void initImpl() {
158
if (subImages != null) {
159
return;
160
}
161
162
int tilesAcross = ((getWidth()-(margin*2) - tw) / (tw + spacing)) + 1;
163
int tilesDown = ((getHeight()-(margin*2) - th) / (th + spacing)) + 1;
164
if ((getHeight() - th) % (th+spacing) != 0) {
165
tilesDown++;
166
}
167
168
subImages = new Image[tilesAcross][tilesDown];
169
for (int x=0;x<tilesAcross;x++) {
170
for (int y=0;y<tilesDown;y++) {
171
subImages[x][y] = getSprite(x,y);
172
}
173
}
174
}
175
176
/**
177
* Get the sub image cached in this sprite sheet
178
*
179
* @param x The x position in tiles of the image to get
180
* @param y The y position in tiles of the image to get
181
* @return The subimage at that location on the sheet
182
*/
183
public Image getSubImage(int x, int y) {
184
init();
185
186
if ((x < 0) || (x >= subImages.length)) {
187
throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);
188
}
189
if ((y < 0) || (y >= subImages[0].length)) {
190
throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);
191
}
192
193
return subImages[x][y];
194
}
195
196
/**
197
* Get a sprite at a particular cell on the sprite sheet
198
*
199
* @param x The x position of the cell on the sprite sheet
200
* @param y The y position of the cell on the sprite sheet
201
* @return The single image from the sprite sheet
202
*/
203
public Image getSprite(int x, int y) {
204
target.init();
205
initImpl();
206
207
if ((x < 0) || (x >= subImages.length)) {
208
throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);
209
}
210
if ((y < 0) || (y >= subImages[0].length)) {
211
throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);
212
}
213
214
return target.getSubImage(x*(tw+spacing) + margin, y*(th+spacing) + margin,tw,th);
215
}
216
217
/**
218
* Get the number of sprites across the sheet
219
*
220
* @return The number of sprites across the sheet
221
*/
222
public int getHorizontalCount() {
223
target.init();
224
initImpl();
225
226
return subImages.length;
227
}
228
229
/**
230
* Get the number of sprites down the sheet
231
*
232
* @return The number of sprite down the sheet
233
*/
234
public int getVerticalCount() {
235
target.init();
236
initImpl();
237
238
return subImages[0].length;
239
}
240
241
/**
242
* Render a sprite when this sprite sheet is in use.
243
*
244
* @see #startUse()
245
* @see #endUse()
246
*
247
* @param x The x position to render the sprite at
248
* @param y The y position to render the sprite at
249
* @param sx The x location of the cell to render
250
* @param sy The y location of the cell to render
251
*/
252
public void renderInUse(int x,int y,int sx,int sy) {
253
subImages[sx][sy].drawEmbedded(x, y, tw, th);
254
}
255
256
/**
257
* @see org.newdawn.slick.Image#endUse()
258
*/
259
public void endUse() {
260
if (target == this) {
261
super.endUse();
262
return;
263
}
264
target.endUse();
265
}
266
267
/**
268
* @see org.newdawn.slick.Image#startUse()
269
*/
270
public void startUse() {
271
if (target == this) {
272
super.startUse();
273
return;
274
}
275
target.startUse();
276
}
277
278
/**
279
* @see org.newdawn.slick.Image#setTexture(org.newdawn.slick.opengl.Texture)
280
*/
281
public void setTexture(Texture texture) {
282
if (target == this) {
283
super.setTexture(texture);
284
return;
285
}
286
target.setTexture(texture);
287
}
288
}
289
290