Taking screenshot with new thread

Current LWJGLRenderer.grabScreenContents() method takes long time to execute.

When the method is called, the rendering and update is blocked for a while.



So I just moved image processing & saving code to a new thread.

But the result screenshot is broken.

What is wrong and how can I fix it?

Thanks in advance.



Index: src/com/jme/renderer/lwjgl/LWJGLRenderer.java
===================================================================
--- src/com/jme/renderer/lwjgl/LWJGLRenderer.java   (revision 4418)
+++ src/com/jme/renderer/lwjgl/LWJGLRenderer.java   (working copy)
@@ -624,40 +624,51 @@
      *            the name of the file to save.
      * @return true if successful, false otherwise.
      */
-    public boolean takeScreenShot(String filename) {
+    public boolean takeScreenShot(final String filename) {
         if (null == filename) {
             throw new JmeException("Screenshot filename cannot be null");
         }
-        File out = new File(filename + ".png");
-        logger.log(Level.INFO, "Taking screenshot: {0}", out.getAbsolutePath());
-
         // Create a pointer to the image info and create a buffered image to
         // hold it.
-        ByteBuffer buff = BufferUtils.createByteBuffer(width * height * 3);
+        final ByteBuffer buff = BufferUtils.createByteBuffer(width * height * 3);
         grabScreenContents(buff, Image.Format.RGB8, 0, 0, width, height);
-        BufferedImage img = new BufferedImage(width, height,
-                BufferedImage.TYPE_INT_RGB);
+       
+        Thread saveThread = new Thread() {
+            public void run() {
+                try {
+                    Thread.sleep(100);
+                } catch (InterruptedException e1) {
+                    e1.printStackTrace();
+                }
 
-        // Grab each pixel information and set it to the BufferedImage info.
-        for (int x = 0; x < width; x++) {
-            for (int y = 0; y < height; y++) {
-               
-                int index = 3 * ((height - y - 1) * width + x);
-                int argb = (((int) (buff.get(index+0)) & 0xFF) << 16) //r
-                         | (((int) (buff.get(index+1)) & 0xFF) << 8)  //g
-                         | (((int) (buff.get(index+2)) & 0xFF));      //b
+                BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 
-                img.setRGB(x, y, argb);
-            }
-        }
+                // Grab each pixel information and set it to the BufferedImage info.
+                for (int x = 0; x < width; x++) {
+                    for (int y = 0; y < height; y++) {
+                       
+                        int index = 3 * ((height - y - 1) * width + x);
+                        int argb = (((int) (buff.get(index+0)) & 0xFF) << 16) //r
+                                 | (((int) (buff.get(index+1)) & 0xFF) << 8)  //g
+                                 | (((int) (buff.get(index+2)) & 0xFF));      //b
 
-        // write out the screenshot image to a file.
-        try {
-            return ImageIO.write(img, "png", out);
-        } catch (IOException e) {
-            logger.warning("Could not create file: " + filename + ".png");
-            return false;
-        }
+                        img.setRGB(x, y, argb);
+                    }
+                }
+
+                // write out the screenshot image to a file.
+                try {
+                    File out = new File(filename + ".png");
+                    logger.log(Level.INFO, "Taking screenshot: {0}", out.getAbsolutePath());
+                    ImageIO.write(img, "png", out);
+                } catch (IOException e) {
+                    logger.warning("Could not create file: " + filename + ".png");
+                }
+            }
+        };
+        saveThread.start();
+        return true;
+       
     }
 
     /**

I solved the problem.  :smiley:

But I can't understand why the 'width' 'height' value is changing over time.