| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /*
- * The MIT License (MIT)
- *
- * Copyright (c) 2014 Ivanix Mobile LLC
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- /*
- * Plugin: SaveCPU
- * Author: Ivanix @ Ivanix Mobile LLC
- * Purpose: Reduce CPU usage caused from redudant rendering
- * on idle or static display scenes
- * reduce fps for casual/puzzle games
- *
- *
- * Configurable Properties:
- *
- * [renderOnFPS]
- * Constrains maximum FPS to value set.
- * Reasonable values from 0 to 60
- * Default value 30
- * Set value to 0 disable rendering based on FPS
- * and use methods described below.
- *
- * [renderOnPointerChange]
- * Render when pointer movement detected.
- * Possible values "true" or "false"
- * Default: false
- * Note that renderOnFPS must be set to 0
- *
- *
- * Callable Methods:
- *
- * [forceRender()]
- * Forces rendering during core game loop
- * Can be called independently or in tandem with above properties.
- * Should be called inside update function.
- * @class Phaser.Plugin.SaveCPU
- */
- /*global
- Phaser: true,
- window: true
- */
- /*jslint nomen: true */
- Phaser.Plugin.SaveCPU = function (game, parent) {
- 'use strict';
- Phaser.Plugin.call(this, game, parent);
- };
- Phaser.Plugin.SaveCPU.prototype = Object.create(Phaser.Plugin.prototype);
- Phaser.Plugin.SaveCPU.constructor = Phaser.Plugin.SaveCPU;
-
- Phaser.Plugin.SaveCPU.prototype.init = function () {
- 'use strict';
- var thisObj;
- this.__defineSetter__("renderOnFPS", function(v) {
- this._renderOnFPS = v;
- this._tsdiff = (1000 / v);
- });
- this.__defineGetter__("renderOnFPS", function() {
- return this._renderOnFPS;
- });
- this._tsdiff = 0;
- // fps default
- this.renderOnFPS = 30;
- this.renderOnPointerChange = false;
- this.renderDirty = true;
-
- if(this.game.updateRender) {
- this._init1();
- } else {
- this._init0();
- }
-
- this.fpsDirty = 0;
- this.hrts = 0;
- thisObj = this;
- window.requestAnimationFrame(function(hrts) {
- thisObj._trackFPS(hrts);
- });
- };
- Phaser.Plugin.SaveCPU.prototype._init0 = function () {
- this.renderType = this.game.renderType;
- this.switchRender = this._switchRender0;
- };
- Phaser.Plugin.SaveCPU.prototype._init1 = function () {
- var game = this.game;
- game.updateRenderReal = game.updateRender;
- game.updateRenderNull = function() {};
- this.switchRender = this._switchRender1;
- };
- Phaser.Plugin.SaveCPU.prototype._trackFPS = function (hrts) {
- var thisObj, diff;
- diff = hrts - this.hrts;
- if (diff > this._tsdiff) {
- this.fpsDirty = true;
- this.hrts = hrts;
- }
-
- thisObj = this;
- window.requestAnimationFrame(function(hrts) {
- thisObj._trackFPS(hrts);
- });
-
- };
- Phaser.Plugin.SaveCPU.prototype._switchRender0 = function () {
- 'use strict';
- var game = this.game;
- if (this.renderDirty) {
- game.renderType = this.renderType;
- } else {
- game.renderType = Phaser.HEADLESS;
- }
- this.renderDirty = false;
- };
- Phaser.Plugin.SaveCPU.prototype._switchRender1 = function () {
- 'use strict';
- var game = this.game;
- if (this.renderDirty) {
- game.updateRender = game.updateRenderReal;
- } else {
- game.updateRender = game.updateRenderNull;
- }
- };
- Phaser.Plugin.SaveCPU.prototype.forceRender = function () {
- 'use strict';
- this.renderDirty = true;
- };
- Phaser.Plugin.SaveCPU.prototype._forceRenderOnPointerChange = function () {
- 'use strict';
- if(!this.renderOnPointerChange) {
- return false;
- }
- var input = this.game.input;
- if (input.oldx !== input.x || input.oldy !== input.y) {
- this.forceRender();
- input.oldx = input.x;
- input.oldy = input.y;
- }
- if (input.oldDown !== input.activePointer.isDown) {
- this.forceRender();
- input.oldDown = input.activePointer.isDown;
- }
- };
- Phaser.Plugin.SaveCPU.prototype._forceRenderOnFPS = function () {
- 'use strict';
-
- if(this.renderOnFPS && this.fpsDirty) {
- this.fpsDirty = false;
- this.forceRender();
- return true;
- } else {
- return false;
- }
- };
- Phaser.Plugin.SaveCPU.prototype.postUpdate = function () {
- 'use strict';
- if (this.renderDirty || this._forceRenderOnFPS()|| this._forceRenderOnPointerChange()) {
- this.switchRender();
- return;
- }
- };
- Phaser.Plugin.SaveCPU.prototype.postRender= function () {
- 'use strict';
- if (this.renderDirty) {
- this.renderDirty = false;
- this.switchRender();
- }
- };
|