-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteStore.java
More file actions
111 lines (83 loc) · 2.81 KB
/
SpriteStore.java
File metadata and controls
111 lines (83 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;
/**
* A resource manager for sprites in the game. Its often quite important
* how and where you get your game resources from. In most cases
* it makes sense to have a central resource loader that goes away, gets
* your resources and caches them for future use.
* <p>
* [singleton]
* <p>
* @author Kevin Glass
*/
public class SpriteStore {
/** The single instance of this class */
private static SpriteStore single = new SpriteStore();
/**
* Get the single instance of this class
*
* @return The single instance of this class
*/
public static SpriteStore get() {
return single;
}
/** The cached sprite map, from reference to sprite instance */
private HashMap sprites = new HashMap();
/**
* Retrieve a sprite from the store
*
* @param ref The reference to the image to use for the sprite
* @return A sprite instance containing an accelerate image of the request reference
*/
public Sprite getSprite(String ref) {
// if we've already got the sprite in the cache
// then just return the existing version
if (sprites.get(ref) != null) {
return (Sprite) sprites.get(ref);
}
// otherwise, go away and grab the sprite from the resource
// loader
BufferedImage sourceImage = null;
try {
// The ClassLoader.getResource() ensures we get the sprite
// from the appropriate place, this helps with deploying the game
// with things like webstart. You could equally do a file look
// up here.
URL url = this.getClass().getClassLoader().getResource(ref);
if (url == null) {
fail("Can't find ref: "+ref);
}
// use ImageIO to read the image in
sourceImage = ImageIO.read(url);
} catch (IOException e) {
fail("Failed to load: "+ref);
}
// create an accelerated image of the right size to store our sprite in
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.BITMASK);
// draw our source image into the accelerated image
image.getGraphics().drawImage(sourceImage,0,0,null);
// create a sprite, add it the cache then return it
Sprite sprite = new Sprite(image);
sprites.put(ref,sprite);
return sprite;
}
/**
* Utility method to handle resource loading failure
*
* @param message The message to display on failure
*/
private void fail(String message) {
// we'n't available
// we dump the message and exit the game
System.err.println(message);
System.exit(0);
}
}