Slick Forums

Discuss the Slick 2D Library
It is currently Sat May 25, 2013 2:38 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: SpriteSheets and memory
PostPosted: Wed Mar 21, 2012 4:51 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
I was using Image in each of my objects. Slick does something in the background where it'll only load each image once, even if used by 100 objects.

I tried switching to SpriteSheet, but my memory usage shot up dramatically. I guess slick doesn't cache/share SpriteSheet like it does with Image? Just trying to understand how it works.

One cheap trick I was thinking, was just to make SpriteSheet static or share it myself between Objects. Any downside to this?


Top
 Profile  
 
PostPosted: Wed Mar 21, 2012 5:28 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
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();
      }
  }


Top
 Profile  
 
PostPosted: Wed Mar 21, 2012 8:03 am 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
You should always have a resource class which loads all the images and just returns a reference for them if you need. As davedes mentioned, for some weird reason Slick hides away a lot of control over loading. There is a lot of room for optimization (e.g. setting color in Graphics = always new Object...???).

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group