• Advertisement
Sign in to follow this  

LWJGL Cahce Textures in a Hashmap

This topic is 1322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.
  • Profit!
Edited by Aldacron

Share this post


Link to post
Share on other sites


How can I do that?
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?

Share this post


Link to post
Share on other sites

How I load the Textures is in the Block.java I call 

import org.newdawn.slick.Texture;

import org.newdawn.slick.TextureLoader;

 

(Or something like that)

 

Texture block = null;

try{

block = TextureLoader.loadTexture(new FileInputStream(new File (type.imageURL)));

 

I have an enumerator Type which has a (String) imageURL.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement