Bad FPS in a 2D android / java game on high resolution devices

I am working on a 2D game on Android / Java. During each frame, I use a fixed-size Back Buffer bitmap (1024x768) to draw all the game resources (background, sprites, ...).

Then at the end of onDraw () I draw this back buffer on the screen with the desired size:

Rect Source = new Rect(0, 0, 1024, 768);
Rect Dest = new Rect(0, 0, m_ScreenWidth, m_ScreenHeight);
canvas.drawBitmap(BackBufferBitmap, Source, Dest, null);

The problem is that when I use this engine to just draw a simple 1024x768 image on the screen (the simplest thing I can do), the operation takes from 35 to 40 milliseconds on the LG G4 phone (i.e. about 25 frames per second). Is it possible to get the best fps with another way to control 2D graphics?

I turn on hardware acceleration, thread priority. I do not have this problem on phones with lower pixel counts. I think my problem is with a lot of pixels on the LG G4 (2560x1440). Is it possible to make drawing faster? Or, otherwise, is it possible to just run my game on such high-definition devices with lower resolution (for example, on a PC)?

EDIT: here is the full code:

1) my View

public class ElementaryView extends SurfaceView implements SurfaceHolder.Callback
{
private ElementaryThread    m_Thread;
private SurfaceHolder       m_Holder;
private Bitmap      m_SimpleBitmap=null;
private Bitmap      m_BackBuffer =null;
private Canvas      m_BackBufferCanvas =null;

public ElementaryView(Context context)
    {
    super (context);
    m_Holder =getHolder();
    m_Holder.addCallback(this);
    setFocusable(true);
    }

@Override
public void surfaceCreated(SurfaceHolder holder) 
    {
    m_Thread =new ElementaryThread(m_Holder,this);
    m_Thread.setPriority(Thread.MAX_PRIORITY);

    m_BackBuffer =Bitmap.createBitmap(1024,768,Config.ARGB_8888);
    m_BackBufferCanvas =new Canvas(m_BackBuffer);
    m_SimpleBitmap =BitmapFactory.decodeResource(this.getResources(), R.drawable.splashscreen); 
    //"splashscreen" is a 1024x768 jpg image

    m_Thread.setRunning(true);
    m_Thread.start();
    }
@Override
protected void onDraw(Canvas canvas) 
    {
    m_BackBufferCanvas.drawBitmap(m_SimpleBitmap, 0,  0, null);

    Rect Source =new Rect(0,0,1024,768);
    Rect Dest =new Rect(0,0,this.getWidth(),this.getHeight());

    canvas.drawBitmap(m_BackBuffer,Source,Dest,null);
    }
}

2) my stream:

public class ElementaryThread extends Thread
{
private SurfaceHolder   m_SurfaceHolder;
private ElementaryView  m_View;
private boolean         m_Running;

public ElementaryThread(SurfaceHolder sh, ElementaryView view)
    {
    super();
    m_SurfaceHolder = sh;
    m_View = view;
    }

public void setRunning(boolean r)       { m_Running = r; }

@SuppressLint("WrongCall") @Override
public void run() 
    {
    Canvas canvas;

    while (m_Running)
        {
        canvas =null;
        try
            {
            canvas =this.m_SurfaceHolder.lockCanvas();
            this.m_View.onDraw(canvas);
            }
        finally
            {
            if (canvas!=null)   m_SurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

And it gives a frame rate below 30 frames per second on the LG G4. However, many 2D games work with the best fps on my G4. Does anyone know how they do this?

+4
source share
2 answers

LG G4 . 2560x1440, , , . , res. . , .

0

-, ( ) SurfaceView

-, onDraw. , . ( ..)

onDraw 3 .

, . ? "This.m_View.onDraw();"

@Override
protected void onDraw(Canvas canvas)

ElementaryView

Thread. - :

@SuppressLint("WrongCall") @Override
public void run() 
    {
    Canvas canvas;

    while (m_Running)
        {
        canvas =null;
        try
            {
            canvas =this.m_SurfaceHolder.lockCanvas();
            Rect Source =new Rect(0,0,1024,768);
            Rect Dest =new Rect(0,0,m_View.getWidth(),m_View.getHeight());

            canvas.drawBitmap(m_SimpleBitmap, 0,  0, null);
            canvas.drawBitmap(m_BackBuffer,Source,Dest,null);
            }
        finally
            {
            if (canvas!=null)   m_SurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}
0

Source: https://habr.com/ru/post/1607831/


All Articles