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开发者_Go百科 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:
// in GameLoop definition
private Bitmap particleA;
private Bitmap particleB;
// somewhere in GameLoop constructor
particleA = BitmapFactor.decodeResource (activity.getResources(), R.drawable.particle_a);
particleB = BitmapFactor.decodeResource (activity.getResources(), R.drawable.particle_b);
// where you build your particle
Particle (GameLoop gl, ...) {
oneLevelDown = new OneLevelDown(GameLoop gl, ...);
}
OneLevelDown (GameLoop gl, ...) {
twoLevelDown = new TwoLevelDown(GameLoop gl, ...);
}
TwoLevelDown (GameLoop gl, ...) {
particleABitmap = gl.getParticleA(); // simple getter
particleBBitmap = gl.getParticleB(); // simple getter
}
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:
class Car{
public int year;
public int name;
}
// Object variables are references to an object's data.
Car a = new Car(); // first car object and variable.
a.year = 1990;
a.name = "Sam";
Car b = new Car();// second car object and variable
b.year = 2000;
b.name = "Bob";
Car c = a; // third car **variable** but same car object as **a**
// primitives are different
int a = 23; // first variable, 4 bytes used
int b = 45; // second variable, 4 bytes used (and a total of 8)
int c = a; // third variable, 4 bytes used (and a total of 12). c took on the value of a, but does not point to the same memory as a
精彩评论