Jump to content

Is it possible to keep Phaser updated at the same interval as RAF in the background?


WombatTurkey
 Share

Recommended Posts

After my topic here, I found a half-ass solution using Web Workers. It works, but it's glitchy.

I've been stuck on this for quite a while so I figured I'd make a topic.

Basically, I am trying to 'spoof' RAF when the game is minimized (if the game is minimized, RAF stops working). And since my entire game is multiplayer / runs off arcade physics, and making Phaser's update function run off of a setInterval timer creates weird behavior.

What I am basically doing now is if the game is minimized, I send a message to a Web Worker to start a setInterval timer and then that Worker sends a message to the main thread telling it to update the game. (Essentially a Spoof)

For example, inside my Workers/WorkerRenderer.js

var RequestAnimationFrameFixTimer, RequestAnimationFrameFix;

onmessage = function(e) {
    var message = e.data;

    if (message == 'Start') {
        RequestAnimationFrameFix();
    } else if (message == 'Stop') {
        if (RequestAnimationFrameFixTimer) {
            clearInterval(RequestAnimationFrameFixTimer);
        }
    }

};


RequestAnimationFrameFix = function() {

    RequestAnimationFrameFixTimer = setInterval(function() {
        postMessage(''); // Don't need to send any text, just notify the thread we are sending a message.
    }, 16);



};

 

And inside Phaser, underneath the create function:

	FixRenderer = new Worker('./js/src/Workers/WorkerRenderer.js');
	FixRenderer.onmessage = function(e) { // Receive the message from the worker to update game continuously 
	    game.update(Date.now()/4);
	}

	currentWindow.on('minimize', function(data) {
	    console.log('Browser minimized, RAF stopped!');
	    FixRenderer.postMessage('Start');
	});
	currentWindow.on('restore', function(data) {
	    console.log('Browser restored, RAF enabled!');
	    FixRenderer.postMessage('Stop');
	});

 

currentWindow is relating back to electron's remote.getCurrentWindow();

The problem is the game is getting updated fine, but not fast enough. It's not matching the speed of the client's FPS because RAF is not being used (Chromium disables RAF when you minimize the window), and relying on setInterval which is a fixed timer.

Another problem is I can lower the setInterval timer and make the game update faster, but then when you go back to your game it 'catches up' and there is a lag of several seconds. I'm trying to exploit time and failing. Any help is appreciated.

Edit: Just a little rant: I think the flux of 2d game development using WebGL is increasing so much why hasn't there been an option to let us keep RAF updated while the game is running in the background? I can understand the argument since Chromium is for browser's to save CPU / GPU usage, but the future of game development inside WebGL is just getting started. It's time for some changes! (This is directed to the Chromium team, not Phaser :P)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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