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 & HelpSyntax Questions › Help with Delay/Break/Pause
Page Index Toggle Pages: 1
Help with Delay/Break/Pause (Read 1303 times)
Help with Delay/Break/Pause
Feb 26th, 2007, 4:31am
 
Hi all,
I'm trying to create a sketch where a random image is displayed, then automatically changes after a brief period of time (in the code below, I have it at 2 seconds).

I thought I could do this with the Delay function, but that does not seem to work.  I thought about the Loop/No Loop option, but everything I saw there required the user to input when to resume the sketch.

In the end, I'll have approximately 400 images to cycle through.

If it helps, here is a brief version of my current code:
###

int pic_counter;
PImage Afghanistan1;
PImage Afghanistan2;

void setup(){
 size(240,240);
 Afghanistan1 = loadImage("Afghanistan11.jpg");
 Afghanistan2 = loadImage("Afghanistan21.jpg");
}

void draw(){
 if(pic_counter == 1){
 image(Afghanistan1,0,0);
 }

 if(pic_counter == 2){
 image(Afghanistan2,0,0);
 }  

for(int i = 0; i<20; i++){
 drawPicture();
}
}

void drawPicture(){
pic_counter = int(random(1,2));
//delay(2000);
//pic_counter = int(random(2,2));
}

###
Thanks,
Will
Re: Help with Delay/Break/Pause
Reply #1 - Feb 26th, 2007, 6:39am
 
Here is an example of a slide show code i'm using

Quote:
// Modified Code from  bodytag.org/tp02/tp02.pde  (Glen Murphy)

PImage scene, im1, im2, im3;
int sloop = 0;
int WIDTH=640;
int HEIGHT=480;
int WH = WIDTH*HEIGHT;

int blend(int org, int col, int alpha) {
 int r1=(org&0x0000ff);
 int g1=(org&0x00ff00);
 int b1=(org&0xff0000);
 int r2=(col&0x0000ff);
 int g2=(col&0x00ff00);
 int b2=(col&0xff0000);
 
 int r3=(((alpha*(r1-r2)) >>8 )+r2)&0x000000ff;
 int g3=(((alpha*(g1-g2)) >>8 )+g2)&0x0000ff00;
 int b3=(((alpha*(b1-b2)) >>8 )+b2)&0x00ff0000;

 return (r3)|(g3)|(b3);
 }

void setup() {
 size(640,480,P3D);
 frameRate(15);
 loadPixels();
 scene = loadImage("myimage01.jpg");
 im1 = loadImage("myimage01.jpg");
 im2 = loadImage("myimage02.jpg");
 im3 = loadImage("myimage03.jpg");
 background(0);
}

void draw() {
for(int i = 0; i < WH-1; i++) {
   if(sloop < 255) {
     scene.pixels[i] = blend(im1.pixels[i], im2.pixels[i], 255-sloop);
     }
   else if(sloop < 510) {
     scene.pixels[i] = blend(im2.pixels[i], im3.pixels[i], 255-sloop%255);
     }
   else if(sloop < 765) {
     scene.pixels[i] = blend(im3.pixels[i], im1.pixels[i], 255-sloop%255);
     }
   else sloop = 0;
pixels[i] = scene.pixels[i];
updatePixels();  
}
sloop++;
}


There is no pause between images, but the fading effect is slow/smooth enough to enjoy the slide. Wink
Re: Help with Delay/Break/Pause
Reply #2 - Feb 26th, 2007, 11:04am
 
One way you could simulate a 2 second delay, would be to only change the image, if the value of millis has increased by 2000 since the last image change;
Code:
int lastMillis;

void setup()
{
//normal setup
lastMillis=0;
}

void draw()
{
if(lastMillis==0 || millis() > (lastMillis+2000))
{
lastMillis=millis();
//move to next image
}
}


However, it should be noted that you're going to have problems with 400 images, though it may not affect the final output with some tweaking.

400 images will take up too much memory to load them all in setup, and loading images in draw can be slow.. so, you may not need to have a 2000 millis delay, since the image loading could take about the same length of time, depending if it's being run as an applet, or a local version.
Page Index Toggle Pages: 1