LWJGL Cahce Textures in a Hashmap

Started by
2 comments, last by coppyhop 9 years, 9 months ago

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.

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!


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?

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

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.

This topic is closed to new replies.

Advertisement