Where do you load number of frames in an animation?

So I was thinking there are a few ways to do it.

  1. At the point where you cache them.
  2. Everytime you create them to be used or in the sprite class or sprite manager that calls them.
  3. Or have some sort of auto search that looks for a frame seperator.

How do you typically do it?

I just created a resourcecache but seemed like I was missing some management.



import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;

public abstract class ResourceFileCache {
    protected HashMap hmResources;

    public ResourceFileCache (){
        hmResources = new HashMap();
    }
    
    // should this include the frames and store as a class?  which has dir, name, frames
    protected void load (String directory, String name){
         try {
                Object resource = ImageIO.read(new File(directory + name));
                hmResources.put(name, resource);                
            } catch (IOException e) {
                System.err.println("Failed to load resource: "+directory+name);
            }        
    }
    
    protected Object get (String name) {
        Object resource = hmResources.get(name);
        if (resource == null)
            System.err.println("Failed to find resource: "+name);
        
       return resource;
    }

}


As always: it depends

If you can cache everything, do it, in a preloader! Nothing more annoying than stuttering gameplay at the moment new units / weapons were supposed to add to the thrill.

If you can’t hold it all into RAM, you’ll need to build a cache that also allows to remove items.