0

At the beginning I have to say that I'm learning java on my own and may make a stupidest mistake ever...

I'm facing a problem with FPS drop while drawing Bitmaps in my 2d game.

My game's map consists of Tiles 100x100px, and every tile has some surface graphic and may have something else (like tree, rock, or whatever).

I have 2 main drawing methods. Both looks similar. First is drawing map and second everything that is on it (trees etc.).

Here is a code. I'm limiting what is to be drawn in this method.

public static void drawMap(Canvas canvas, Player player, ArrayList<MapField> map)
{
    int a = player.getShiftX()/Constants.TILE_SIZE;
    int b = player.getShiftY()/Constants.TILE_SIZE;
    for (int x = a-Constants.VISIBILITY_X; x<=a+Constants.VISIBILITY_X; x++)
    {
        if (x>=0&&x<=99)
        {
            for (int y = b-Constants.VISIBILITY_Y; y<=b+Constants.VISIBILITY_Y*2-1; y++)
            {
                if (y>=0&&y<=99)
                {
                    map.get(x+y*100).update();
                    map.get(x+y*100).draw(canvas);
                }
            }
        }
    }
}

Then I'm calling:

public void draw(Canvas canvas)
    {
        canvas.drawBitmap(Graphics.TILES_BITMAP[tileId], null, rect, p);
    }

When I limit "vision" to 3 squares each direction (drawing about 60 tiles) FPS is 60. As soon as I get rid of that limit (whole screen is to be drew - about 250 tiles) FPS are dropping to 27-30 which makes game unplayable.

It is normal behaviour? Is Java that limited?

Or just maybe I made a mistake here?

Full code to be seen here (if anyone care to check):

gitlink

Someone told me that with that amount of graphics i should already use some OpenGL and suggested me to learn LibGDX for example. But as for me, pure java is more elegant :)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LazyMike
  • 1
  • 2

1 Answers1

0

Ok, i've managed to solve this issue. It would only for for API26+ though.

Instead of just:

canvas = this.surfaceHolder.lockCanvas();

i've put:

canvas = this.surfaceHolder.lockHardwareCanvas();

And now again i have 60FPS+ :)

LazyMike
  • 1
  • 2
  • lockHardwareCanvas is a poorly supported feature. It does not work properly on all devices. I have confirmed this. – Johann May 04 '21 at 11:17
  • @Johann Is there any other solution for such issue? I've just noticed that my game is at about 28fps on new phone while at 60 FPS on older one i was testing in a first place. – LazyMike May 31 '22 at 15:53