From a10547e090df1c5f87d1823674fa0755c3e71d27 Mon Sep 17 00:00:00 2001 From: benfry Date: Tue, 21 Sep 2004 22:33:39 +0000 Subject: [PATCH] working on threads and problems with external libs --- processing/app/PdeRuntime.java | 17 +++- processing/core/PApplet.java | 181 ++++++++++++++++----------------- processing/core/PGraphics.java | 4 +- processing/todo.txt | 2 + 4 files changed, 107 insertions(+), 97 deletions(-) diff --git a/processing/app/PdeRuntime.java b/processing/app/PdeRuntime.java index 4f65a9d0d..6a71b1924 100644 --- a/processing/app/PdeRuntime.java +++ b/processing/app/PdeRuntime.java @@ -90,7 +90,9 @@ public class PdeRuntime implements PdeMessageConsumer { windowLocation.x + "," + windowLocation.y) : (PApplet.EXT_LOCATION + x1 + "," + y1); - String command[] = new String[] { + String command[] = new String[] { + //"cmd", "/c", "start", + "java", "-Djava.library.path=" + // sketch.libraryPath might be "" @@ -103,6 +105,9 @@ public class PdeRuntime implements PdeMessageConsumer { location, PApplet.EXT_SKETCH_FOLDER + sketch.folder.getAbsolutePath(), sketch.mainClassName + + //, "1>", "C:\\net1.txt" + //, "2>", "C:\\net2.txt" }; //for (int i = 0; i < command.length; i++) { @@ -114,6 +119,7 @@ public class PdeRuntime implements PdeMessageConsumer { processInput = new SystemOutSiphon(process.getInputStream()); processError = new PdeMessageSiphon(process.getErrorStream(), this); processOutput = process.getOutputStream(); + //process.waitFor(); } else { // !externalRuntime //Class c = Class.forName(className); @@ -292,6 +298,7 @@ public class PdeRuntime implements PdeMessageConsumer { } catch (Exception e) { // this will pass through to the first part of message // this handles errors that happen inside setup() + e.printStackTrace(); // mod by fry for removal of KjcEngine applet.finished = true; @@ -551,22 +558,26 @@ java.lang.NullPointerException // read, block until something good comes through while (Thread.currentThread() == thread) { try { + //System.out.println("readin"); int count = input.read(boofer, 0, boofer.length); + //System.out.println("readout " + count); if (count == -1) { thread = null; } else { System.out.print(new String(boofer, 0, count)); + //System.out.flush(); } } catch (IOException e) { // this is prolly because the app was quit & the stream broken //e.printStackTrace(System.out); + //e.printStackTrace(); thread = null; } //System.out.println("SystemOutSiphon: out"); - thread = null; - } + //thread = null; + } } } } diff --git a/processing/core/PApplet.java b/processing/core/PApplet.java index 5ed5bbb8c..ff5de8f20 100644 --- a/processing/core/PApplet.java +++ b/processing/core/PApplet.java @@ -41,6 +41,7 @@ public class PApplet extends Applet implements PConstants, Runnable, MouseListener, MouseMotionListener, KeyListener, FocusListener { + // jdkVersionStr = "1.3" or "1.1" or whatever static final String jdkVersionStr = System.getProperty("java.version").substring(0,3); static final double jdkVersion = @@ -55,12 +56,17 @@ public class PApplet extends Applet /** Path to sketch folder */ public String folder; // = System.getProperty("user.dir"); + /** When debugging headaches */ static final boolean THREAD_DEBUG = false; //true; public int pixels[]; public int mouseX, mouseY; public int pmouseX, pmouseY; + + // used to set pmouseX/Y to mouseX/Y the first time + // mouseX/Y are used, otherwise pmouseX/Y would always + // be zero making a big jump. yech. boolean firstMouseEvent; public boolean mousePressed; @@ -122,13 +128,10 @@ public class PApplet extends Applet //boolean qmouseDragged; //boolean qmouseMoved; - // used to set pmouseX/Y to mouseX/Y the first time - // mouseX/Y are used, otherwise pmouseX/Y would always - // be zero making a big jump. yech. - boolean firstFrame; + //boolean firstFrame; // current frame number (could this be used to replace firstFrame?) - public int frame; + public int frameCount; // true if the feller has spun down public boolean finished; @@ -193,7 +196,7 @@ public class PApplet extends Applet finished = false; // just for clarity //drawn = false; - firstFrame = true; + //firstFrame = true; // this will be cleared by loop() if it is not overridden //drawMethod = true; @@ -369,8 +372,9 @@ public class PApplet extends Applet } - boolean updated = false; + //boolean updated = false; + /* public void update() { if (firstFrame) firstFrame = false; @@ -380,18 +384,23 @@ public class PApplet extends Applet getToolkit().sync(); // force repaint now (proper method) if (THREAD_DEBUG) println(" 3c update() internal " + firstFrame); } + */ public void update(Graphics screen) { - if (THREAD_DEBUG) println(" 4 update() external"); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 4 update() external"); paint(screen); } - synchronized public void paint(Graphics screen) { - if (THREAD_DEBUG) println(" 5a enter paint"); + //synchronized public void paint(Graphics screen) { + public void paint(Graphics screen) { + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 5a enter paint"); // ignore the very first call to paint, since it's coming // from the o.s., and the applet will soon update itself anyway. - if (firstFrame) return; + //if (firstFrame) return; + if (frameCount == 0) return; // without ignoring the first call, the first several frames // are confused because paint() gets called in the midst of @@ -402,9 +411,12 @@ public class PApplet extends Applet // try to fight over it. this was causing a randomized slowdown // that would cut the framerate into a third on macosx, // and is probably related to the windows sluggishness bug too - if (THREAD_DEBUG) println(" 5b enter paint sync"); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 5b enter paint sync"); synchronized (g) { + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 5c inside paint sync"); //System.out.println("5b paint has sync"); //Exception e = new Exception(); //e.printStackTrace(); @@ -416,21 +428,67 @@ public class PApplet extends Applet if (g != null) { screen.drawImage(g.image, 0, 0, null); } + //if (THREAD_DEBUG) println("notifying all"); + //notifyAll(); + //thread.notify(); //System.out.println(" 6 exit paint"); } - updated = true; + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 6 exit paint"); + //updated = true; } public void run() { try { while ((Thread.currentThread() == thread) && !finished) { - updated = false; + //updated = false; - if (PApplet.THREAD_DEBUG) println("nextFrame()"); - //println(looping + " " + redraw); - if (looping || redraw) nextFrame(); - redraw = false; + if (PApplet.THREAD_DEBUG) println(Thread.currentThread().getName() + + " formerly nextFrame()"); + //if (looping || redraw) nextFrame(); + if (looping || redraw) { + if (fpsTarget != 0) framerate_delay(); + + synchronized (g) { + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 1a beginFrame"); + g.beginFrame(); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 1b draw"); + draw(); + + // these are called *after* loop so that valid + // drawing commands can be run inside them. it can't + // be before, since a call to background() would wipe + // out anything that had been drawn so far. + dequeueMouseEvents(); + dequeueKeyEvents(); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 2b endFrame"); + g.endFrame(); + //} // end sync + + //update(); + // formerly 'update' + //if (firstFrame) firstFrame = false; + // internal frame counter + frameCount++; + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 3a calling repaint() " + frameCount); + repaint(); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 3b calling Toolkit.sync " + frameCount); + getToolkit().sync(); // force repaint now (proper method) + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " 3c done " + frameCount); + //if (THREAD_DEBUG) println(" 3d waiting"); + //wait(); + //if (THREAD_DEBUG) println(" 3d out of wait"); + //frameCount++; + } + } + redraw = false; // unset 'redraw' flag in case it was set // moving this to update() (for 0069+) for linux sync problems //if (firstFrame) firstFrame = false; @@ -439,21 +497,24 @@ public class PApplet extends Applet // this is necessary since the drawing is sometimes in a // separate thread, meaning that the next frame will start // before the update/paint is completed - while (!updated) { + //while (!updated) { try { - if (PApplet.THREAD_DEBUG) System.out.println(looping + " " + redraw); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " " + looping + " " + redraw); //Thread.yield(); // windows doesn't like 'yield', so have to sleep at least // for some small amount of time. - if (PApplet.THREAD_DEBUG) System.out.println("gonna sleep"); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " gonna sleep"); // can't remember when/why i changed that to '1'.. // i have a feeling that some applets aren't gonna like that Thread.sleep(looping ? 1 : 10000); // sleep to make OS happy - if (PApplet.THREAD_DEBUG) System.out.println("outta sleep"); + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " outta sleep"); } catch (InterruptedException e) { break; } - } + //} } } catch (Exception e) { // formerly in kjcapplet, now just checks to see @@ -475,72 +536,8 @@ public class PApplet extends Applet e.printStackTrace(); } } - } - - - public void nextFrame() { - //mouseX = qmouseX; // only if updated? - //mouseY = qmouseY; - - if (fpsTarget != 0) framerate_delay(); - - /* - // attempt to draw a static image using draw() - if (!drawn) { - //synchronized (g.image) { - synchronized (g) { - // always do this once. empty if not overridden - g.beginFrame(); - draw(); - - if (drawMethod) { - g.endFrame(); - update(); - finished = true; - } - drawn = true; - } // end synch - } - */ - - // if not a static app, run the loop - //if (!drawMethod) { - synchronized (g) { - g.beginFrame(); - //mouseUpdated = false; - //insideLoop = true; - draw(); - //insideLoop = false; - - // these are called *after* loop so that valid - // drawing commands can be run inside them. it can't - // be before, since a call to background() would wipe - // out anything that had been drawn so far. - dequeueMouseEvents(); - dequeueKeyEvents(); - - /* - if (qmouseMoved) { - mouseMoved(); - qmouseMoved = false; - } - if (qmouseDragged) { - mouseDragged(); - qmouseDragged = false; - } - */ - g.endFrame(); - } // end synch - update(); - //} - - // takedown - //if (!loopMethod) { - //finished = true; - //} - - // internal frame counter - frame++; + if (THREAD_DEBUG) println(Thread.currentThread().getName() + + " thread finished"); } @@ -903,7 +900,7 @@ public class PApplet extends Applet * I'm not sure if this is even helpful anymore. */ public void delay(int napTime) { - if (firstFrame) return; + if (frameCount == 0) return; if (napTime > 0) { try { Thread.sleep(napTime); @@ -1081,7 +1078,7 @@ public class PApplet extends Applet } //File file = new File(folder, "screen-" + nf(frame, 4) + ".tif"); - save(save_location("screen-" + nf(frame, 4) + ".tif", true)); + save(save_location("screen-" + nf(frameCount, 4) + ".tif", true)); //save("screen-" + nf(frame, 4) + ".tif"); } @@ -1117,7 +1114,7 @@ public class PApplet extends Applet // in case the user tries to make subdirs with the filename //new File(file.getParent()).mkdirs(); //save(file.getAbsolutePath()); - save(save_location(prefix + nf(frame, count) + suffix, true)); + save(save_location(prefix + nf(frameCount, count) + suffix, true)); } } diff --git a/processing/core/PGraphics.java b/processing/core/PGraphics.java index 45d591231..dbfa8d864 100644 --- a/processing/core/PGraphics.java +++ b/processing/core/PGraphics.java @@ -514,7 +514,7 @@ public class PGraphics extends PImage * initializes engine before drawing a new frame */ public void beginFrame() { - if (PApplet.THREAD_DEBUG) System.out.println(" 1 beginFrame"); + //if (PApplet.THREAD_DEBUG) System.out.println(" 1 beginFrame"); /* if (camera_mode == -1) { //System.out.println("setting up camera"); @@ -566,7 +566,7 @@ public class PGraphics extends PImage * indicates a completed frame */ public void endFrame() { - if (PApplet.THREAD_DEBUG) System.out.println(" 2 endFrame"); + //if (PApplet.THREAD_DEBUG) System.out.println(" 2 endFrame"); if (hints[NEW_GRAPHICS]) { diff --git a/processing/todo.txt b/processing/todo.txt index 85554efdb..fc2d109cc 100644 --- a/processing/todo.txt +++ b/processing/todo.txt @@ -75,6 +75,8 @@ o when running externally, build into sketch folder? _ add all imported libs to hash table of jars +_ should library be called extension? + _ libraries _ attachLibrary(new libsomething()) and attachLibrary("pitaru.Sonia"); o final stop() for static shutdown of lib