Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Wednesday, 17 July 2013

Using the thread convenience method in processing-2.0

Chinchbug published this sketch at openprocessing here is much simpler version that makes use of the processing-2.0 convenience function thread (nasty thing is it works by reflection internally ho-hum!). It takes a string as argument for a "public" function that gets called in a new thread.
   1 float ang;
   2 int curr;
   3 PImage frames[];
   4 boolean bLoadDone;
   5 
   6 void setup() {
   7   size(305, 395);
   8   background(0);
   9   smooth();
  10   noStroke();
  11   textAlign(CENTER, CENTER);
  12   frames = new PImage[116];
  13   bLoadDone = false;
  14   thread("loadFrames"); // processing convenience method for starting new Thread with a function 
  15   ang = 0.0;
  16 }
  17 
  18 
  19 void draw() {
  20   if (bLoadDone) {
  21     background(0);
  22     image(frames[curr++], 0, 0);
  23     if (curr > 115) {
  24       curr = 0;
  25     }
  26   } 
  27   else {
  28     float x, y;
  29     ang += 0.1;
  30     x = cos(ang) * 8;
  31     y = sin(ang) * 8;
  32     fill(0, 8);
  33     rect(50, 150, 100, 100);
  34     fill(32, 32, 255);
  35     ellipse(x + 100, y + 200, 8, 8);
  36     fill(0);
  37     rect(120, 150, 170, 100);
  38     fill(128);
  39     text(String.format("loading frames (%s of 115)", curr), 200, 200);
  40   }
  41 }
  42 
  43 void loadFrames() {
  44   for (curr = 0; curr < 116; curr++) {
  45     frames[curr] = loadImage(dataPath(String.format("a%s copy.jpg", nf(curr, 3))));
  46     delay(75); //just slows down this thread - the main draw() cycle is unaffected...
  47   }
  48   curr = 0;
  49   frameRate(30);
  50   bLoadDone = true;
  51 }
  52 

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2