Hey,
looked at your code and i see two things, you should change:
1. You check the delays of the first gif-animation, which you pick in setup(), but whenever you load a new gif, you have to extract its own delay-times.
2. You check for the number of frames passed when deciding if you pick a new random image/animation. It's more reliable, if you check for the time passed instead.
Here is another example on displaying random gif-animations, you would have to change it, when you want jpgs too.
- import gifAnimation.*;
PImage[] animation;
float Gifanime;
int GifFragNumber;
int xGif = 2;
int[] delays;
int currentFrame, frameStart, displayStart, displayTime;
void setup() {
size(500, 500);
imageMode(CENTER);
}
void draw() {
background (255);
// this part chooses a new gif-file and calculates how long it shall be shown
if (displayTime <= millis() - displayStart) {
// pick an new gif-file
float Gifanime = random (0, xGif);
int GifFragNumberPlus= ceil(Gifanime);
// number of repetitions
float r = random (2, 5);
int gifrepeats = int(r);
// load gif-animation and delay-times
animation = Gif.getPImages (this, "Gif_" + GifFragNumberPlus + ".gif");
delays = getDelays("Gif_" + GifFragNumberPlus + ".gif");
println("Gif_" + GifFragNumberPlus + ".gif, repeat: "+gifrepeats);
// calculate length of the whole animation
int gifTime = 0;
for (int i = 0; i< animation.length; i++) {
gifTime += delays[i];
}
displayTime = gifrepeats*gifTime;
displayStart = millis();
currentFrame = 0;
}
// show the gif-animation
if (millis()-frameStart > delays[currentFrame]) {
currentFrame = (++currentFrame)%animation.length;
frameStart = millis();
}
image(animation[currentFrame], width/2, height/2);
}
// get delay-times of a gif-animation
int[] getDelays(String gifname) {
GifDecoder gifDecoder = new GifDecoder();
gifDecoder.read(openStream(gifname));
int n = gifDecoder.getFrameCount();
int[] delays = new int[n];
for (int i = 0; i < n; i++) {
delays[i] = gifDecoder.getDelay(i);
}
return delays;
}