Jump to content

What's wrong with my pause code?


cartman
 Share

Recommended Posts

I wrote a simple function to pause rendering.

window.addEventListener('blur',function(){
  _ENGINE.stopRenderLoop()
  console.log('paused');
});
window.addEventListener('focus',function(){
  _ENGINE.runRenderLoop(function(){
    _SCENE.render();    
  })
  console.log('resumed')
});

When the playground code is running, i tried to pause the game by clicking the outside elements of my web browser (you can click on the console log, or the URL tab to activate the pause).

The funny thing is, when I click on the scene to resume the rendering, the cube has already travelled a lot. It's as it the computations for the cube (its animations) wasn't paused.

Playground link: http://www.babylonjs-playground.com/#14EGUT#23

Any ideas?

Link to comment
Share on other sites

I'm very new to babylon and not really familiar with the API and how it works in the background, with that said:

From a first look it seems the render loop is separated from the animations, in other words I believe the animations are being played in the background but are simply not rendered when you pause.

Try using scene.stopAnimation(cube) and then start it again from the frame it stopped on resume.

Link to comment
Share on other sites

@royibernthal if I do scene.stopAnimation(cube), how do I resume it upon regaining focus? 

The Scene class doesn't have any resumeAnimation method.

I have a feeling this requires scene.beginAnimation(target, from, to, loop, speedRatio), but i don't know how to implement it (there's the 'from', and 'to' variables i'm missing). i.e how to get the last position it's on.

 

Link to comment
Share on other sites

thanks @Deltakosh, i modified the code to abstract the animatable as a global.

Somehow the bug persists. I paused the game (made it lose window focus), then resumed. It appears the cube has already travelled far far ahead.

Code: http://www.babylonjs-playground.com/#14EGUT#28

// Assign animatable to global var
_ANIMATABLE = scene.beginAnimation(cube, 0, frame, true);


// Pause/resume code
window.addEventListener('blur',function(){
	_ENGINE.stopRenderLoop()
	_ANIMATABLE.pause();
  console.log('paused');
});
window.addEventListener('focus',function(){
	_ENGINE.runRenderLoop(function () {
	_ANIMATABLE.restart();
    _SCENE.render();    
  })
  console.log('resumed')
});

 

Link to comment
Share on other sites

10 hours ago, Deltakosh said:

Because you cannot stop the engine if you want to pause animations :)

http://www.babylonjs-playground.com/#14EGUT#29

@Deltakosh thanks... is there any way to pause both engine and animations at the same time? My goal is to avoid my computer/laptop being too overloaded with activity (the CPU gets hot really fast after running the code for about 1-2 minutes).

With the engine paused, it worked really well for my CPU (but unfortunately the anims weren't paused).

Link to comment
Share on other sites

@cartmanFor my understanding it is actually stops the engine (runRenderLoop). which is good, when you start research in babylon documentation and switch tabs.
 

I do not know whether you 've noticed , but babylonjs uses webgl. webgl relies on the gpu.
But yeah, if i watch ytube vids or something its heating up. Make sure your code is clean and your endusers make a short break, after playing your games for hours :)



@cartman you got a bug, i have no idea why the animation keeps on processing.
keep me uptodate if you continue on working on renderEvenInBackground.

Good Luck

Link to comment
Share on other sites

@Nabroski thanks for the tips. I'm still unclear what to do.

My goal is: when user switch tabs (does something else), i need to pause (or stop) the engine entirely (stop render, stop calculation, stop animation), so the user's device stays cool and happy.

This is the best i come up with: http://www.babylonjs-playground.com/#14EGUT#34 (it appears the animation code is still running, because after resuming, the cube has already travelled far far ahead..... According to Deltakosh, it's one or the other :( )

Any ideas?

 

Link to comment
Share on other sites

I tested that by switching between tabs.

I just remembered that I removed your pause code.  It should be pretty easy to add it back.  Just remember that when you restart to set the BABYLON.Tools.resetLastTime to true or you code set the lastTime yourself BABYLON.Tools.lastTime = window.performance.now();.  

I just pasted your pause code in and it appears to work.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...