SaveCPU.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2014 Ivanix Mobile LLC
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. /*
  26. * Plugin: SaveCPU
  27. * Author: Ivanix @ Ivanix Mobile LLC
  28. * Purpose: Reduce CPU usage caused from redudant rendering
  29. * on idle or static display scenes
  30. * reduce fps for casual/puzzle games
  31. *
  32. *
  33. * Configurable Properties:
  34. *
  35. * [renderOnFPS]
  36. * Constrains maximum FPS to value set.
  37. * Reasonable values from 0 to 60
  38. * Default value 30
  39. * Set value to 0 disable rendering based on FPS
  40. * and use methods described below.
  41. *
  42. * [renderOnPointerChange]
  43. * Render when pointer movement detected.
  44. * Possible values "true" or "false"
  45. * Default: false
  46. * Note that renderOnFPS must be set to 0
  47. *
  48. *
  49. * Callable Methods:
  50. *
  51. * [forceRender()]
  52. * Forces rendering during core game loop
  53. * Can be called independently or in tandem with above properties.
  54. * Should be called inside update function.
  55. * @class Phaser.Plugin.SaveCPU
  56. */
  57. /*global
  58. Phaser: true,
  59. window: true
  60. */
  61. /*jslint nomen: true */
  62. Phaser.Plugin.SaveCPU = function (game, parent) {
  63. 'use strict';
  64. Phaser.Plugin.call(this, game, parent);
  65. };
  66. Phaser.Plugin.SaveCPU.prototype = Object.create(Phaser.Plugin.prototype);
  67. Phaser.Plugin.SaveCPU.constructor = Phaser.Plugin.SaveCPU;
  68. Phaser.Plugin.SaveCPU.prototype.init = function () {
  69. 'use strict';
  70. var thisObj;
  71. this.__defineSetter__("renderOnFPS", function(v) {
  72. this._renderOnFPS = v;
  73. this._tsdiff = (1000 / v);
  74. });
  75. this.__defineGetter__("renderOnFPS", function() {
  76. return this._renderOnFPS;
  77. });
  78. this._tsdiff = 0;
  79. // fps default
  80. this.renderOnFPS = 30;
  81. this.renderOnPointerChange = false;
  82. this.renderDirty = true;
  83. if(this.game.updateRender) {
  84. this._init1();
  85. } else {
  86. this._init0();
  87. }
  88. this.fpsDirty = 0;
  89. this.hrts = 0;
  90. thisObj = this;
  91. window.requestAnimationFrame(function(hrts) {
  92. thisObj._trackFPS(hrts);
  93. });
  94. };
  95. Phaser.Plugin.SaveCPU.prototype._init0 = function () {
  96. this.renderType = this.game.renderType;
  97. this.switchRender = this._switchRender0;
  98. };
  99. Phaser.Plugin.SaveCPU.prototype._init1 = function () {
  100. var game = this.game;
  101. game.updateRenderReal = game.updateRender;
  102. game.updateRenderNull = function() {};
  103. this.switchRender = this._switchRender1;
  104. };
  105. Phaser.Plugin.SaveCPU.prototype._trackFPS = function (hrts) {
  106. var thisObj, diff;
  107. diff = hrts - this.hrts;
  108. if (diff > this._tsdiff) {
  109. this.fpsDirty = true;
  110. this.hrts = hrts;
  111. }
  112. thisObj = this;
  113. window.requestAnimationFrame(function(hrts) {
  114. thisObj._trackFPS(hrts);
  115. });
  116. };
  117. Phaser.Plugin.SaveCPU.prototype._switchRender0 = function () {
  118. 'use strict';
  119. var game = this.game;
  120. if (this.renderDirty) {
  121. game.renderType = this.renderType;
  122. } else {
  123. game.renderType = Phaser.HEADLESS;
  124. }
  125. this.renderDirty = false;
  126. };
  127. Phaser.Plugin.SaveCPU.prototype._switchRender1 = function () {
  128. 'use strict';
  129. var game = this.game;
  130. if (this.renderDirty) {
  131. game.updateRender = game.updateRenderReal;
  132. } else {
  133. game.updateRender = game.updateRenderNull;
  134. }
  135. };
  136. Phaser.Plugin.SaveCPU.prototype.forceRender = function () {
  137. 'use strict';
  138. this.renderDirty = true;
  139. };
  140. Phaser.Plugin.SaveCPU.prototype._forceRenderOnPointerChange = function () {
  141. 'use strict';
  142. if(!this.renderOnPointerChange) {
  143. return false;
  144. }
  145. var input = this.game.input;
  146. if (input.oldx !== input.x || input.oldy !== input.y) {
  147. this.forceRender();
  148. input.oldx = input.x;
  149. input.oldy = input.y;
  150. }
  151. if (input.oldDown !== input.activePointer.isDown) {
  152. this.forceRender();
  153. input.oldDown = input.activePointer.isDown;
  154. }
  155. };
  156. Phaser.Plugin.SaveCPU.prototype._forceRenderOnFPS = function () {
  157. 'use strict';
  158. if(this.renderOnFPS && this.fpsDirty) {
  159. this.fpsDirty = false;
  160. this.forceRender();
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. };
  166. Phaser.Plugin.SaveCPU.prototype.postUpdate = function () {
  167. 'use strict';
  168. if (this.renderDirty || this._forceRenderOnFPS()|| this._forceRenderOnPointerChange()) {
  169. this.switchRender();
  170. return;
  171. }
  172. };
  173. Phaser.Plugin.SaveCPU.prototype.postRender= function () {
  174. 'use strict';
  175. if (this.renderDirty) {
  176. this.renderDirty = false;
  177. this.switchRender();
  178. }
  179. };