@@ -583,6 +583,33 @@ cc.path = /** @lends cc.path# */{
583583 * @see cc.loader
584584 */
585585
586+ var imagePool = {
587+ _pool : new Array ( 10 ) ,
588+ _MAX : 10 ,
589+ _smallImg : "data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=" ,
590+
591+ count : 0 ,
592+ get : function ( ) {
593+ if ( this . count > 0 ) {
594+ this . count -- ;
595+ var result = this . _pool [ this . count ] ;
596+ this . _pool [ this . count ] = null ;
597+ return result ;
598+ }
599+ else {
600+ return new Image ( ) ;
601+ }
602+ } ,
603+ put : function ( img ) {
604+ var pool = this . _pool ;
605+ if ( img instanceof HTMLImageElement && this . count < this . _MAX ) {
606+ img . src = this . _smallImg ;
607+ pool [ this . count ] = img ;
608+ this . count ++ ;
609+ }
610+ }
611+ } ;
612+
586613/**
587614 * Singleton instance of cc.Loader.
588615 * @name cc.loader
@@ -867,7 +894,7 @@ cc.loader = (function () {
867894 * @param {function } callback
868895 * @returns {Image }
869896 */
870- loadImg : function ( url , option , callback ) {
897+ loadImg : function ( url , option , callback , img ) {
871898 var opt = {
872899 isCrossOrigin : true
873900 } ;
@@ -876,30 +903,22 @@ cc.loader = (function () {
876903 else if ( option !== undefined )
877904 callback = option ;
878905
879- var img = this . getRes ( url ) ;
880- if ( img ) {
881- callback && callback ( null , img ) ;
882- return img ;
883- }
884-
885906 var queue = _queue [ url ] ;
886907 if ( queue ) {
887908 queue . callbacks . push ( callback ) ;
888909 return queue . img ;
889910 }
890911
891- img = new Image ( ) ;
912+ img = img || imagePool . get ( ) ;
892913 if ( opt . isCrossOrigin && location . origin !== "file://" )
893914 img . crossOrigin = "Anonymous" ;
915+ else
916+ img . crossOrigin = null ;
894917
895918 var loadCallback = function ( ) {
896919 this . removeEventListener ( 'load' , loadCallback , false ) ;
897920 this . removeEventListener ( 'error' , errorCallback , false ) ;
898921
899- if ( ! _urlRegExp . test ( url ) ) {
900- cc . loader . cache [ url ] = img ;
901- }
902-
903922 var queue = _queue [ url ] ;
904923 if ( queue ) {
905924 var callbacks = queue . callbacks ;
@@ -912,17 +931,21 @@ cc.loader = (function () {
912931 queue . img = null ;
913932 delete _queue [ url ] ;
914933 }
934+
935+ if ( cc . _renderType === cc . game . RENDER_TYPE_WEBGL ) {
936+ imagePool . put ( img ) ;
937+ }
915938 } ;
916939
917940 var self = this ;
918941 var errorCallback = function ( ) {
919942 this . removeEventListener ( 'load' , loadCallback , false ) ;
920943 this . removeEventListener ( 'error' , errorCallback , false ) ;
921944
922- if ( img . crossOrigin && img . crossOrigin . toLowerCase ( ) === "anonymous" ) {
945+ if ( window . location . protocol !== 'https:' && img . crossOrigin && img . crossOrigin . toLowerCase ( ) === "anonymous" ) {
923946 opt . isCrossOrigin = false ;
924947 self . release ( url ) ;
925- cc . loader . loadImg ( url , opt , callback ) ;
948+ cc . loader . loadImg ( url , opt , callback , img ) ;
926949 } else {
927950 var queue = _queue [ url ] ;
928951 if ( queue ) {
@@ -936,6 +959,10 @@ cc.loader = (function () {
936959 queue . img = null ;
937960 delete _queue [ url ] ;
938961 }
962+
963+ if ( cc . _renderType === cc . game . RENDER_TYPE_WEBGL ) {
964+ imagePool . put ( img ) ;
965+ }
939966 }
940967 } ;
941968
0 commit comments