Slick caches image when you use the "ref" constructor:
Code:
image = new Image("myimage.png");
image2 = new Image("myimage.png");
image.getTexture() == image2.getTexture()
//returns true
The same should apply to using "ref" on SpriteSheet constructors. You can also specify a "cache name" with the overloaded InputStream constructor to specify the name to use in the cache. Furthermore, Slick will cache an image by its filter, "flipped" value and transparent colors. So if you create an image with FILTER_NEAREST, and then create another image using the same path with FILTER_LINEAR, two openGL textures will be cached.
In my personal opinion the caching should be removed from Slick as it hides too much from the user; only on Android is it truly useful. The rest of the devs/community will have to decide on this, though.
In general I would suggest
not relying on Slick's cache for your games, as it's too much "under the hood" -- if you do things yourself, you will know exactly where and when an image is being loaded and unloaded. Instead, treat the "ref" constructor as if it were actually loading the image (i.e. don't call it more than once).
Worth mentioning, the following operations are all really quick since no new textures need to be loaded/decoded:
Code:
new Image(Texture)
new Image(Image)
Image.getSubImage
Image.getFlippedCopy
Image.getScaledCopy
Image.copy
Simple example of a custom cache:
Code:
private static HashMap<String, Image> imageMap = new HashMap<String, Image>();
public static Image loadImage(String key, String path, int filter) {
Image ret;
imageMap.put(key, ret = new Image(path, filter));
return ret;
}
public static Image getImage(String key) {
return imageMap.get(key);
}
public static void unloadImage(String key) {
Image image = imageMap.get(key);
if (image!=null) {
imageMap.remove(image);
image.release();
}
}