Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : game loop pause/resume issue

I am able to pause/resume my game by making my game thread(which holds/synchronized with surfaceview) wait/notifyAll. This all runs well using the in game pause button.

However, when I click home/back button I am able to pause my game, but when I resumes my game by clicking on its icon, I receive non responsive game screen back to me.

I have put logs on OnResume() method but nothing gets printed in LogCat! If I click on my game screen I get error dialog to "Force Close" or "Wait" on my game activity.

Why I am getting this non responsive screen as if my application Hangged after pause ? How can I handle physical buttons in same way as inGame pause/resume button ?

here is my logcat view of the operation. you can see onResume sop not getting printed similar to onPause.

LogCat View

EDIT : Code called by my pause/resume button and by onPause/onResume functions where "this" is thread class itself

protected void setPause(){
        synchronized (this) {
            isPaused = true;
                }
    }

    protected void setResume(){
        synchronized (this) {
            isPaused = false;
            this.notifyAll();
        }
    }

Game Loop Code :

synchronized (this) {
            while (running) {
                if (isPaused) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    Canvas c = null;

                    try {
                        c = view.getHolder().lockCanvas();
                        synchronized (view.getHolder()) {
                            view.onDraw(c);
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } finally {
                        if (c != null) {
                            view.getHolder().unlockCanvasAndPost(c);
                        }
                    }
                    sleepTime = ticksPS
                            - (System.currentTimeMillis() - startTime);
                    try {
                        if (sleepTime > 0)
                            sleep(sleepTime);
                        else
                            sleep(10);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

This how I am calling it from surface view :

public void surfaceCreated(SurfaceHolder holder) {

                // start the game loop
                System.out.println("Starting Game Loop @@@@@@@@@");
                if(isPaused){
                    System.out.println("GamePaused");
                    gameLoopThread.setResume();
                }
                else{
                    System.out.println("Game Starting");
                    gameLoopThread.setRunning(true);
                }
                gameLoopThread.start();

            }

EDIT 2: If I Pause my game using pause button and let my phone get locked automatically... after resuming I am able to play normally with out any issue. As per logs I see surface destroyed is not getting called in this case.

like image 865
Abhinav Tyagi Avatar asked Jun 01 '26 16:06

Abhinav Tyagi


1 Answers

After onResume, your SurfaceView is created again, but it lost all the data it had before (all info needed for onDraw). you have to save the data in surfaceDestroyed or before, and reload it in the onCreate of your surfaceview

like image 132
Tamir Scherzer Avatar answered Jun 04 '26 11:06

Tamir Scherzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!