From 804beeb6a3b8d374e7a95600c700f109f6f40280 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Wed, 11 Feb 2009 22:23:10 -0500 Subject: 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. --- .../fourisland/fourpuzzle/util/ObjectLoader.java | 44 +++++++++++----------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'src/com/fourisland') 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; import java.io.InputStream; import java.net.URL; import java.util.HashMap; +import java.util.Map; import java.util.Map.Entry; import java.util.logging.Level; import java.util.logging.Logger; @@ -34,18 +35,17 @@ import org.jdesktop.application.ResourceMap; */ public class ObjectLoader { - private static HashMap objectCache = new HashMap(); + private static Map imageCache = new HashMap(); + private static Map musicCache = new HashMap(); + private static Map soundCache = new HashMap(); static { Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { - for (Entry o : objectCache.entrySet()) + for (Entry o : soundCache.entrySet()) { - if (o.getKey().startsWith("Sound/")) - { - ((Clip) o.getValue()).close(); - } + o.getValue().close(); } } })); @@ -53,7 +53,7 @@ public class ObjectLoader { public static BufferedImage getImage(String type, String name) { - if (!objectCache.containsKey(type + "/" + name)) + if (!imageCache.containsKey(type + "/" + name)) { ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); String filename = getFilename(type, name, "png"); @@ -65,15 +65,15 @@ public class ObjectLoader { Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); } - addToObjectCache(type,name,bImg); + addToObjectCache(imageCache, type + "/" + name, bImg); } - return (BufferedImage) objectCache.get(type + "/" + name); + return imageCache.get(type + "/" + name); } public static BufferedImage getImage(String type, String name, int transparencyColor) { - if (!objectCache.containsKey(type + "/" + name)) + if (!imageCache.containsKey(type + "/" + name)) { ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); String filename = getFilename(type, name, "png"); @@ -87,25 +87,25 @@ public class ObjectLoader { bImg.createGraphics().drawImage(bImg, 0, 0, new Color(transparencyColor, true), null); - addToObjectCache(type,name,bImg); + addToObjectCache(imageCache, type + "/" + name, bImg); } - return (BufferedImage) objectCache.get(type + "/" + name); + return imageCache.get(type + "/" + name); } - public static void addToObjectCache(String type, String name, Object object) + public static void addToObjectCache(Map cacheMap, String name, Object object) { - if (objectCache.size() >= 100) + if (cacheMap.size() >= 100) { - objectCache.clear(); + cacheMap.clear(); } - objectCache.put(type + "/" + name, object); + cacheMap.put(name, object); } public static Sequence getMusic(String name) { - if (!objectCache.containsKey("Music/" + name)) + if (!musicCache.containsKey(name)) { ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); String filename = getFilename("Music", name, "mid"); @@ -126,15 +126,15 @@ public class ObjectLoader { Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); } - addToObjectCache("Music", name, seq); + addToObjectCache(musicCache, name, seq); } - return (Sequence) objectCache.get("Music/" + name); + return musicCache.get(name); } public static Clip getSound(String name) { - if (!objectCache.containsKey("Sound/" + name)) + if (!soundCache.containsKey("Sound/" + name)) { ResourceMap rm = PuzzleApplication.INSTANCE.getContext().getResourceManager().getResourceMap(); String filename = getFilename("Sound", name, "wav"); @@ -165,10 +165,10 @@ public class ObjectLoader { Logger.getLogger(ObjectLoader.class.getName()).log(Level.SEVERE, null, ex); } - addToObjectCache("Sound", name, line); + addToObjectCache(soundCache, name, line); } - return (Clip) objectCache.get("Sound/" + name); + return soundCache.get(name); } public static String getFilename(String type, String name, String ex) -- cgit 1.4.1