3
\$\begingroup\$

I've been trying to record some gameplay footage of the game I'm currently working on, using the Android SDK and ADB to do it.

Whilst the game runs at constant 60 fps in my medium-end phone, as soon as i start recording, it drops to around 50-55 fps, but always inconsistent. Now, I know that screen recording does "push" harder for the phone and thus may bring the fps down, but I'm worried that, because of this behavior, lower end smartphones will also have these lag issues - which shouldn't happen, as it's a relatively straight forward 2D game.

After performance testing for a while, I figured out that the major cause of lag are the "background elements": being a side-scroller, I have a list that holds 4 of these background elements (which are just textures retrieved from an atlas). As the camera goes further, I then update the position of those textures, so they also appear further, like so:

private List<Mountain> mountains;
// ...
mountains = new ArrayList<Mountain>(4);
for (int i = 0; i < 4; i++) {
        mountainPos = new Vector2(20 + 100 * i, 150);
        mountainColor = Color.BLACK;
        mountains.add(new Mountain(
            mountainPos, 350, 250, "texturePath", mountainColor));
// ...
// On the update method, I update the positions.
for (int i = 0; i < 4; i++) {
    // If the mountain can't be seen by the camera...
    if (hero.getPosition().x - mountains.get(i).getPosition().x >= 500) {
        highestMountainX += 100;
        mountains.get(i).setPosition(new Vector2(
                highestMountainX,
                mountains.get(i).getPosition().y)
        ));
   }
}
// ...
// On the render method
for (int i = 0; i < 4; i++)
    mountains.get(i).render(batch);

So, this leaves me with two questions:

1. Roughly, should I be worried about lag issues on lower-end smartphones, considering the fps drops I get while recording in my medium-end one?

2. The sprites I mention above are 350x250 pixels, and are rendered with that same width and height (350 and 250, respectively). Is the position updating method above inefficient? Any optimizations I could do?

Thanks in advance.

EDIT 1: Not sure if this is relevant, but the spritebatch's total render calls, everytime the render method is called, ranges from 4 to 14 (during the play state).

\$\endgroup\$
3
\$\begingroup\$

You should avoid invoking new instances in your render and update methods. As these methods are called numerous times per second, this can be a pretty straining task, as the unreferenced instances will have to be freed in memory through garbage collection. To solve the two Vector2 problems, easy:

Declare and instantiate two global Vector2 attributes, and change your code to:

//first Vector2
mountainPos = firstGlobalVector.set(20 + 100 * i, 150);

//second Vector2
mountains.get(i).setPosition(secondGlobalVector.set(
                highestMountainX,
                mountains.get(i).getPosition().y)
        ));

As I do not know how your Mountain class is constructed, I cannot directly help you avoid the continuous invocation of new Mountain instances. Just try to solve it in the same manner as we've solved the Vector2 problems. Avoid new instances, and try to update already existing instances.

\$\endgroup\$
  • \$\begingroup\$ Hey, first off thanks for helping! I actually didn't realize how possibly expensive the instantiation of Vector2 objects could be. I'll try to optimize as much as possible. Thank you! \$\endgroup\$ – Francisco Jun 25 '16 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.