diff options
author | Starla Insigna <hatkirby@fourisland.com> | 2009-02-11 22:23:10 -0500 |
---|---|---|
committer | Starla Insigna <hatkirby@fourisland.com> | 2009-02-11 22:23:10 -0500 |
commit | 804beeb6a3b8d374e7a95600c700f109f6f40280 (patch) | |
tree | 7d810fe68de9b2a69207840b36b8b37faf71d227 /src/com/fourisland | |
parent | 325f6291557ff38d3992a0fb38a4efbc70aefa58 (diff) | |
download | fourpuzzle-804beeb6a3b8d374e7a95600c700f109f6f40280.tar.gz fourpuzzle-804beeb6a3b8d374e7a95600c700f109f6f40280.tar.bz2 fourpuzzle-804beeb6a3b8d374e7a95600c700f109f6f40280.zip |
Engine: Optimized ObjectLoader
Split ObjectLoader's objectCache into three caches: imageCache (BufferedImage), musicCache (Sequence) and soundCache (Clip). This way, each resource type has its own section and no time-consuming casting is necessary.
Diffstat (limited to 'src/com/fourisland')
-rwxr-xr-x | src/com/fourisland/fourpuzzle/util/ObjectLoader.java | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java index 3f518cf..6df3796 100755 --- a/src/com/fourisland/fourpuzzle/util/ObjectLoader.java +++ b/src/com/fourisland/fourpuzzle/util/ObjectLoader.java | |||
@@ -12,6 +12,7 @@ import java.io.IOException; | |||
12 | import java.io.InputStream; | 12 | import java.io.InputStream; |
13 | import java.net.URL; | 13 | import java.net.URL; |
14 | import java.util.HashMap; | 14 | import java.util.HashMap; |
15 | import java.util.Map; | ||
15 | import java.util.Map.Entry; | 16 | import java.util.Map.Entry; |
16 | import java.util.logging.Level; | 17 | import java.util.logging.Level; |
17 | import java.util.logging.Logger; | 18 | import java.util.logging.Logger; |
@@ -34,18 +35,17 @@ import org.jdesktop.application.ResourceMap; | |||
34 | */ | 35 | */ |
35 | public class ObjectLoader { | 36 | public class ObjectLoader { |
36 | 37 | ||
37 | private static HashMap<String,Object> objectCache = new HashMap<String,Object>(); | 38 | private static Map<String,BufferedImage> imageCache = new HashMap<String,BufferedImage>(); |
39 | private static Map<String,Sequence> musicCache = new HashMap<String,Sequence>(); | ||
40 | private static Map<String,Clip> soundCache = new HashMap<String,Clip>(); | ||
38 | 41 | ||
39 | static | 42 | static |
40 | { | 43 | { |
41 | Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | 44 | Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { |
42 | public void run() { | 45 | public void run() { |
43 | for (Entry<String, Object> o : objectCache.entrySet()) | 46 | for (Entry<String, Clip> o : soundCache.entrySet()) |
44 | { | 47 | { |
45 | if (o.getKey().startsWith("Sound/")) | 48 | o.getValue().close(); |
46 | { | ||
47 | ((Clip) o.getValue()).close(); | ||
48 | } | ||
49 | } | 49 | } |
50 | } | 50 | } |
51 | })); | 51 | })); |
@@ -53,7 +53,7 @@ public class ObjectLoader { | |||
53 | 53 | ||
54 | public static BufferedImage getImage(String type, String name) | 54 | public static BufferedImage getImage(String type, String name) |
55 | { | 55 | { |
56 | if (!objectCache.containsKey(type + "/" + name)) | 56 | if (!imageCache.containsKey(type + "/" + name)) |
57 | { | 57 | { |
58 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); | 58 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); |
59 | String filename = getFilename(type, name, "png"); | 59 | String filename = getFilename(type, name, "png"); |
@@ -65,15 +65,15 @@ public class ObjectLoader { | |||
65 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | 65 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); |
66 | } | 66 | } |
67 | 67 | ||
68 | addToObjectCache(type,name,bImg); | 68 | addToObjectCache(imageCache, type + "/" + name, bImg); |
69 | } | 69 | } |
70 | 70 | ||
71 | return (BufferedImage) objectCache.get(type + "/" + name); | 71 | return imageCache.get(type + "/" + name); |
72 | } | 72 | } |
73 | 73 | ||
74 | public static BufferedImage getImage(String type, String name, int transparencyColor) | 74 | public static BufferedImage getImage(String type, String name, int transparencyColor) |
75 | { | 75 | { |
76 | if (!objectCache.containsKey(type + "/" + name)) | 76 | if (!imageCache.containsKey(type + "/" + name)) |
77 | { | 77 | { |
78 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); | 78 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); |
79 | String filename = getFilename(type, name, "png"); | 79 | String filename = getFilename(type, name, "png"); |
@@ -87,25 +87,25 @@ public class ObjectLoader { | |||
87 | 87 | ||
88 | bImg.createGraphics().drawImage(bImg, 0, 0, new Color(transparencyColor, true), null); | 88 | bImg.createGraphics().drawImage(bImg, 0, 0, new Color(transparencyColor, true), null); |
89 | 89 | ||
90 | addToObjectCache(type,name,bImg); | 90 | addToObjectCache(imageCache, type + "/" + name, bImg); |
91 | } | 91 | } |
92 | 92 | ||
93 | return (BufferedImage) objectCache.get(type + "/" + name); | 93 | return imageCache.get(type + "/" + name); |
94 | } | 94 | } |
95 | 95 | ||
96 | public static void addToObjectCache(String type, String name, Object object) | 96 | public static void addToObjectCache(Map cacheMap, String name, Object object) |
97 | { | 97 | { |
98 | if (objectCache.size() >= 100) | 98 | if (cacheMap.size() >= 100) |
99 | { | 99 | { |
100 | objectCache.clear(); | 100 | cacheMap.clear(); |
101 | } | 101 | } |
102 | 102 | ||
103 | objectCache.put(type + "/" + name, object); | 103 | cacheMap.put(name, object); |
104 | } | 104 | } |
105 | 105 | ||
106 | public static Sequence getMusic(String name) | 106 | public static Sequence getMusic(String name) |
107 | { | 107 | { |
108 | if (!objectCache.containsKey("Music/" + name)) | 108 | if (!musicCache.containsKey(name)) |
109 | { | 109 | { |
110 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); | 110 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); |
111 | String filename = getFilename("Music", name, "mid"); | 111 | String filename = getFilename("Music", name, "mid"); |
@@ -126,15 +126,15 @@ public class ObjectLoader { | |||
126 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | 126 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); |
127 | } | 127 | } |
128 | 128 | ||
129 | addToObjectCache("Music", name, seq); | 129 | addToObjectCache(musicCache, name, seq); |
130 | } | 130 | } |
131 | 131 | ||
132 | return (Sequence) objectCache.get("Music/" + name); | 132 | return musicCache.get(name); |
133 | } | 133 | } |
134 | 134 | ||
135 | public static Clip getSound(String name) | 135 | public static Clip getSound(String name) |
136 | { | 136 | { |
137 | if (!objectCache.containsKey("Sound/" + name)) | 137 | if (!soundCache.containsKey("Sound/" + name)) |
138 | { | 138 | { |
139 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); | 139 | ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); |
140 | String filename = getFilename("Sound", name, "wav"); | 140 | String filename = getFilename("Sound", name, "wav"); |
@@ -165,10 +165,10 @@ public class ObjectLoader { | |||
165 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); | 165 | Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); |
166 | } | 166 | } |
167 | 167 | ||
168 | addToObjectCache("Sound", name, line); | 168 | addToObjectCache(soundCache, name, line); |
169 | } | 169 | } |
170 | 170 | ||
171 | return (Clip) objectCache.get("Sound/" + name); | 171 | return soundCache.get(name); |
172 | } | 172 | } |
173 | 173 | ||
174 | public static String getFilename(String type, String name, String ex) | 174 | public static String getFilename(String type, String name, String ex) |