Tint() issue when used in Class Object - Does tint() interact across objects?

I’m trying to build a program that displays different slide-shows in multiple locations on the screen.

  • I’ve created class to build individual slide-shows.
  • I’m using tint() to fade one image to the next. This works when cross-fade occurs on one object.
  • When two slide-shows are ‘fading’ at the same time there seems to be an interaction between the created objects that interferes with the crossfade. There must be an interaction, but I don’t find shared global variables.

Am missing something? I know there are quirks in the java rendering engine for tint(). I set to P2D.
Any thoughts appreciated???

[Key d - one show fade, Key f - two shows fade, Key r- reset]

// the purpose of this script is to display images on a vertical screen in two fields. 

// place images for slideshows in these sub-directories
String topImgDir = "topShow";
String bottomImgDir = "bottomShow";
// rotate screen for vertical placement
boolean rotate90 = false;
// transition time in seconds. nominal, depends on frame rate actually delivered. Assumes 25fps.
float fade = 0.5; 

boolean debug = true;

SlideShow show1, show2;
int fps = 25; // set framerate

void setup() {
  fullScreen(P2D, 2);
  background(0);
  imageMode(CENTER);
  frameRate(fps); // not sure it will actually do this rate

  show1 = new SlideShow(topImgDir, fade, 500, 500, 0, -260);
  show2 = new SlideShow(bottomImgDir, fade, 500, 500, 0, 260);
}

void draw() {
  //background(0);
  translate(width/2, height/2);
  if (rotate90)   rotate(PI/2);
  show1.draw();
  show2.draw();
  if (debug) {  
    circle(0, 0, 10);
  }
}

// ============================================================
// ============================================================
class SlideShow {
  PImage img, onDeck; 
  String path;
  int sizeX, sizeY; // sise of the slideshow window
  int showX, showY; // X-Y offset of the slidewhow window
  StringList imageList;
  File[] images;
  float fadeTime;
  int fade1 = 255; // the transparency for images used for fade transition
  boolean transitionFlag = false;

  SlideShow(String dirName, float _fadeTime, int _sizeX, int _sizeY, int _showX, int _showY) {
    sizeX = _sizeX; 
    sizeY = _sizeY; 
    showX = _showX; 
    showY = _showY;
    fadeTime = _fadeTime;
    path = sketchPath() + "/" + dirName + "/";
    imageList = listFileNames(path);
    imageList = addPath(imageList, path);
    validateImages(imageList);

    int imgNum1 = 0;  // temp
    int imgNum2 = 1;  // temp
    img = loadImageToSize(imageList.get(imgNum1));
    onDeck = loadImageToSize(imageList.get(imgNum2));
  }

  // display the image in the correct location
  void draw() {
    if (transitionFlag) {
      transition1();
    }

    pushMatrix();
    translate(showX, showY);
    image(onDeck, 0, 0);
    tint(255, fade1);
    image(img, 0, 0);
    if (debug) {
      stroke(40, 60, 100);
      noFill();
      rectMode(CENTER);
      rect(0, 0, sizeX, sizeY);
    }
    popMatrix();
  }

  void nextImage() {
  }

  void transition1() {
    fade1 -= int(256/(fadeTime*fps)); // Start at 255 and reduce 
    println( "fade1:", fade1);
    if (fade1 <= 0) { 
      transitionFlag = false;
      println("2-FrameRate Calculated;", (float(frameCount)/millis()*1000));
      println();
      return;
    }
  }

  // load the image and resize the image to slideshow size before returning it. 
  PImage loadImageToSize(String imgLoc) {
    PImage photo = loadImage(imgLoc);
    float resizeImg;
    if (photo.width >= photo.height) {
      resizeImg = float(sizeX)/float(photo.width);
    } else { 
      resizeImg = float(sizeY)/float(photo.height);
    }
    photo.resize(int(photo.width*resizeImg), 0); // 0 means do it proportionally all dimensions
    if (debug)   println( "resize factor:", resizeImg);
    return photo;
  }


  void fade() {  // sets flag/variables at start of transition 
    transitionFlag = true;
    fade1 = 255;
    println("transitionFlag:", transitionFlag);
    println("1-FrameRate Calculated;", (float(frameCount)/millis()*1000));
  }
  void reset() {
    transitionFlag = false;
    fade1 = 255;
  }

  // Function returns all the jpg files in a directory as an array of Strings  
  StringList listFileNames(String dir) {
    File file = new File(dir);
    if (file.isDirectory()) {
      String[] names = file.list();
      // convert to StringList so I can use sort function
      StringList namesList = new StringList();
      for (int i = 0; i < names.length; i++ ) { 
        if (names[i].indexOf(".jpg") != -1) namesList.append(names[i]);
      }
      namesList.sort();
      return namesList;
    } else { 
      // If it's not a directory
      return null;
    }
  }

  // !!!!!!  modify for rotation. 
  // See if the images are too small for the show. 
  void validateImages(StringList _imageList) {
    for (String imgName : _imageList) {
      PImage img = loadImage(imgName);
      if ((img.width < sizeX) && (img.height<sizeY)) {
        println("--- WARNING --- encountered an image that is too small for this display!");
        println("    Image" +imageList.get(0) + " width is " + img.width + ", height is " + img.height);
        println("    Please resize and try again.");
        exit();
      }
    }
  }

  // append the path to the file name. 
  StringList addPath(StringList imagesList, String _path) {
    StringList imgPath = new StringList();
    for (String name : imagesList) {
      imgPath.append(_path + name);
    }
    return imgPath;
  }
}

// ============================================================
// KeyStrokes and Utility scripts
// ============================================================
void keyPressed() {
  if (key == 'f' || key == 'F') { 
    show1.fade();
    show2.fade();
  } else if (key == 'd' || key == 'D') { //
    show1.fade();
  } else if (key == 'g' || key == 'G') { //
    show2.fade();
  } else if (key == 'r' || key == 'R') { //
    show1.reset();
    show2.reset();
  } else if (key == 'x' || key == 'X') {
    shutdown();
  }
}
// ============================================================
void shutdown() {
  println("");
  println(" Exit & Close"); 
  exit();
}

1 Like

tint() changes the global style state. This is true if it is called in draw, from a function, or from an object method. All other direct canvas drawing in that draw loop is affected.

To isolate tint(), use pushStyle() / popStyle() – the same way that you isolate translate() with pushMatrix() / popMatrix().

3 Likes

Jeremy,
That makes sense. I’ll give it a try.
Thanks

Lee