0

i'm currently writing a android game, but now, i'm having some strange issues with the controls

if i have a framelayout with all my controls(the HP, points, game controls..etc) and put it on top of my surfaceview, my fps will never get above 20 fps; if i remove the framelayout, the game will hit 50+fps.

I'm suspecting that the system is rendering my surfaceview and framelayout's content all together in one thread, which explains why it's so slow

here's my game loop code

public void run() {
    Canvas c;
    initTimingElements();

    long beginTime;     // the time when the cycle begun
    long timeDiff;      // the time it took for the cycle to execute
    int sleepTime;      // ms to sleep (<0 if we're behind)
    int framesSkipped;  // number of frames being skipped 

    sleepTime = 0;

    while (_run) {
        c = null;
        //long start = System.currentTimeMillis(); 

        try {

            c = _panel.getHolder().lockCanvas(null); // this is the problem!!

            synchronized(_panel.getHolder()) {


                beginTime = System.currentTimeMillis();
                framesSkipped = 0;  // resetting the frames skipped

                    _panel.updatePhysics();                     
                    _panel.checkForHits();
                    _panel.checkForKill();
                    _panel.checkForMCHits();
                    //if i put stuff related to ui in the loop, it will slow the control down to a unblelieveable state 
                    //_panel.checkNPCQt();
                    _panel.onDraw(c);


                    timeDiff = System.currentTimeMillis() - beginTime;
                    // calculate sleep time
                    sleepTime = (int)(FRAME_PERIOD - timeDiff);

                    if (sleepTime > 0) {
                        // if sleepTime > 0 we're OK
                        /*
                        try {
                            // send the thread to sleep for a short period
                            // very useful for battery saving
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {}
                        */

                    }

                    while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
                        // we need to catch up
                        //_panel.updatePhysics(); // update without rendering
                        sleepTime += FRAME_PERIOD;  // add frame period to check if in next frame
                        framesSkipped++;
                    }

                    if (framesSkipped > 0) {
                        Log.d(TAG, "Skipped:" + framesSkipped);
                    }
                    // for statistics
                    framesSkippedPerStatCycle += framesSkipped;
                    // calling the routine to store the gathered statistics
                    storeStats();
            }

        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                _panel.getHolder().unlockCanvasAndPost(c);
                //Log.d(TAG, "unlock canvas");
            }
        }

i've pretty sure i got all my game logic and bitmaps are right; if i comment out all onDraw functions and everything else, i still got only 20-23 fps with framelayout there.

is there any alternatives? can create a separate thread for the UI draw? or am i just doing it wrong?

here is my XML state: enter image description here

tom91136
  • 8,662
  • 12
  • 58
  • 74

1 Answers1

0

Solved, i accidentally messed up the framelayout, clean project is the answer

tom91136
  • 8,662
  • 12
  • 58
  • 74