-1

I'm making a simple 2d game in android. However, even with only a few images on screen, I'm running into problems with fps(its around 15-25 and quite stuttery).

Currently I use canvas, and simply set up an image each frame with everything I need to display on it.

Here's some of my render code:

public void render(Painter g) {
    g.setColor(Color.rgb(208, 244, 247));
    g.fillRect(0, 0, GameMainActivity.GAME_WIDTH, GameMainActivity.GAME_HEIGHT);
    g.setColor(Color.rgb(139,69,19));
    g.fillRect(0, 1400, GameMainActivity.GAME_WIDTH, GameMainActivity.GAME_HEIGHT - 1400);
    g.drawImage(Assets.background3stone, currentBackground3X, 141);
    g.drawImage(Assets.background3stone, currentBackground3X + 3459, 141);
    g.drawImage(Assets.background2stone, currentBackground2X, 141);
    g.drawImage(Assets.background2stone, currentBackground2X + 3459, 141);
    g.drawImage(Assets.ground, currentBackground1X, 760);
    g.drawImage(Assets.ground, currentBackground1X + 3000, 760);
    renderNews(g);
    renderButtons(g);
    g.drawImage(Assets.leaderboard, 40, 180, 148, 148);

    g.drawImage(Assets.achievements, 40, 354, 148, 148);

    g.drawString("FPS: " + frameValue, 50, 200);
}

This code runs for every iteration of the game loop.

Does anyone know any ways to optimize my performance? Currently I redraw literally everything every frame, is there a way to not redraw static images? Would switching to openGl help a lot?

Thanks!

mongy910
  • 537
  • 1
  • 5
  • 16

3 Answers3

1

There's also a lot of good and efficient game engines available for Android (For example LibGDX). Game engines are usually well optimized for showing, moving and animating multiple images and they usually come with plenty other useful features.

However, if you prefer not to use game engines, I'd recommend using OpenGL to get some boost. You may also get some boost by optimizing your images. Try to reduce the sizes of images and don't use too large images. Also try to load images efficiently to the program.

Leevi Lehtonen
  • 118
  • 1
  • 1
  • 7
0

Switching to openGL would always help a lot. But your problem is that you're drawing directly to the screen. Don't do that. Draw to a bitmap, then blit the bitmap to the screen in a single operation. The performance of that will be MUCH higher. It also would allow you to do your drawing on a 2nd thread and only draw to the screen on the main one.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I heard that opengl is really confusing to switch to. Is this true? If so, would libgdx make an easier transition? – mongy910 Feb 21 '15 at 20:47
0

Before switching to openGL you may want to try to not redraw the image itself, but just to move it. You just need to set new LayoutParams to your image. You may also want to look at TranslateAnimation in case you are moving stuff softly. Or you use a matrix transformation.

Gunnar Bernstein
  • 6,074
  • 2
  • 45
  • 67
  • What do you mean just set new LayoutParams? I thought every time the image location changes it has to be redrawn to the canvas? – mongy910 Feb 21 '15 at 20:45
  • Yes, it has to be redrawn on the screen, but the resource file does not have to be loaded again, no bitmap created, size calculated etc. That is much faster. You can keep an array of ImageViews to point at the ones drawn and then just move it. http://stackoverflow.com/questions/10141244/android-please-explain-how-to-properly-move-a-view – Gunnar Bernstein Feb 21 '15 at 23:42