Changeset 605

Show
Ignore:
Timestamp:
07/17/08 00:09:34 (4 months ago)
Author:
tom
Message:

implemeting a tile pool for Map and TweenMap? ... if you override createTile you should check the Map and TweenMap? implementations to see how to take advantage of the pool

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/tom-tweenlite/lib/com/modestmaps/core/TileGrid.as

    r603 r605  
    164164 
    165165                        this.tileQueue = new TileQueue(); 
    166                         this.tileCache = new TileCache(); 
     166                        this.tileCache = new TileCache(map); 
    167167 
    168168                        doubleClickEnabled = true; 
     
    233233                         
    234234                        if (debugField.parent) { 
    235                                 debugField.text = "tx: " + tx.toFixed(3) + " ty: " + tx.toFixed(3) + " sc: " + scale.toFixed(4) 
     235                                debugField.text = "tx: " + tx.toFixed(3) 
     236                                                + "\nty: " + tx.toFixed(3) 
     237                                                + "\nsc: " + scale.toFixed(4) 
    236238                                                + "\nfps: " + fps.toFixed(0) 
    237239                                                + "\ncurrent child count: " + well.numChildren 
     
    242244                                                + "\nfinished tiles: " + tileCache.size 
    243245                                                + "\nrecently used tiles: " + recentlySeen.length 
     246                                                + "\nTiles created: " + map.tileCount 
    244247                                                + "\nmemory: " + (System.totalMemory/1048576).toFixed(1) + "MB"; 
    245248                                debugField.width = debugField.textWidth+8; 
    246249                                debugField.height = debugField.textHeight+4; 
     250                                debugField.x = mapWidth - debugField.width - 15;  
     251                                debugField.y = mapHeight - debugField.height - 15; 
    247252                        } 
    248253                         
     
    446451                        } 
    447452 
     453/*                      trace((maxCol - minCol + 1) * (maxRow - minRow + 1), "tiles visible at currentZoom"); 
     454                        trace(visibleTiles.length, " tiles visible"); */ 
     455 
    448456                        // make absolutlely sure all our newly visible tiles are cached if they're done loading 
    449457                        // TODO: this should probably happen onLoadEnd when there are no URLs left?  
     
    466474                                        well.removeChild(tile); 
    467475                                        tile.hide(); 
    468                                 } 
    469                         } 
     476                                        if (!tileCache.containsKey(tile.name)) { 
     477                                                //trace("destroying tile that was in the well but never cached"); 
     478                                                map.destroyTile(tile); 
     479                                        } 
     480                                } 
     481                        } 
     482 
     483                        //trace(well.numChildren, " tiles in the well"); 
    470484                                                 
    471485                        // sort children by difference from current zoom level 
     
    491505                                var tileScale:Number = Math.pow(2, zoomLevel-tile.zoom); 
    492506                                tileScale = Math.ceil(tileScale * tileWidth) / tileWidth; // round up to the nearest pixel 
    493                                 tile.scaleX = tile.scaleY = tileScale;                           
    494  
    495                                 if (tile.zoom != currentZoom && tile.alpha < 1) { 
    496                                         trace("showing now!"); 
    497                                         tile.showNow(); 
    498                                 } 
     507                                tile.scaleX = tile.scaleY = tileScale; 
    499508                        } 
    500509                         
    501510                        // all the visible tiles will be at the end of recentlySeen 
    502511                        // let's make sure we keep them around: 
    503                         var maxRecentlySeen:int = Math.max(visibleTiles.length,maxTilesToKeep); 
     512                        var maxRecentlySeen:int = Math.max(visibleTiles.length, maxTilesToKeep); 
     513                         
     514/*                      trace("maxRecentlySeen is", maxRecentlySeen); 
     515                        trace("recentlySeen.length is", recentlySeen.length); */ 
    504516                         
    505517                        // prune cache of already seen tiles if it's getting too big: 
    506518                        if (recentlySeen.length > maxRecentlySeen) { 
     519                                //trace("... and that's too big");                               
     520 
    507521                                // throw away keys at the beginning of recentlySeen 
    508                                 recentlySeen = recentlySeen.slice(recentlySeen.length - maxTilesToKeep, recentlySeen.length); 
     522                                recentlySeen = recentlySeen.slice(recentlySeen.length - maxRecentlySeen, recentlySeen.length); 
     523                                 
     524/*                              trace("recentlySeen.length is now", recentlySeen.length); 
     525                                 
     526                                trace("trimming cache from", tileCache.size); */ 
     527 
    509528                                // loop over our internal tile cache  
    510529                                // and throw out tiles not in recentlySeen 
    511530                                tileCache.retainKeys(recentlySeen);  
     531 
     532//                              trace("to", tileCache.size); 
    512533                        } 
    513534                         
     
    9791000                         
    9801001                        while (well.numChildren > 0) {                   
    981                                 well.removeChildAt(0); 
     1002                                var tile:Tile = well.removeChildAt(0) as Tile; 
     1003                                if (!tileCache.containsKey(tile.name)) { 
     1004                                        map.destroyTile(tile); 
     1005                                } 
    9821006                        } 
    9831007                         
     
    11801204 
    11811205import com.modestmaps.core.Tile; 
    1182 import flash.utils.Dictionary;   
     1206import flash.utils.Dictionary; 
     1207import com.modestmaps.Map;       
    11831208 
    11841209class TileQueue 
     
    12321257        // Tiles we've already seen and fully loaded, by key (.name) 
    12331258        protected var alreadySeen:Dictionary; 
    1234          
    1235         public function TileCache() 
    1236         { 
     1259        protected var map:Map; // for handing tiles back! 
     1260         
     1261        public function TileCache(map:Map) 
     1262        { 
     1263                this.map = map; 
    12371264                alreadySeen = new Dictionary(); 
    12381265        } 
     
    12671294                for (var key:String in alreadySeen) { 
    12681295                        if (keys.indexOf(key) < 0) { 
     1296                                map.destroyTile(alreadySeen[key] as Tile); 
    12691297                                delete alreadySeen[key]; 
    12701298                        } 
     
    12751303        { 
    12761304                for (var key:String in alreadySeen) { 
     1305                        map.destroyTile(alreadySeen[key] as Tile); 
    12771306                        delete alreadySeen[key]; 
    12781307                } 
  • branches/tom-tweenlite/lib/com/modestmaps/Map.as

    r600 r605  
    156156                        setExtent(extent);                       
    157157                } 
     158                 
     159                //addChild(grid.debugField); 
    158160        } 
    159161         
     
    841843        } 
    842844 
     845                protected var tilePool:Array = []; 
     846                public var tileCount:int = 0; 
     847 
     848                protected function getTileFromPool(column:int, row:int, zoom:int):Tile 
     849                { 
     850                if (tilePool.length > 0) { 
     851                        var tile:Tile = tilePool.shift() as Tile; 
     852                        tile.row = row; 
     853                        tile.column = column; 
     854                        tile.zoom = zoom; 
     855                        tile.hide(); 
     856                        return tile; 
     857                }                                                
     858                return null; 
     859                } 
     860 
    843861                /** override this if you want to create your own tiles */ 
    844862            public function createTile(column:int, row:int, zoom:int):Tile 
    845863            { 
     864                var tile:Tile = getTileFromPool(column, row, zoom); 
     865                if (tile) { 
     866                        return tile; 
     867                } 
     868                trace("made", tileCount++, "tiles"); 
    846869                return new Tile(column, row, zoom); 
     870            }         
     871 
     872                /** once TileGrid is done with a tile, it will hand it back to you */ 
     873            public function destroyTile(tile:Tile):void 
     874            { 
     875                while (tile.numChildren > 0) { 
     876                        tile.removeChildAt(0); 
     877                } 
     878                tilePool.push(tile); 
    847879            }         
    848880                 
  • branches/tom-tweenlite/lib/com/modestmaps/TweenMap.as

    r599 r605  
    257257                override public function createTile(column:int, row:int, zoom:int):Tile 
    258258                { 
     259                        var tile:Tile = getTileFromPool(column, row, zoom); 
     260                if (tile) { 
     261                        return tile; 
     262                }                        
     263                tileCount++; 
    259264                        return new TweenTile(column, row, zoom); 
    260265                }