Best way to reuse a texture

Hi,

Im in the process of porting my Falling Rocks game to android and I am having some FPS issues, I have discovered that this is due to the way my objects are created.

My FPS on desktop is 60 solid but FPS on a Galaxy S4 and Tab 3 are around 35-40, which is low considering the power of these devices and the simplicity of the game.

My game creates 300 new objects every minute on normal and does 600/minute on hard, this means that my game does a minimum of 300 File I/O operations a minute.

This seems to be the problem, now I thought “heh, i’ll just create an assets class with a bunch of static fields and reuse the sprites”, well that did not work out well because my objects have a random size every spawn, also after 5 seconds they start to blink to let you know they are about to expire. Which is obviously not working because when 1 sprite flashes they all flash.

However when I did this my FPS was 60 solid on both devices.

What can I do to reduce these File I/O operations? I thought object pooling but that means I would have to load in like , 30 of every texture into memory and store them, then borrow and put back constantly. Might end up hogging all the memory or making the GC go crazy?

Solution

Baring in mine I am using the LibGDX library but that should not make a difference.

Basically create a static reference to the texture/sprite somewhere like so:


public class AssetLoader{

public static final Sprite EMERALD = spriteLoader("data/img/gems/emerald.png");

}

Then when creating a new object, you can use the following constructor in the Sprite class:

/** Creates a sprite that is a copy in every way of the specified sprite. */
	public Sprite (Sprite sprite) {
		set(sprite);
	}

So I done this:

setSprite(AssetLoader.EMERALD);

The set sprite method declares a new sprite, rather than taking the object reference itself, it copes it. So don’t use

this.sprite = sprite;

Instead use

this.sprite = new Sprite(sprite);