ivanix Posted July 25, 2014 Share Posted July 25, 2014 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 Link to comment Share on other sites More sharing options...
ivanix Posted July 25, 2014 Author Share Posted July 25, 2014 Hmm, looks like I didn't get the files attached on the first post.I keep forgetting to click on the 'Attach This File' button. savecpu.zip Brad P. Taylor 1 Link to comment Share on other sites More sharing options...
Brad P. Taylor Posted July 26, 2014 Share Posted July 26, 2014 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 More sharing options...
rich Posted July 28, 2014 Share Posted July 28, 2014 Sounds really neat. I've not looked at the source yet, but how does it work? I assume it doesn't render until something happens? Link to comment Share on other sites More sharing options...
ivanix Posted July 28, 2014 Author Share Posted July 28, 2014 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 More sharing options...
kroop Posted September 23, 2014 Share Posted September 23, 2014 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 More sharing options...
chg Posted September 23, 2014 Share Posted September 23, 2014 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 More sharing options...
kroop Posted September 23, 2014 Share Posted September 23, 2014 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 issueIt'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 More sharing options...
Pooya72 Posted July 7, 2015 Share Posted July 7, 2015 how this plugin works with currently latest version of phaser (2.3.0) ? geros 1 Link to comment Share on other sites More sharing options...
ivanix Posted January 30, 2016 Author Share Posted January 30, 2016 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 More sharing options...
ivanix Posted January 30, 2016 Author Share Posted January 30, 2016 Sincere apologies for not replying to prior questions and comments, I have been out of the loop for a while : ( Link to comment Share on other sites More sharing options...
ivanix Posted January 31, 2016 Author Share Posted January 31, 2016 To my chagrin, I just found that SaveCPU did not work on safari because window.performance.now() is not supported. Revised to use hires timestamp from window.requestAnimationFrame() new pull request: https://github.com/photonstorm/phaser-plugins/pull/16 Link to comment Share on other sites More sharing options...
caiser Posted February 14, 2016 Share Posted February 14, 2016 So how do we use this plugin ? A simple 'this.game.plugins.add(Phaser.Plugin.SaveCPU)' ? The Github page has no documentation in it. Thanks Link to comment Share on other sites More sharing options...
rgk Posted February 15, 2016 Share Posted February 15, 2016 Caiser, you add it to your create or init for a stage. Also amazing plugin, really gave me better performance on my game. (which I should be releasing real soon!) Link to comment Share on other sites More sharing options...
caiser Posted February 15, 2016 Share Posted February 15, 2016 Thanks rgk, And is there a way to stop it when needed ? I want it to work in some states and not others. Link to comment Share on other sites More sharing options...
rgk Posted February 16, 2016 Share Posted February 16, 2016 I think its only enabled in the state you start it in, but I'm not sure if that enables it from that point on. Link to comment Share on other sites More sharing options...
ivanix Posted February 23, 2016 Author Share Posted February 23, 2016 @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 More sharing options...
ivanix Posted February 23, 2016 Author Share Posted February 23, 2016 @rgk, thanks for the compliments : ) Link to comment Share on other sites More sharing options...
TheSNGuy Posted September 2, 2016 Share Posted September 2, 2016 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 More sharing options...
Recommended Posts