Note: my guard line was intended to be at the
start of display()...
But it was a good idea to give the current state of the code, I saw I made a stupid loadPhotos() function...
Since you got code that doesn't rely actually on webcam, I could test it just by adding images to the watched dir. I made a number of small changes:
Code:import processing.opengl.*;
ArrayList photos = new ArrayList();
String[] fileNameList;
PImage[] loadedPhotos;
int counter, fileCount, imageCount;
boolean autoPlay = true; // by default autoplay is OFF
HashSet loadedFileNames = new HashSet();
String path = "/Users/menno/Documents/Processing/picture_pool/";
void setup() {
size(1024,768,OPENGL);
//size(screen.width,screen.height,OPENGL); // full screen mode
smooth();
loadedPhotos = new PImage[100]; // maximum of 10.000 images
loadFileNames();
loadPhotos(); // all existing images are loaded in setup
fileCount = fileNameList.length;
}
void draw() {
background(51);
checkNew(); // checks for new images in the data directory every draw cycle
if(autoPlay) autoPlay();
Photo lastPhoto = null;
for (int i = 0; i < photos.size(); i++) {
Photo s = (Photo) photos.get(i);
s.display();
s.move();
lastPhoto = s;
}
if (lastPhoto != null && lastPhoto.bStopped && photos.size() == fileCount) {
// reset slideshow when it reaches the last slide which stopped to move
photos.clear();
loadPhotos();
}
}
void loadFileNames() {
File folder = new File(path);
FilenameFilter imgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.startsWith("_") &&
(name.toLowerCase().endsWith(".jpg") ||
name.toLowerCase().endsWith(".png"));
}
};
fileNameList = folder.list(imgFilter);
}
void loadPhotos() {
for (int i = 0; i < fileNameList.length; i++) {
if (!loadedFileNames.contains(fileNameList[i])) {
// A new file!
loadedPhotos[imageCount++] = loadImage(path + fileNameList[i]);
loadedFileNames.add(fileNameList[i]);
}
}
}
void checkNew() {
loadFileNames();
if (fileNameList.length > fileCount) {
loadPhotos(); // only call loadPhotos if there are new images
fileCount = fileNameList.length;
}
}
void autoPlay() {
counter++;
if (counter >= 60 && photos.size() < imageCount)
{
photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30)));
counter=0;
}
}
void keyPressed() {
if (key == 'z') {if(autoPlay) autoPlay = false; else autoPlay = true;}
if (key == ' ') {if (photos.size() < imageCount) {photos.add(new Photo(random(80,width-80),random(60,height-60),random(-30,30)));}}
if (key == 'x') {if (photos.size() > 0) {photos.remove(photos.size()-1);}}
if (key == 'c') {photos.clear();}
}
class Photo {
PImage photo;
float targetXpos;
float targetYpos;
float rotation;
float xpos;
float ypos;
float easing;
boolean bStopped;
Photo(float targetXposTemp, float targetYposTemp, float rotationTemp) {
photo = loadedPhotos[photos.size()];
xpos = width/2;
ypos = -130;
targetXpos = targetXposTemp;
targetYpos = targetYposTemp;
rotation = rotationTemp;
easing = 0.1;
}
void display() {
rectMode(CENTER);
imageMode(CENTER);
noStroke();
pushMatrix();
translate(width/2,height/2);
rotate(radians(rotation));
translate(-width/2,-height/2);
translate(xpos,ypos);
fill(0,0,0,50);
rect(4,4,330,250);
fill(255,255,255);
rect(0,0,330,250);
image(photo,0,0,320,240);
popMatrix();
}
void move() {
if (bStopped) return;
if (dist(xpos,ypos,targetXpos,targetYpos) > 1) { // only move if the destination has not been reached
xpos = (1-easing) * xpos + (easing) * targetXpos;
ypos = (1-easing) * ypos + (easing) * targetYpos;
} else {
bStopped = true;
}
}
}
Seems to work OK for my limited testing.