About performance and drawing Bitmaps

62 Views Asked by At

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 :)

1

There are 1 best solutions below

2
LazyMike On

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+ :)