Skip to content

Commit

Permalink
background rendering of buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
camillol committed Oct 30, 2011
1 parent a2a07e3 commit d727a20
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
45 changes: 39 additions & 6 deletions MapView.pde
Expand Up @@ -3,6 +3,7 @@ import com.modestmaps.*;
import com.modestmaps.core.*;
import com.modestmaps.geo.*;
import com.modestmaps.providers.*;
import java.util.concurrent.*;

class MapView extends View {
InteractiveMap mmap;
Expand All @@ -21,11 +22,13 @@ class MapView extends View {
int MAX_BUFFERS_TO_KEEP = 64;


Map<Coordinate, PGraphics> buffers;
Map<Coordinate, Future<PGraphics>> buffers;
ExecutorService bufferExec;
boolean USE_BUFFERS = true;

double TILE_EXPAND_FACTOR = 0.05; // as a fraction of the tile size


MapView(float x_, float y_, float w_, float h_)
{
super(x_, y_, w_, h_);
Expand All @@ -38,15 +41,23 @@ class MapView extends View {
mmap.MAX_IMAGES_TO_KEEP = 64;
mmap.setCenterZoom(new Location(39,-98), int(zoomValue));

buffers = new LinkedHashMap<Coordinate, PGraphics>(MAX_BUFFERS_TO_KEEP, 0.75, true) {
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > MAX_BUFFERS_TO_KEEP;
buffers = new LinkedHashMap<Coordinate, Future<PGraphics>>(MAX_BUFFERS_TO_KEEP, 0.75, true) {
protected boolean removeEldestEntry(Entry<Coordinate, Future<PGraphics>> eldest) {
if (size() > MAX_BUFFERS_TO_KEEP) {
eldest.getValue().cancel(true);
this.remove(eldest.getKey());
}
return false;
}
};
bufferExec = Executors.newSingleThreadExecutor();
}

void rebuildOverlay()
{
for (Future<PGraphics> future : buffers.values()) {
future.cancel(true);
}
buffers.clear();
}

Expand Down Expand Up @@ -97,9 +108,18 @@ class MapView extends View {
coord.zoom = round(coord.zoom);

if (!buffers.containsKey(coord))
buffers.put(coord, makeOverlayBuffer(coord));
buffers.put(coord, bufferExec.submit(new BufferMaker(coord)));

image(buffers.get(coord), coord.column*mmap.TILE_WIDTH, coord.row*mmap.TILE_HEIGHT, mmap.TILE_WIDTH, mmap.TILE_HEIGHT);
if (buffers.get(coord).isDone()) {
try {
PGraphics img = buffers.get(coord).get();
image(img, coord.column*mmap.TILE_WIDTH, coord.row*mmap.TILE_HEIGHT, mmap.TILE_WIDTH, mmap.TILE_HEIGHT);
} catch (InterruptedException e) {
println(e);
} catch (ExecutionException e) {
println(e);
}
}
count++;
}
}
Expand Down Expand Up @@ -144,6 +164,19 @@ class MapView extends View {
return buf;
}

class BufferMaker implements Callable<PGraphics> {
Coordinate coord;
BufferMaker(Coordinate coord)
{
this.coord = coord;
}

PGraphics call()
{
return makeOverlayBuffer(coord);
}
}

void drawContent()
{
imageMode(CORNER); // modestmaps needs this - I sent a patch, but who knows when it'll be committed
Expand Down
2 changes: 2 additions & 0 deletions Sighting.pde
Expand Up @@ -207,6 +207,8 @@ class Bucket {
- time of day
- month
- season??
- unemployment (by county and year)?
- income??
*/

/* SQLite DB access */
Expand Down
2 changes: 1 addition & 1 deletion cs424p3.pde
Expand Up @@ -195,7 +195,7 @@ void draw()
sightingDetailsView.y = detailsAnimator.value;

rootView.draw();
println(seconds);
// println(seconds);
if (settingsView.play.value){
if (!startedPlaying){
startedPlaying = true;
Expand Down

0 comments on commit d727a20

Please sign in to comment.