I'm developing a game in Java for Android, using LibGDX. In my render(), between the batch.begin() and batch.end() I'm running through all the game objects and draw them.
Every game object is stored in a list, which that list is stored inside a hash map. I iterate through this hash map, getting every list and going through that list, like this:
batch.begin();
Iterator it = objectsMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
Array list = (Array) pair.getValue();
for (int i = 0; i < list.size; i++) {
GameObject object = (GameObject) list.get(i);
drawObject(object);
}
}
batch.end();
My drawObject() code:
private void drawObject(GameObject object, float x, float y) {
if (object == null) {
return;
}
if (object.getVisibility()) {
TextureAtlas.AtlasRegion currentFrame = object.getCurrentFrame();
if (currentFrame != null) {
float objectAlpha = object.getAlpha();
Color color = batch.getColor();
if (objectAlpha != 1) {
color.a = object.getAlpha();
batch.setColor(color);
}
batch.draw(currentFrame, x - object.getOriginX(), y - object.getOriginY(), object.getOriginX(), object.getOriginY(), currentFrame.getRegionWidth(), currentFrame.getRegionHeight(), object.scaleX, object.scaleY, object.getSpriteDirection());
color.a = 1;
batch.setColor(color);
}
}
}
A method called setRegion() sets the current animation frames of the object:
protected void setRegion(String name, float frameDuration) {
currentFrame = atlas.findRegion(name, 0);
if (currentFrame == null) {
currentFrame = atlas.findRegion(name, -1);
}
width = currentFrame.getRegionWidth();
height = currentFrame.getRegionHeight();
animationStateTime = 0;
animation = new Animation(frameDuration, atlas.findRegions(name), Animation.PlayMode.LOOP);
}
And in the update() of the objects this is called:
currentFrame = (TextureAtlas.AtlasRegion) animation.getKeyFrame(animationStateTime, isLooping);
The getCurrentFrame() method only returns currentFrame variable. The alpha is a field with a value between 0 and 1. (getAlpha() returns only the field)
My question is that considered a "heavy" activity? Can this be a significant impact on the FPS values? Because I'm running my game on old devices (like Galaxy S2) and the FPS can get pretty low values. I'm trying to find the cause to it.
I used to have a reference to every list but with the time I realized I'm going to have too much lists, so I decided to do it like this. Maybe you know a better way of holding references to all game objects?
Thanks!
getCurrentFrame()
andgetAlpha()
? Really hard to say anything about it without the code itself. \$\endgroup\$