darkflame
darkflame

Reputation: 998

Libgdx Timer pause's on lost focus?

I noticed if I use GDXs Timer class on a default, newly created LibGDX project it will not fire at all while the application is minimized or not in focus. At least this is true on Desktop deployment Windows 10.

JAVAs own timer class, meanwhile, fires regardless of focus. A simple example app to demonstrate the difference;

@Override
public void create () {

    //gdx timer (does not update when focus lost);
    //------------------
    com.badlogic.gdx.utils.Timer.schedule(new com.badlogic.gdx.utils.Timer.Task(){
        @Override
        public void run() {                                
            long currentTime = System.currentTimeMillis();    
            Log.info("____GDX_____________________:"+currentTime+"_________");                
        }
    }
    , 0  
    , 1.0f ); 


    //java timer (updates when focus lost);
    //-----------------
    Timer test = new Timer();
    test.scheduleAtFixedRate(new TimerTask() {        
        @Override
        public void run() {
            long currentTime = System.currentTimeMillis();    
            Log.info("___JAVA____________________:"+currentTime+"_________");    
        }          
    }, 0, 1000);
}

Running this you can clearly see both logs firing when in focus, and only Javas when not in focus.

My questions are;

a) Is the behavior of LibGDXs timer expected, or have I done something wrong in setup? The description of LibGDXs timer doesn't seem to mention the auto-pause.

b) I wish my application to run in the background unless explicitly paused. Merely,say, alt+tabbing should not be enough. Should I just switch to using JAVAs timer? Does this have cross-platform implications?

Thanks, Darkflame

Upvotes: 2

Views: 324

Answers (1)

pr0gramista
pr0gramista

Reputation: 9038

a) Yes, the documentation of TimerThread, which handles Timer, says:

Manages the single timer thread. Stops thread on libgdx application pause and dispose, starts thread on resume.

b) Since libGDX Timer is nothing special than just a Thread, which listen for application change (pause, resume etc.), with list of tasks, it should be fine to use Java's Timer. Since it is from 1.3 (and the libGDX target is 1.6) it should not have any cross-platform implications.

Upvotes: 2

Related Questions