I have a GameLoop class with a ParticleSystem object defined in it.
The ParticleSystem contains an array of Particle objects – I don’t want to have to load an image for each individual particle, id like to just be able to draw from a source image in the GameLoop class – how can I do this in an efficient manner?
SOLUTION:
Here is what Dan S helped me come up with:
public class ResourceManager {
Context mContext;
public final HashMap<String, Bitmap> Texture = new HashMap<String, Bitmap>();
//Constructor
public ResourceManager(Context context)
{
mContext = context;
Texture.put("particle1", loadBitmap(R.drawable.particle1));
}
public Bitmap getBitmap(String key)
{
return Texture.get(key);
}
/** Load an image */
protected Bitmap loadBitmap(int id) { return BitmapFactory.decodeResource(mContext.getResources(), id); }
} //End ResourceManager
then define it in a class:
rm = new ResourceManager(mContext);
And then pass the rm variable down the way:
ParticleSystem.Draw(rm, canvas);
{
Particle.Draw(rm, canvas);
}
and in Particle class, there is a String assetKey I set in constructor – so that I can refer to the Bitmap by a name:
public void doDraw(ResourceManager rm, Canvas canvas) {
canvas.drawBitmap(rm.getBitmap(AssetKey), xpos, ypos, null);
}
I hope this is helpful to someone else also.
In your constructor of GameLoop create your bitmaps and hold a reference to them there, set them as final variables for added protection against accidental assignment. Whenever you create a new particle system pass them to the constructor or set them in an appropriate setter method.
Example:
Just keep passing GameLoop down until you’re done. This might not feel efficient but once you get the hang of it you’ll like, also see this article about Dependency Injection.
Java memory usage example: