using swingworker to fix the hanging bug (also in opengl)

This commit is contained in:
benfry
2005-02-05 21:55:14 +00:00
parent 00f8d3821c
commit 6c830abc69
5 changed files with 246 additions and 65 deletions

View File

@@ -1939,6 +1939,7 @@ public class PApplet extends Applet
}
//////////////////////////////////////////////////////////////
// PERLIN NOISE
@@ -2319,7 +2320,7 @@ public class PApplet extends Applet
}
return img;
}
System.err.println("loadImage(): bad targa image format");
die("loadImage(): bad targa image format");
return null;
}
@@ -2344,14 +2345,10 @@ public class PApplet extends Applet
}
return new PFont(input);
//} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
System.err.println("Could not load font " + filename);
System.err.println("Make sure that the font has been copied");
System.err.println("to the data folder of your sketch.");
System.err.println();
e.printStackTrace();
die("Could not load font " + filename + "\n" +
"Make sure that the font has been copied\n" +
"to the data folder of your sketch.", e);
}
return null;
}
@@ -4270,42 +4267,212 @@ public class PApplet extends Applet
// MAIN
private static class WorkerVar {
private Thread thread;
WorkerVar(Thread t) { thread = t; }
synchronized Thread get() { return thread; }
synchronized void clear() { thread = null; }
}
class Worker {
private Object value;
private WorkerVar workerVar;
protected synchronized Object getValue() {
return value;
}
private synchronized void setValue(Object x) {
value = x;
}
/**
* Compute the value to be returned by the <code>get</code> method.
*/
//public abstract Object construct();
public Object construct() {
//while ((Thread.currentThread() == this) && !finished) {
try {
// is this what's causing all the trouble?
int anything = System.in.read();
if (anything == EXTERNAL_STOP) {
//System.out.println("********** STOPPING");
// adding this for 0073.. need to stop libraries
// when the stop button is hit.
PApplet.this.stop();
//System.out.println("********** REALLY");
finished = true;
}
} catch (IOException e) {
// not tested (needed?) but seems correct
//stop();
finished = true;
//thread = null;
}
try {
Thread.sleep(250);
//Thread.sleep(100); // kick up latency for 0075?
} catch (InterruptedException e) { }
return null;
}
/**
* Called on the event dispatching thread (not on the worker thread)
* after the <code>construct</code> method has returned.
*/
public void finished() {
}
/**
* A new method that interrupts the worker thread. Call this method
* to force the worker to stop what it's doing.
*/
public void interrupt() {
Thread t = workerVar.get();
if (t != null) {
t.interrupt();
}
workerVar.clear();
}
/**
* Return the value created by the <code>construct</code> method.
* Returns null if either the constructing thread or the current
* thread was interrupted before a value was produced.
*
* @return the value created by the <code>construct</code> method
*/
public Object get() {
while (true) {
Thread t = workerVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return null;
}
}
}
/**
* Start a thread that will call the <code>construct</code> method
* and then exit.
*/
public Worker() {
final Runnable doFinished = new Runnable() {
public void run() { finished(); }
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
workerVar.clear();
}
javax.swing.SwingUtilities.invokeLater(doFinished);
}
};
Thread t = new Thread(doConstruct);
workerVar = new WorkerVar(t);
}
/**
* Start the worker thread.
*/
public void start() {
Thread t = workerVar.get();
if (t != null) {
t.start();
}
}
}
public void setupExternal(Frame frame) {
//externalRuntime = true;
/*
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
//while ((Thread.currentThread() == this) && !finished) {
try {
// is this what's causing all the trouble?
int anything = System.in.read();
if (anything == EXTERNAL_STOP) {
//System.out.println("********** STOPPING");
// adding this for 0073.. need to stop libraries
// when the stop button is hit.
PApplet.this.stop();
//System.out.println("********** REALLY");
finished = true;
}
} catch (IOException e) {
// not tested (needed?) but seems correct
//stop();
finished = true;
//thread = null;
}
try {
Thread.sleep(250);
//Thread.sleep(100); // kick up latency for 0075?
} catch (InterruptedException e) { }
}
});
*/
/*
Thread ethread = new Thread() { //new Runnable() {
public void run() {
// this fixes the "code folder hanging bug" (mostly)
setPriority(Thread.MIN_PRIORITY);
*/
final Worker worker = new Worker();
while ((Thread.currentThread() == this) && !finished) {
try {
// is this what's causing all the trouble?
int anything = System.in.read();
if (anything == EXTERNAL_STOP) {
//System.out.println("********** STOPPING");
/*
final SwingWorker worker = new SwingWorker() {
public Object construct() {
//while ((Thread.currentThread() == this) && !finished) {
try {
// is this what's causing all the trouble?
int anything = System.in.read();
if (anything == EXTERNAL_STOP) {
//System.out.println("********** STOPPING");
// adding this for 0073.. need to stop libraries
// when the stop button is hit.
PApplet.this.stop();
// adding this for 0073.. need to stop libraries
// when the stop button is hit.
PApplet.this.stop();
//System.out.println("********** REALLY");
finished = true;
}
} catch (IOException e) {
// not tested (needed?) but seems correct
//stop();
//System.out.println("********** REALLY");
finished = true;
//thread = null;
}
try {
Thread.sleep(250);
//Thread.sleep(100); // kick up latency for 0075?
} catch (InterruptedException e) { }
} catch (IOException e) {
// not tested (needed?) but seems correct
//stop();
finished = true;
//thread = null;
}
try {
Thread.sleep(250);
//Thread.sleep(100); // kick up latency for 0075?
} catch (InterruptedException e) { }
return null;
}
};
ethread.start();
//ethread.start();
*/
frame.addComponentListener(new ComponentAdapter() {
public void componentMoved(ComponentEvent e) {

View File

@@ -24,6 +24,14 @@ X changed PMovie.length to PMovie.duration
X move around/simplify loadImage() inside PApplet
X working to add more die() statements inside PApplet
fixed in previous releases
X text stuff
X text() with alignment doesn't work for multiple lines
X don't change the size of a font when in screen space mode
X patch rotated text (from visualclusto) into bfont
X what sort of api? textSpace(ROTATED) ?
X rotated text has a bug for when it goes offscreen
opengl
X why is the thing hanging until 'stop' is hit?
X what happens when stop is hit that sets it free?
@@ -48,8 +56,6 @@ X why is the first one failing?
_ basic sample audio playback needed for p5
_ make separate java 1.1 and java 1.3 classes
_ once debugged, merge these back together and use reflection
_ (unless it's a messy disaster)
X beginFrame() around setup()
X draw mode stuff happens inside setup..
@@ -57,6 +63,8 @@ o or maybe need to get better at size() inside of draw() ?
_ make this consistent with the regular PApplet
_ otherwise things are going to be weird/difficult for debugging
_ threading horks up dual processor machine..
_ make sure background() gets called at least once with opengl
_ otherwise screen never actually updates
_ depth() shouldn't be needed for opengl unless actually 3D
@@ -71,6 +79,9 @@ _ still threading issues with running opengl
_ first run hangs until quit
_ though doesn't seem to replicate when p5 is restarted
_ should image i/o and sound i/o be moved into PImage and PSound?
_ how to load external encoders/decoders
_ fix non-bound textures from mangling everything else
_ fix enable/disable textures for some objects
_ fix endian ordering issues so that things work properly
@@ -84,6 +95,12 @@ _ needs custom animator thread..
_ be consistent about getXxx() methods
_ how to handle full screen (opengl especially) or no screen (for scripts)
_ add gzipInput and gzipOutput
_ image loading bug is huge
_ can ALPHA fonts work using the other replace modes?
_ resolve ARGB versus RGBA versus just A issues for fonts
_ make sure that current scenario works identically on mac
@@ -122,7 +139,8 @@ _ implement size(0, 0) -> just doesn't bother doing a frame.show();
_ too abstract, just have draw() call exit by default
_ so if nothing inside draw, just quits
opengl documentation
documentation
_ must call depth() for 3D applications
_ lights cannot be enabled/disabled throughout
_ lighting will be based on what's left at endFrame()
_ images should be a power of 2, or call modified()
@@ -135,10 +153,11 @@ _ specifically, if drawn rotated 90 in either direction, or 180
_ if just rotate/translate, then can use SCREEN_SPACE for fonts
_ bring screen space and font size settings back in to PGraphics
_ causing too much trouble to be stuck down in PFont
_ don't allocate zbuffer & stencil until depth() is called
_ explicitly state depth()/nodepth()
_ don't allocate zbuffer & stencil until depth() is called
_ massive graphics engine changes
_ explicitly state depth()/nodepth()
_ move to new graphics engine
_ test with rgb cube, shut off smoothing
_ make sure line artifacts are because of smoothing
@@ -166,10 +185,6 @@ _ perhaps something gets corrected?
_ arc with stroke only draws the arc shape itself
_ may need a 'wedge' function to draw a stroke around the whole thing
_ how to handle full screen (opengl especially) or no screen (for scripts)
_ image loading bug is huge
_ need timer in as part of the api
_ or at least include an example that uses it
@@ -446,6 +461,13 @@ CORE / PGraphics
1 _ http://processing.org/discourse/yabb/YaBB.cgi?board=Tools;action=display;num=1082055374;start=0
CORE / Sound
1 _ merge PSound and PSound2 via reflection?
1 _ once debugged, merge these back together and use reflection
1 _ (unless it's a messy disaster)
CORE / Details
1 _ framerate(30) is still flickery and jumpy..
@@ -509,13 +531,6 @@ CORE / Mac OS X
CORE / PFont
1 _ text() with alignment doesn't work for multiple lines
1 _ don't change the size of a font when in screen space mode
1 _ patch rotated text (from visualclusto) into bfont
1 _ what sort of api? textSpace(ROTATED) ?
1 _ rotated text has a bug for when it goes offscreen
CORE / New Graphics
What the hell do we do with this code?