PGraphics and class or drawing in layers. Also how to make this faster?
in
Programming Questions
•
11 months ago
Hi,
I was playing with this code. Basically a ellipse that search and changes grey pixels to red pixels. I got some questions in the code itself, but the main is: why moving line 127 to inside draw() i only see one object? As commented in the code i think i'm calling image to many times which i assume is making things slower than needed. Press 'z' to dismiss zoom frame. (it's hard to see the pixels changes without zoom).
Another strange thing. If i increase the size() to 1000x1000 the sketch goes much slower. Eveb with the same amount of followers created...
What am i missing?
thanks.
- Folower[] f = new Folower[10];
- // drawing folower in layer1
- PGraphics layer1;
- //layer0 is bg
- PGraphics layer0;
- PFont font;
- boolean show = true;// show or not the folowers press s to tooglle
- boolean zoom = true;// show or not zoom under mouse press z
- void setup()
- {
- size(500, 500, JAVA2D);
- smooth();
- layer1 = createGraphics(width, height, JAVA2D);
- layer0 = createGraphics(width, height, JAVA2D);
- // do i need beginDraw here? Or Why i don't need it :)
- layer0.loadPixels();
- for (int i=0; i < layer0.pixels.length; i++)
- {
- int luck = (int) random(0, 5);
- layer0.pixels[i] = (luck == 0)? color(200):color(255);
- }
- layer0.updatePixels();
- //random initial position and target
- for (int i=0; i < f.length; i++)
- {
- PVector posi = new PVector (random(width), random(height));
- PVector tar = new PVector (random(width), random(height));
- f[i] = new Folower(posi, tar);
- }
- font = createFont("arial", 10);
- }
- void draw()
- {
- image(layer0, 0, 0);
- if(show)
- {
- for (int i=0; i < f.length; i++)
- {
- f[i].display();
- }
- }
- else
- {
- for (int i=0; i < f.length; i++)
- {
- f[i].update();
- }
- }
- // if i call this here i can only see one follower
- //they all exixts and acts in the bg though..
- //image(layer1, 0, 0, width, height);
- if(zoom)
- copy(mouseX-25, mouseY-25, 50,50, mouseX-75, mouseY-75, 150,150);
- }
- void keyTyped()
- {
- if(key == 'z' || key == 'Z')
- zoom = !zoom;
- if(key == 's' || key == 's')
- show= !show;
- }
- ////// Folower class
- class Folower {
- PVector pos;
- PVector target;
- float curvex, curvey;
- int bellySize = 10;
- Folower(PVector _pos, PVector _target)
- {
- pos = _pos;
- target = _target;
- curvex = random (0.5, 3);
- curvey = random (0.5, 3);
- }
- void setTarget(PVector t)
- {
- target = t;
- curvex = random (1, 3);
- curvey = random (1, 3);
- }
- void display()
- {
- update();
- layer1.beginDraw();
- layer1.background(0, 0);
- layer1.noFill();
- layer1.ellipse(target.x, target.y, 3, 3);
- layer1.fill(255, 0, 0, 30);
- layer1.ellipse( pos.x, pos.y, bellySize, bellySize);
- layer1.point( pos.x, pos.y);
- layer1.textFont(font, 10);
- layer1.fill(0);
- layer1.text(nf(bellySize-10, 2), pos.x, pos.y + 10 + bellySize/2);
- layer1.endDraw();
- //* if i move this to draw, wont work,*/
- //* but i think it is dumb here calling same thing to much times...*/
- image(layer1, 0, 0, width, height);
- }
- void update()
- {
- if (!found())
- {
- pos.x = pos.x + (((target.x - pos.x)*curvex)*0.2) ;
- pos.y = pos.y + (((target.y - pos.y)*curvey)*0.2) ;
- }
- else
- {
- if (isFood())
- {
- bellySize++;
- layer0.beginDraw();
- layer0.loadPixels();
- layer0.pixels[int(pos.y*width+pos.x)] = color(255,0,0);
- layer0.updatePixels();
- layer0.endDraw();
- }
- PVector newTarget = new PVector(random(0, width-1), random(0, height-1));
- setTarget(newTarget);
- }
- }
- boolean isFood()
- {
- color c = layer0.pixels[int(pos.y* width + pos.x)]; //layer0.get((int)pos.x, (int)pos.y);
- return (c >> 16 & 0xFF) == 200;
- }
- boolean found()
- {
- return pos.dist(target) < 0.1;
- }
- }//eoc
1