From cd059b79f8f95ea86cb865375a7a468980a0cd81 Mon Sep 17 00:00:00 2001 From: Ben Morrow Date: Sun, 18 Aug 2013 18:34:13 -0700 Subject: [PATCH] Much faster granims --- BenMorrow.pde | 6 +++--- GranimPattern.pde | 54 +++++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/BenMorrow.pde b/BenMorrow.pde index 911f886..8a1dd02 100644 --- a/BenMorrow.pde +++ b/BenMorrow.pde @@ -70,9 +70,9 @@ class GranimTestPattern extends GranimPattern } public void clearALL() { - for(Point p : model.points) + for(int i = 0; i < colors.length; i++) { - colors[p.index] = 0; + colors[i] = 0; } } @@ -101,7 +101,7 @@ class GranimTestPattern2 extends GranimPattern super.run(deltaMs); Graphic randomsGraphic = getGraphicByName("myRandoms"); randomsGraphic.position = Math.round(sin(count)*1000)+5000; - count+= 0.0005; + count+= 0.005; } public void clearALL() { diff --git a/GranimPattern.pde b/GranimPattern.pde index 0af19f3..606c9e2 100644 --- a/GranimPattern.pde +++ b/GranimPattern.pde @@ -1,6 +1,7 @@ import java.util.LinkedHashMap; class Graphic { + public boolean changed = false; public int position = 0; public ArrayList graphicBuffer; Graphic() @@ -16,21 +17,21 @@ class Graphic }; class Granim extends Graphic { - LinkedHashMap displayList; + HashMap displayList; Granim() { - displayList = new LinkedHashMap(); + displayList = new HashMap(); } public Graphic addGraphic(String name, Graphic g) { - //graphicBuffer.clear(); while(width()< g.position+1) { graphicBuffer.add(color(0,0,0)); } drawAll(); displayList.put(name , g); + changed =true; return g; } @@ -41,15 +42,29 @@ class Granim extends Graphic public void update() { - + for(Graphic g : displayList.values()) { if(g instanceof Granim) { ((Granim) g).update(); + + } + changed = changed || g.changed; + if(changed) + { + while(width()< g.position + g.width()) + { + graphicBuffer.add(color(0,0,0)); + } + if(g.changed) + { + drawOne(g); + g.changed =false; + } } } - drawAll(); + changed = false; } public void drawOne(Graphic g) @@ -58,25 +73,16 @@ class Granim extends Graphic } public void drawAll() { - graphicBuffer.clear(); - for(Graphic g : displayList.values()) - { - while(width()< g.position + g.width()) - { - graphicBuffer.add(color(0,0,0)); - } - drawOne(g); - } } }; class GranimPattern extends SCPattern { - LinkedHashMap displayList; + HashMap displayList; GranimPattern(GLucose glucose) { super(glucose); - displayList = new LinkedHashMap(); + displayList = new HashMap(); } public Graphic addGraphic(String name, Graphic g) @@ -94,7 +100,7 @@ class GranimPattern extends SCPattern { drawToPointList(); } - + private Integer[] gbuffer; public void drawToPointList() { for(Graphic g : displayList.values()) @@ -103,12 +109,16 @@ class GranimPattern extends SCPattern { ((Granim) g).update(); } - List drawList = model.points.subList(g.position, g.position + g.width()); - + List drawList = model.points.subList(Math.min(g.position,colors.length-1), Math.min(g.position + g.width(),colors.length-1)); + println("drawlistsize "+drawList.size()); + + gbuffer = g.graphicBuffer.toArray(new Integer[0]); + for (int i=0; i < drawList.size(); i++) { - colors[drawList.get(i).index] = (int) g.graphicBuffer.get(i); + colors[drawList.get(i).index] = gbuffer[i]; } + g.changed =false; } } @@ -192,13 +202,14 @@ class RandomsGranim extends Granim public void update() { super.update(); - if(count % 50==0) + if(instanceCount<50 && count % 20==0) { instanceCount++; Graphic h=addGraphic("myrandoms_"+instanceCount, makeGraphic(_len)); h.position = instanceCount*(_len+100); println("one more " + instanceCount+" at "+h.position); count=0; + changed = true; } count++; @@ -216,5 +227,6 @@ class ColorDotsGraphic extends Graphic { graphicBuffer.add(color(colorVal, 255, 255)); } + changed = true; } }; -- 2.34.1