1 /**************************************************************************** 2 Copyright (c) 2008-2010 Ricardo Quesada 3 Copyright (c) 2011-2012 cocos2d-x.org 4 Copyright (c) 2013-2014 Chukong Technologies Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * <p> 29 * cc.pool is a singleton object serves as an object cache pool.<br/> 30 * It can helps you to improve your game performance for objects which need frequent release and recreate operations<br/> 31 * Some common use case is : 32 * 1. Bullets in game (die very soon, massive creation and recreation, no side effect on other objects) 33 * 2. Blocks in candy crash (massive creation and recreation) 34 * etc... 35 * </p> 36 * 37 * @example 38 * var sp = new cc.Sprite("a.png"); 39 * this.addChild(sp); 40 * cc.pool.putInPool(sp); 41 * 42 * cc.pool.getFromPool(cc.Sprite, "a.png"); 43 * @class 44 * @name cc.pool 45 */ 46 cc.pool = /** @lends cc.pool# */{ 47 _pool: {}, 48 49 _releaseCB: function () { 50 this.release(); 51 }, 52 53 _autoRelease: function (obj) { 54 var running = obj._running === undefined ? false : !obj._running; 55 cc.director.getScheduler().schedule(this._releaseCB, obj, 0, 0, 0, running) 56 }, 57 58 /** 59 * Put the obj in pool 60 * @param obj 61 */ 62 putInPool: function (obj) { 63 var pid = obj.constructor.prototype.__pid; 64 if (!pid) { 65 var desc = { writable: true, enumerable: false, configurable: true }; 66 desc.value = ClassManager.getNewID(); 67 Object.defineProperty(obj.constructor.prototype, '__pid', desc); 68 } 69 if (!this._pool[pid]) { 70 this._pool[pid] = []; 71 } 72 // JSB retain to avoid being auto released 73 obj.retain && obj.retain(); 74 // User implementation for disable the object 75 obj.unuse && obj.unuse(); 76 this._pool[pid].push(obj); 77 }, 78 79 /** 80 * Check if this kind of obj has already in pool 81 * @param objClass 82 * @returns {boolean} if this kind of obj is already in pool return true,else return false; 83 */ 84 hasObject: function (objClass) { 85 var pid = objClass.prototype.__pid; 86 var list = this._pool[pid]; 87 if (!list || list.length === 0) { 88 return false; 89 } 90 return true; 91 }, 92 93 /** 94 * Remove the obj if you want to delete it; 95 * @param obj 96 */ 97 removeObject: function (obj) { 98 var pid = obj.constructor.prototype.__pid; 99 if (pid) { 100 var list = this._pool[pid]; 101 if (list) { 102 for (var i = 0; i < list.length; i++) { 103 if (obj === list[i]) { 104 // JSB release to avoid memory leak 105 obj.release && obj.release(); 106 list.splice(i, 1); 107 } 108 } 109 } 110 } 111 }, 112 113 /** 114 * Get the obj from pool 115 * @param args 116 * @returns {*} call the reuse function an return the obj 117 */ 118 getFromPool: function (objClass/*,args*/) { 119 if (this.hasObject(objClass)) { 120 var pid = objClass.prototype.__pid; 121 var list = this._pool[pid]; 122 var args = Array.prototype.slice.call(arguments); 123 args.shift(); 124 var obj = list.pop(); 125 // User implementation for re-enable the object 126 obj.reuse && obj.reuse.apply(obj, args); 127 // JSB release to avoid memory leak 128 cc.sys.isNative && obj.release && this._autoRelease(obj); 129 return obj; 130 } 131 }, 132 133 /** 134 * remove all objs in pool and reset the pool 135 */ 136 drainAllPools: function () { 137 for (var i in this._pool) { 138 for (var j = 0; j < this._pool[i].length; j++) { 139 var obj = this._pool[i][j]; 140 // JSB release to avoid memory leak 141 obj.release && obj.release(); 142 } 143 } 144 this._pool = {}; 145 } 146 };