Jump to content

New Plugin: SaveCPU


ivanix
 Share

Recommended Posts

Hi everyone,

 

Today, I created my first plugin for Phaser called SaveCPU (savecpu.js).

 

A few months back, I noticed rather high cpu usage when running phaser games.

Recently, I also found high cpu when using 2.0.7,  so I decide to create a plugin that could be added retroactively  to games using prior versions of phaser.

 

Use case scenarios include casual/puzzle games,  menus that wait for user input, or for applications such as a slideshow where a lower frame rate can be used.

 

So I think that not only should it reduce CPU and keep my notebook cooler, it should also help save battery on mobile devices.

 

The plugin should work on phaser versions 2.0.3 to current of 2.0.7.

Attached zip contains a couple samples for testing CPU usage before and after applying the saveCPU plugin.

 

I'll issue a  pull request as soon as I can figure out  how githubbing works :P

Link to comment
Share on other sites

ivanix,maybe you can start with a github gist?

 

I'm very new to the whole putting code online for anyone to see/use. Setting up a repository for a single file module also seemed like overkill to me so I put off posting any code until recently when I discovered gists. I had no idea they were so easy to setup and use. 

 

https://help.github.com/articles/creating-gists

 

-bpt

Link to comment
Share on other sites

Hi Rich,

 

When the plugin determines nothing should be rendered it sets  renderType to HEADLESS.  

There are three ways to determine when to force a render.

Render on max FPS set, render on pointer changes, or manually force a render.

 

I went ahead and made a pull request on the phaser-plugins repo,  hope I did it correctly :)

 

BR

Link to comment
Share on other sites

  • 1 month later...

Hey, I just took this into use and noticed massive improvement what it comes to my laptop ability to handle Phaser content. The problem was that the integrated gpu overheated also the cpu even without heavy load. But few questions:

 

1. The default setting (30fps) seems to be in place by just loading the plugin 

this.game.plugins.add(Phaser.Plugin.SaveCPU);

I'd like to think I have quite trained eye what it comes to fps and to me the default seems worse than 30. Even changing it to 31 seems better. Am I crazy (it's possible) or can this have something to do with the browser rendering / sync with it? If so, what's the best way to find the "sweet spot"?

2. My current project is basically based on states, events and tweens. A lot of static screens that are animated into next state by using tweens when an event is triggered. So I don't use the update functions basically at all. I tried to poke the renderOnFPS and renderOnPointerChange but with little success. Is it somehow possible to use this so that the fps is zero by default, but when an event is fired it's set to 60 and when the tween is done back to 0?

Thanks!

Link to comment
Share on other sites

I'd like to think I have quite trained eye what it comes to fps and to me the default seems worse than 30. Even changing it to 31 seems better. Am I crazy (it's possible) or can this have something to do with the browser rendering / sync with it? If so, what's the best way to find the "sweet spot"?

 

I have had a look at the code on Github as shared by Lewster32 in your other thread, and looking at the forceRenderOnFPS function:

Phaser.Plugin.SaveCPU.prototype.forceRenderOnFPS = function () {    'use strict';        var ts, diff;    ts = window.performance.now();    diff = ts - this.now;    if (diff < (1000 / this.renderOnFPS)) {        return false;    }    this.now = ts;    this.forceRender();    return true;};

I think you might be right, assuming this is called at around 60 times a second and renderOnFPS is 30, there are bound to be times where diff is just a few milliseconds smaller than the target frame rate causing 2rd 60Hz frame to be skipped in a row.

Note: the extra time when a frame is drawn is discarded rather than being accumulated

 

In the case where requestFrameAnimation is making the Phaser loop run at around 60 frames a second, renderOnFPS vales greater than 30 (but still much less than 60) surely increase the chance you'll render every 2nd frame*

 

Do you see any difference between say renderOnFPS = 31 and renderOnFPS = 50?*

 

EDIT: Just re-read this, to clarify I'm not suggesting you should do this as a workaround, but rather to confirm the issue

Link to comment
Share on other sites

Do you see any difference between say renderOnFPS = 31 and renderOnFPS = 50?*

 

EDIT: Just re-read this, to clarify I'm not suggesting you should do this as a workaround, but rather to confirm the issue

It's definitely harder to see difference between 31 and 50 than 30 and 31. Obviously this is highly unscientific measurement-by-eye, but still.

Link to comment
Share on other sites

  • 9 months later...
  • 6 months later...
On 7/7/2015 at 4:33 AM, Pooya72 said:

how this plugin works with currently latest version of phaser (2.3.0) ? 

Hello all,

I just updated SaveCPU to work with phaser 2.4.4.

pull request: https://github.com/photonstorm/phaser-plugins/pull/15

demo1: Egg Man Walking (still image)

without savecpu: http://www.ivanix.com/ivanix4games/samples/samplePhaser.html

with savecpu: http://www.ivanix.com/ivanix4games/samples/sampleSaveCpu.html

demo2: Egg Man Walking (animation)

without savecpu: http://www.ivanix.com/ivanix4games/samples/sampleCjs2Phaser.html

with savecpu: http://www.ivanix.com/ivanix4games/samples/sampleSaveCpu2.html

best regards

Link to comment
Share on other sites

  • 2 weeks later...

@caiser,  once it is added to the game, it is enabled for all states in the game I believe.  

To ensure rendering happens all the time,  simply call forceRender() in an update function.

At the moment, it is a bit cumbersome to completely remove from the game engine after it is added, but possible.

Referencing one of the examples, http://www.ivanix.com/ivanix4games/samples/sampleSaveCpu.js ,  can safely remove as follows:

game.plugins.remove(saveCpu);  

saveCpu.game = game;

saveCpu.forceRender();

saveCpu.switchRender();

Note the object referenced by saveCpu variable cannot be re-added to the game.  A new saveCpu object will need to be created as before.

 

Link to comment
Share on other sites

  • 6 months later...

Has anyone used SaveCPU on a recent version of Phaser?

I'm using SaveCPU with 2.5 and I'm running into this issue where I'm occasionally getting a single dead frame (all black) wanted to check if anyone else has run into this and/or has a solution.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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