From 4922e90dd092a10351d2bfd3c53af637c87aa85a Mon Sep 17 00:00:00 2001 From: codeanticode Date: Fri, 23 Jan 2015 17:08:00 -0500 Subject: [PATCH] trying to get smooth change to work --- core/src/processing/opengl/PGL.java | 8 +- .../processing/opengl/PGraphicsOpenGL.java | 3 +- .../src/processing/lwjgl/PSurfaceLWJGL.java | 158 ++++++++++++++---- 3 files changed, 132 insertions(+), 37 deletions(-) diff --git a/core/src/processing/opengl/PGL.java b/core/src/processing/opengl/PGL.java index 44400d3e1..a8451572b 100644 --- a/core/src/processing/opengl/PGL.java +++ b/core/src/processing/opengl/PGL.java @@ -66,9 +66,9 @@ public abstract class PGL { // Parameters protected static boolean USE_FBOLAYER_BY_DEFAULT = false; - protected static int REQUESTED_DEPTH_BITS = 24; - protected static int REQUESTED_STENCIL_BITS = 8; - protected static int REQUESTED_ALPHA_BITS = 8; + public static int REQUESTED_DEPTH_BITS = 24; + public static int REQUESTED_STENCIL_BITS = 8; + public static int REQUESTED_ALPHA_BITS = 8; /** Switches between the use of regular and direct buffers. */ protected static boolean USE_DIRECT_BUFFERS = true; @@ -139,7 +139,7 @@ public abstract class PGL { protected boolean fboLayerCreated = false; protected boolean fboLayerInUse = false; protected boolean firstFrame = true; - protected int reqNumSamples; + public int reqNumSamples; protected int numSamples; protected IntBuffer glColorFbo; protected IntBuffer glMultiFbo; diff --git a/core/src/processing/opengl/PGraphicsOpenGL.java b/core/src/processing/opengl/PGraphicsOpenGL.java index 55e7c6d4a..5c1985cf2 100644 --- a/core/src/processing/opengl/PGraphicsOpenGL.java +++ b/core/src/processing/opengl/PGraphicsOpenGL.java @@ -6521,7 +6521,8 @@ public class PGraphicsOpenGL extends PGraphics { pgl.getIntegerv(PGL.SAMPLES, intBuffer); int temp = intBuffer.get(0); if (quality != temp && 1 < temp && 1 < quality) { - quality = temp; + // TODO check why the samples is higher that initialized smooth level. +// quality = temp; } } if (quality < 2) { diff --git a/java/libraries/lwjgl/src/processing/lwjgl/PSurfaceLWJGL.java b/java/libraries/lwjgl/src/processing/lwjgl/PSurfaceLWJGL.java index a788f84f9..04d4390c3 100644 --- a/java/libraries/lwjgl/src/processing/lwjgl/PSurfaceLWJGL.java +++ b/java/libraries/lwjgl/src/processing/lwjgl/PSurfaceLWJGL.java @@ -19,6 +19,7 @@ import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.DisplayMode; +import org.lwjgl.opengl.PixelFormat; import processing.core.PApplet; import processing.core.PConstants; @@ -28,6 +29,7 @@ import processing.core.PSurface; import processing.event.Event; import processing.event.KeyEvent; import processing.event.MouseEvent; +import processing.opengl.PGL; import processing.opengl.PGraphicsOpenGL; public class PSurfaceLWJGL implements PSurface { @@ -61,6 +63,8 @@ public class PSurfaceLWJGL implements PSurface { protected static KeyPoller keyPoller; protected static MousePoller mousePoller; + protected static DisplayResarter restarter; + Thread thread; boolean paused; Object pauseObject = new Object(); @@ -173,6 +177,31 @@ public class PSurfaceLWJGL implements PSurface { // pgl.pixel_scale = 2; } + pgl.reqNumSamples = graphics.quality; + + System.err.println("DISPLAY PARENT: " + Display.getParent()); + System.err.println("DISPLAY SMOOTH: " + pgl.reqNumSamples); + + try { + int argb = graphics.backgroundColor; + float r = ((argb >> 16) & 0xff) / 255.0f; + float g = ((argb >> 8) & 0xff) / 255.0f; + float b = ((argb) & 0xff) / 255.0f; + Display.setInitialBackground(r, g, b); + Display.setDisplayMode(new DisplayMode(sketchWidth, sketchHeight)); + System.err.println(sketchWidth + " " + sketchHeight); + if (fullScreenRequested) { + Display.setFullscreen(true); + } + } catch (LWJGLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + restarter = new DisplayResarter(); + restarter.start(); + + // sketchWidth = sketch.width = sketch.sketchWidth(); // sketchHeight = sketch.height = sketch.sketchHeight(); @@ -323,10 +352,12 @@ public class PSurfaceLWJGL implements PSurface { } - @Override + boolean forceExit = false; + @Override public void setSmooth(int level) { - // TODO Auto-generated method stub - + System.err.println("set smooth " + level); + pgl.reqNumSamples = level; + restarter.changeRequested = true; } @@ -353,7 +384,7 @@ public class PSurfaceLWJGL implements PSurface { @Override public void blit() { - Display.update(); // maybe this ?? + // Nothing to do here } @Override @@ -421,27 +452,23 @@ public class PSurfaceLWJGL implements PSurface { // DisplayMode[] modes = Display.getAvailableDisplayModes(); // for (DisplayMode mode: modes) { // System.err.println(mode.toString()); -// } - - System.err.println("DISPLAY PARENT: " + Display.getParent()); - - Display.setDisplayMode(new DisplayMode(sketchWidth, sketchHeight)); - System.err.println(sketchWidth + " " + sketchHeight); - if (fullScreenRequested) { - Display.setFullscreen(true); - } - +// } // Display.setDisplayMode(Display.getDesktopDisplayMode()); // -// Display.create(); - - - Display.create(); +// Display.create(); +// Display.destroy(); + System.err.println("CREATE THE DISPLAY"); + PixelFormat format = new PixelFormat(PGL.REQUESTED_ALPHA_BITS, + PGL.REQUESTED_DEPTH_BITS, + PGL.REQUESTED_STENCIL_BITS, pgl.reqNumSamples); + Display.create(format); } catch (LWJGLException e) { e.printStackTrace(); System.exit(0); } + boolean deinit = true; + keyPoller = new KeyPoller(sketch); keyPoller.start(); @@ -537,22 +564,31 @@ public class PSurfaceLWJGL implements PSurface { y0 = y; if (Display.isCloseRequested()) break; - } - - if (externalMessages) { - sketch.exit(); // don't quit, need to just shut everything down (0133) + if (forceExit) { + System.err.println("QUIT"); + deinit = false; + break; + } } - keyPoller.requestStop(); - mousePoller.requestStop(); - sketch.dispose(); // call to shutdown libs? - Display.destroy(); + if (deinit) { + if (externalMessages) { + sketch.exit(); // don't quit, need to just shut everything down (0133) + } + + keyPoller.requestStop(); + mousePoller.requestStop(); + sketch.dispose(); // call to shutdown libs? + Display.destroy(); - // If the user called the exit() function, the window should close, - // rather than the sketch just halting. - if (sketch.exitCalled()) { - sketch.exitActual(); + // If the user called the exit() function, the window should close, + // rather than the sketch just halting. + if (sketch.exitCalled()) { + sketch.exitActual(); + } } + + } } @@ -669,7 +705,10 @@ public class PSurfaceLWJGL implements PSurface { try { Thread.sleep(10); } catch (InterruptedException e) { - e.printStackTrace(); + // http://stackoverflow.com/questions/1024651/do-i-have-to-worry-about-interruptedexceptions-if-i-dont-interrupt-anything-mys/1024719#1024719 +// e.printStackTrace(); + Thread.currentThread().interrupt(); // restore interrupted status + break; } } } @@ -679,6 +718,59 @@ public class PSurfaceLWJGL implements PSurface { } } + protected class DisplayResarter extends Thread { + boolean changeRequested = false; + + @Override + public void run() { + while (true) { + if (changeRequested) { + forceExit = true; + thread.interrupt(); + while (thread.isAlive()) { +// System.err.println("alive"); + Thread.yield(); + } + thread = null; + + keyPoller.requestStop(); + keyPoller.interrupt(); + while (keyPoller.isAlive()) { + Thread.yield(); + } + mousePoller.requestStop(); + mousePoller.interrupt(); + while (mousePoller.isAlive()) { + Thread.yield(); + } + + /* + System.err.println("will destroy surface"); + Display.destroy(); + try { + Display.setDisplayMode(new DisplayMode(sketchWidth, sketchHeight)); + } catch (LWJGLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +*/ + startThread(); + + + + changeRequested = false; + } + try { + Thread.sleep(10); + } catch (InterruptedException e) { +// e.printStackTrace(); + Thread.currentThread().interrupt(); // restore interrupted status + break; + } + } + } + } + protected class MousePoller extends Thread { protected PApplet parent; @@ -791,7 +883,9 @@ public class PSurfaceLWJGL implements PSurface { try { Thread.sleep(10); } catch (InterruptedException e) { - e.printStackTrace(); +// e.printStackTrace(); + Thread.currentThread().interrupt(); // restore interrupted status + break; } } }