Here are the two pieces of code I promised earlier today. The first one works ok whereas the second one doesn't:
Code:
// Example A: This program seems to work ok. A frame is saved
// at every press of key 'S' and program continues to draw without
// problems.
color[] cols = new color[3];
int a = 0, b = 0;
void setup() {
size(100, 100);
cols[0] = color(255, 0, 0);
cols[1] = color(0, 255, 0);
cols[2] = color(0, 0, 255);
}
void draw() {
a = (a + 1) % 2;
b = (b + 1) % 3;
for ( int x = 0; x < 100; x++ ) {
for ( int y = (x + a) % 2; y < 100; y += 2) {
set(x, y, cols[b]);
}
}
}
void keyPressed() {
char k = (char) key;
if ( k == 's' || k == 'S' ) {
saveFrame();
}
}
Code:
// Example C: The following program exhibits strange
// saveFrame behaviour. Prior to saving the framerate
// is very high (tens or hundreds of frames per second).
// After a frame is saved by pressing 'S' the framerate
// drops down to few frames per second.
// My original program draws over ten thousand lines per
// frame. So if saveFrame() mysteriously slows down line()
// then it is pretty obvious why the program seems to freeze
// completely.
color[] cols = new color[3];
int a = 0, b = 0;
void setup() {
size(100, 100);
cols[0] = color(255, 0, 0);
cols[1] = color(0, 255, 0);
cols[2] = color(0, 0, 255);
}
void draw() {
a = (a + 1) % 2;
b = (b + 1) % 3;
stroke(cols[b]);
for ( int y = a; y < 100; y += 2) {
line(0, y, 99, y);
}
}
void keyPressed() {
char k = (char) key;
if ( k == 's' || k == 'S' ) {
saveFrame();
}
}