+
+ cleanTextures: function() {
+ // Get rid of unneeded textures to save memory.
+ // TODO: Be less aggressive, maybe keep neighboring zoom levels (but x/y coords there are zoom-specific).
+ if (!gMap.glTexturesPerZoomLevel) {
+ // Calculate how many textures we need to keep for one zoom level.
+ // ceil(width/size) gives us the minimum, keep one on either side as well.
+ gMap.glTexturesPerZoomLevel =
+ Math.ceil(gMap.gl.drawingBufferWidth / gMap.tileSize + 2) *
+ Math.ceil(gMap.gl.drawingBufferHeight / gMap.tileSize + 2);
+ console.log("Keeping " + gMap.glTexturesPerZoomLevel + " textures per level");
+ }
+ if (Object.keys(gMap.glTextures).length > gMap.glTexturesPerZoomLevel) {
+ console.log("Cleaning textures... (have " + Object.keys(gMap.glTextures).length + " atm)");
+
+ // Find coordinate ranges for tiles to keep.
+ var tMin = normalizeCoords({x: Math.floor((gMap.pos.x - gMap.baseDim.wid / 2) / gMap.baseDim.tsize) - 1,
+ y: Math.floor((gMap.pos.y - gMap.baseDim.ht / 2) / gMap.baseDim.tsize) - 1,
+ z: gMap.pos.z});
+ var tMax = normalizeCoords({x: Math.ceil((gMap.pos.x + gMap.baseDim.wid / 2) / gMap.baseDim.tsize) + 1,
+ y: Math.ceil((gMap.pos.y + gMap.baseDim.ht / 2) / gMap.baseDim.tsize) + 1,
+ z: gMap.pos.z});
+ console.log("In range: " + tMin.x + "," + tMin.y + "," + tMin.z + " - " + tMax.x + "," + tMax.y + "," + tMax.z);
+ for (aTileKey in gMap.glTextures) {
+ var keyMatches = aTileKey.match(/([^:]+)::(\d+),(\d+),(\d+)/);
+ if (keyMatches && keyMatches[1] != "loading") {
+ var txData = {
+ style: keyMatches[1],
+ x: keyMatches[2],
+ y: keyMatches[3],
+ z: keyMatches[4],
+ }
+ var delTx = false;
+ if (txData.style != gMap.activeMap) { delTx = true; console.log("Different map style: " + txData.style); }
+ if (!delTx && (txData.z < tMin.z || txData.z > tMax.z)) { delTx = true; console.log("Out-of-range zoom: " + txData.z); }
+ if (tMin.x < tMax.x) {
+ if (!delTx && (txData.x < tMin.x || txData.x > tMax.x)) { delTx = true; console.log("Out-of-range X: " + txData.x); }
+ }
+ else {
+ // We are crossing over the 0 coordinate!
+ if (!delTx && (txData.x < tMin.x && txData.x > tMax.x)) { delTx = true; console.log("Out-of-range X: " + txData.x); }
+ }
+ if (tMin.y < tMax.y) {
+ if (!delTx && (txData.y < tMin.y || txData.y > tMax.y)) { delTx = true; console.log("Out-of-range Y: " + txData.y); }
+ }
+ else {
+ // We are crossing over the 0 coordinate!
+ if (!delTx && (txData.y < tMin.y && txData.y > tMax.y)) { delTx = true; console.log("Out-of-range Y: " + txData.y); }
+ }
+ if (delTx) {
+ // Delete texture from GL and from the array we are holding.
+ gMap.gl.deleteTexture(gMap.glTextures[aTileKey]);
+ delete gMap.glTextures[aTileKey];
+ }
+ }
+ }
+ console.log("Cleaning complete, " + Object.keys(gMap.glTextures).length + " textures left)");
+ //clearInterval(gMap.glTxCleanIntervalID);
+ }
+ },