In my previous question, I asked how can I stop the too many files exception, I knew what my problem was, and the solution was, Have the Texture Loader Cache the Textures In a Hashmap so that the instance can check if it exists already, and then use it if it is. How can I do that? I'm using slick-utils TextureLoader.
GDNet+ - Reputation: 3104
Posted 07 July 2014 - 11:11 PM
I haven't used slick-utils, but if you can't find any option for caching in the documentation, then it's very simple to implement it yourself.
- Create your own TextureLoader class which contains an instance of the slick-utils loader (unless the Slick loader uses static methods, of course) and a java.util.HashMap<String,Texture>.
- Implement a load method that accepts a file name and returns a texture.
- Inside your load method, take the following steps:
- Try to fetch the texture from your cache by calling 'get' on your hash map instance using the file name as a key.
- If the 'get' call returns null, call the Slick texture loader, add the loaded texture to the hash map with the file name as the key, then return it.
- If the 'get' call doesn't return null, then simply return the texture.
Edited by Aldacron, 07 July 2014 - 11:19 PM.
Crossbones+ - Reputation: 3969
Posted 08 July 2014 - 04:21 AM
What have you tried? Did you read the HashMap docs? Do you know what a key-value store is? Do you know how to save a reference for an object and retrieve it later when you need it?
How can I do that?
"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"
Members - Reputation: 109
Posted 08 July 2014 - 02:05 PM
How I load the Textures is in the Block.java I call
(Or something like that)
Texture block = null;
block = TextureLoader.loadTexture(new FileInputStream(new File (type.imageURL)));
I have an enumerator Type which has a (String) imageURL.