We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › loop/noLoop/redraw difficulties
Page Index Toggle Pages: 1
loop/noLoop/redraw difficulties (Read 534 times)
loop/noLoop/redraw difficulties
Feb 27th, 2009, 7:14am
 
I'm having difficulty with the interplay between loop(), noLoop(), and redraw().

The program I'm writing mixes in the video library looking at the screen to do things like blob detection.  Part of this is a calibration step where I alternate filling the drawing with stripes, grab a camera frame and do some image processing to create a camera to screen transform.

I couldn't get the loop/noLoop/redraw interaction right, so I built the attached test.  It starts by looping through the gray colors (if the noLoop in setup is commented out).  Pressing any key sets the fillColor to Red, then Green, then Blue; calls redraw(), then pauses a second after each.

What happens on a key press is simply a pause, without the R/G/B backgrounds taking effect.  If I uncomment the noLoop in setup, it works the first time, but failes from then on.

So how is all this supposed to work?  Or am I going at this wrong, and there is an immediate drawing mode that I don't know about?  

As I understand it, the right Processing "pattern" is to do all your drawing only with draw(), using state variables to indicate what to draw.  This is due to the setup required each time through the draw(), thus the need for a redraw() which wraps the draw() with the proper code.

Thanks!

Owen

color fillColor = 0;
void setup() {
 size(600, 600, P2D);
 //noLoop(); // If uncommented, works first time, but then fails
}
void draw() {
 background(fillColor);
 fillColor = (fillColor + 1) % 256 ;
}
void keyPressed(){
 print("key pressed");
 color savedColor = fillColor;
 noLoop();
 fillTest(color(255,0,0));
 fillTest(color(0,255,0));
 fillTest(color(0,0,255));
 fillColor = savedColor;
 loop();
 println(" -- key done");
}
void fillTest(color c) {
 print(" . ");
 fillColor = c;
 redraw();
 delay(1000);
}

Re: loop/noLoop/redraw difficulties
Reply #1 - Feb 27th, 2009, 8:44am
 
I gotta go, so I read hastily and do a quick, perhaps bad answer. But I fear to have read that key and mouse events are handled only if draw loop is active.
If that's your problem and the case, a simple solution is to have a global boolean dontDraw.
Set it to true instead of calling noDraw(), reset it to false to do a redraw, and at start of draw() do:

if (dontDraw)
return;

with proper handling to reset it or what's not.
Re: loop/noLoop/redraw difficulties
Reply #2 - Feb 27th, 2009, 6:15pm
 
PhiLho: thanks, that helps, quite a bit.

It makes me realize that my real problem is the overall state machine used by Processing.

For example, within a draw(), I think all graphics is deferred until the draw() exits.  This optimizes the graphics setup overhead that would occur with immediate drawing.  Probably has a double buffering optimization as well.

Outside of draw(), it seems like there's an immediate mode graphics.

Yet a draw() is needed in order to process events.  Not sure why, I suspect its related to the thread architecture of Processing.

So what I *REALLY* need, it seems, is a pointer to a detailed description of the Processing state machine: how drawing, events, threads all work together and how the loop/noLoop/redraw -- and event management, and immediate graphics all integrate.

Whew!

Owen
Re: loop/noLoop/redraw difficulties
Reply #3 - Feb 28th, 2009, 9:36am
 
Making another thread (loop/noLoop/redraw -- part 2) doesn't help in keeping information consistent...

I was misleading in above message: noLoop() prevents draw() to run (unless called explicitly with redraw()), but fortunately doesn't prevent events to be called. In all cases, you need draw() (even an empty one) to have these events.
Page Index Toggle Pages: 1