add function to get visible tiles only

This commit is contained in:
Hannes Janetzek 2013-02-04 01:02:54 +01:00
parent f3f1c4ae54
commit 0d87aabd3d

View File

@ -320,6 +320,8 @@ public class GLRenderer implements GLSurfaceView.Renderer {
} }
} }
private static Object tilelock = new Object();
static void draw() { static void draw() {
long start = 0; long start = 0;
@ -372,27 +374,31 @@ public class GLRenderer implements GLSurfaceView.Renderer {
MapTile[] tiles = mDrawTiles.tiles; MapTile[] tiles = mDrawTiles.tiles;
if (changed) { if (changed) {
// get visible tiles synchronized (GLRenderer.tilelock) {
for (int i = 0; i < tileCnt; i++)
tiles[i].isVisible = false;
// relative zoom-level, 'tiles' could not have been updated after // get visible tiles
// zoom-level changed. for (int i = 0; i < tileCnt; i++)
byte z = tiles[0].zoomLevel; tiles[i].isVisible = false;
float div = FastMath.pow(z - pos.zoomLevel);
// transform screen coordinates to tile coordinates // relative zoom-level, 'tiles' could not have been updated after
float scale = pos.scale / div; // zoom-level changed.
float px = (float) pos.x * div; byte z = tiles[0].zoomLevel;
float py = (float) pos.y * div; float div = FastMath.pow(z - pos.zoomLevel);
if (div != 1)
Log.d(TAG, "tiles not from current zoom level");
// transform screen coordinates to tile coordinates
float scale = pos.scale / div;
float px = (float) pos.x * div;
float py = (float) pos.y * div;
for (int i = 0; i < 8; i += 2) { for (int i = 0; i < 8; i += 2) {
coords[i + 0] = (px + coords[i + 0] / scale) / Tile.TILE_SIZE; coords[i + 0] = (px + coords[i + 0] / scale) / Tile.TILE_SIZE;
coords[i + 1] = (py + coords[i + 1] / scale) / Tile.TILE_SIZE; coords[i + 1] = (py + coords[i + 1] / scale) / Tile.TILE_SIZE;
}
mHolderCount = 0;
mScanBox.scan(coords, z);
} }
mHolderCount = 0;
mScanBox.scan(coords, z);
} }
tileCnt += mHolderCount; tileCnt += mHolderCount;
@ -506,6 +512,40 @@ public class GLRenderer implements GLSurfaceView.Renderer {
return ((t.tileX % 4) + (t.tileY % 4 * 4) + 1); return ((t.tileX % 4) + (t.tileY % 4 * 4) + 1);
} }
public static TileSet getVisibleTiles(TileSet td) {
if (mDrawTiles == null)
return td;
MapTile[] newTiles = mDrawTiles.tiles;
int cnt = mDrawTiles.cnt;
MapTile[] nextTiles;
synchronized (TileManager.tilelock) {
// lock tiles (and their proxies) to not be removed from cache
for (int i = 0; i < cnt; i++)
if (newTiles[i].isVisible)
newTiles[i].lock();
if (td == null)
td = new TileSet(newTiles.length);
nextTiles = td.tiles;
// unlock previously active tiles
for (int i = 0, n = td.cnt; i < n; i++)
nextTiles[i].unlock();
}
synchronized (GLRenderer.tilelock) {
// copy newTiles to nextTiles
td.cnt = 0;
for (int i = 0; i < cnt; i++)
if (newTiles[i].isVisible)
nextTiles[td.cnt++] = newTiles[i];
}
return td;
}
@Override @Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) { public void onSurfaceChanged(GL10 glUnused, int width, int height) {
Log.d(TAG, "SurfaceChanged:" + mNewSurface + " " + width + " " + height); Log.d(TAG, "SurfaceChanged:" + mNewSurface + " " + width + " " + height);