OSDN Git Service

[updated] LwjglCanvas to shutdown properly when stop() is called.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sun, 14 Aug 2011 00:44:07 +0000 (00:44 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sun, 14 Aug 2011 00:44:07 +0000 (00:44 +0000)
[updated] OpenALMusic to not gen AL buffers until as late as possible.
[fixed] Warnings.
[fixed] Label layout when text is changed.
[fixed] TableLayout alignment methods.
[added] LwjglFrame for a resizable LJWGL application window.
[added] FileProcessor to utils (should go in tools).

backends/gdx-backend-lwjgl/.classpath
backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.java
backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglFrame.java [new file with mode: 0644]
backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALMusic.java
gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java
gdx/src/com/badlogic/gdx/graphics/glutils/ETC1.java
gdx/src/com/badlogic/gdx/math/FileProcessor.java [new file with mode: 0644]
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Label.java
gdx/src/com/badlogic/gdx/scenes/scene2d/ui/tablelayout/BaseTableLayout.java

index c5d281c..606a7cd 100644 (file)
@@ -2,7 +2,7 @@
 <classpath>\r
        <classpathentry excluding="**/.svn/*" kind="src" path="src"/>\r
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>\r
-       <classpathentry kind="lib" path="/gdx/libs/gdx-natives.jar"/>\r
+       <classpathentry exported="true" kind="lib" path="/gdx/libs/gdx-natives.jar"/>\r
        <classpathentry kind="lib" path="libs/gdx-backend-lwjgl-natives.jar"/>\r
        <classpathentry combineaccessrules="false" exported="true" kind="src" path="/gdx-openal"/>\r
        <classpathentry kind="lib" path="libs/lwjgl_util.jar"/>\r
index 7acd135..78a0c6a 100644 (file)
@@ -39,11 +39,9 @@ import com.badlogic.gdx.Preferences;
 import com.badlogic.gdx.backends.openal.OpenALAudio;\r
 import com.badlogic.gdx.utils.GdxRuntimeException;\r
 \r
-/**\r
- * An OpenGL surface on an AWT Canvas, allowing OpenGL to be embedded in a Swing application. All OpenGL calls are done on the\r
+/** An OpenGL surface on an AWT Canvas, allowing OpenGL to be embedded in a Swing application. All OpenGL calls are done on the\r
  * EDT. This is slightly less efficient then a dedicated thread, but greatly simplifies synchronization.\r
- * @author Nathan Sweet\r
- */\r
+ * @author Nathan Sweet */\r
 public class LwjglCanvas implements Application {\r
        final LwjglGraphics graphics;\r
        final OpenALAudio audio;\r
@@ -54,7 +52,7 @@ public class LwjglCanvas implements Application {
        final List<Runnable> runnables = new ArrayList<Runnable>();\r
        boolean running = true;\r
        int logLevel = LOG_INFO;\r
-       \r
+\r
        public LwjglCanvas (ApplicationListener listener, boolean useGL2) {\r
                LwjglNativesLoader.load();\r
 \r
@@ -98,27 +96,33 @@ public class LwjglCanvas implements Application {
                return canvas;\r
        }\r
 \r
-       @Override public Audio getAudio () {\r
+       @Override\r
+       public Audio getAudio () {\r
                return audio;\r
        }\r
 \r
-       @Override public Files getFiles () {\r
+       @Override\r
+       public Files getFiles () {\r
                return files;\r
        }\r
 \r
-       @Override public Graphics getGraphics () {\r
+       @Override\r
+       public Graphics getGraphics () {\r
                return graphics;\r
        }\r
 \r
-       @Override public Input getInput () {\r
+       @Override\r
+       public Input getInput () {\r
                return input;\r
        }\r
 \r
-       @Override public ApplicationType getType () {\r
+       @Override\r
+       public ApplicationType getType () {\r
                return ApplicationType.Desktop;\r
        }\r
 \r
-       @Override public int getVersion () {\r
+       @Override\r
+       public int getVersion () {\r
                return 0;\r
        }\r
 \r
@@ -132,7 +136,7 @@ public class LwjglCanvas implements Application {
                listener.create();\r
                listener.resize(Math.max(1, graphics.getWidth()), Math.max(1, graphics.getHeight()));\r
 \r
-               final Runnable runnable = new Runnable() {\r
+               EventQueue.invokeLater(new Runnable() {\r
                        int lastWidth = Math.max(1, graphics.getWidth());\r
                        int lastHeight = Math.max(1, graphics.getHeight());\r
 \r
@@ -159,45 +163,37 @@ public class LwjglCanvas implements Application {
                                audio.update();\r
                                Display.update();\r
                                if (graphics.vsync) Display.sync(60);\r
+                               if (running && !Display.isCloseRequested()) EventQueue.invokeLater(this);\r
                        }\r
-               };\r
-\r
-               new Thread("LWJGL Canvas") {\r
-                       public void run () {\r
-                               while (running && !Display.isCloseRequested()) {\r
-                                       try {\r
-                                               EventQueue.invokeAndWait(runnable);\r
-                                       } catch (Exception ex) {\r
-                                               throw new GdxRuntimeException(ex);\r
-                                       }\r
-                               }\r
-                       }\r
-               }.start();\r
+               });\r
        }\r
 \r
        public void stop () {\r
+               if (!running) return;\r
+               running = false;\r
+               Display.destroy();\r
                EventQueue.invokeLater(new Runnable() {\r
                        public void run () {\r
-                               if (!running) return;\r
-                               running = false;\r
                                listener.pause();\r
                                listener.dispose();\r
-                               Display.destroy();\r
                        }\r
                });\r
        }\r
 \r
-       @Override public long getJavaHeap () {\r
+       @Override\r
+       public long getJavaHeap () {\r
                return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();\r
        }\r
 \r
-       @Override public long getNativeHeap () {\r
+       @Override\r
+       public long getNativeHeap () {\r
                return getJavaHeap();\r
        }\r
 \r
        Map<String, Preferences> preferences = new HashMap<String, Preferences>();\r
 \r
-       @Override public Preferences getPreferences (String name) {\r
+       @Override\r
+       public Preferences getPreferences (String name) {\r
                if (preferences.containsKey(name)) {\r
                        return preferences.get(name);\r
                } else {\r
@@ -207,51 +203,56 @@ public class LwjglCanvas implements Application {
                }\r
        }\r
 \r
-       @Override public void postRunnable (Runnable runnable) {\r
+       @Override\r
+       public void postRunnable (Runnable runnable) {\r
                synchronized (runnables) {\r
                        runnables.add(runnable);\r
                }\r
        }\r
 \r
-       public void log(String tag, String message) {\r
-       if(logLevel >= LOG_INFO) {\r
-                       System.out.println(tag + ":" + message);                \r
+       public void log (String tag, String message) {\r
+               if (logLevel >= LOG_INFO) {\r
+                       System.out.println(tag + ":" + message);\r
                }\r
-   }\r
+       }\r
 \r
-       @Override public void log (String tag, String message, Exception exception) {\r
-               if(logLevel >= LOG_INFO) {\r
+       @Override\r
+       public void log (String tag, String message, Exception exception) {\r
+               if (logLevel >= LOG_INFO) {\r
                        System.out.println(tag + ":" + message);\r
                        exception.printStackTrace(System.out);\r
                }\r
        }\r
-       \r
-       @Override public void error (String tag, String message) {\r
-               if(logLevel >= LOG_ERROR) {\r
-                       System.err.println(tag + ":" + message);                        \r
+\r
+       @Override\r
+       public void error (String tag, String message) {\r
+               if (logLevel >= LOG_ERROR) {\r
+                       System.err.println(tag + ":" + message);\r
                }\r
        }\r
 \r
-\r
-       @Override public void error (String tag, String message, Exception exception) {\r
-               if(logLevel >= LOG_ERROR) {\r
+       @Override\r
+       public void error (String tag, String message, Exception exception) {\r
+               if (logLevel >= LOG_ERROR) {\r
                        System.err.println(tag + ":" + message);\r
                        exception.printStackTrace(System.err);\r
                }\r
        }\r
 \r
-\r
-       @Override public void setLogLevel (int logLevel) {              \r
+       @Override\r
+       public void setLogLevel (int logLevel) {\r
                this.logLevel = logLevel;\r
        }\r
-       \r
-       @Override public void exit () {\r
+\r
+       @Override\r
+       public void exit () {\r
                postRunnable(new Runnable() {\r
-                       @Override public void run () {\r
+                       @Override\r
+                       public void run () {\r
                                LwjglCanvas.this.listener.pause();\r
                                LwjglCanvas.this.listener.dispose();\r
                                System.exit(-1);\r
-                       }                       \r
+                       }\r
                });\r
        }\r
 }\r
diff --git a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglFrame.java b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglFrame.java
new file mode 100644 (file)
index 0000000..bbe7c86
--- /dev/null
@@ -0,0 +1,29 @@
+\r
+package com.badlogic.gdx.backends.lwjgl;\r
+\r
+import java.awt.event.WindowAdapter;\r
+import java.awt.event.WindowEvent;\r
+\r
+import javax.swing.JFrame;\r
+\r
+import com.badlogic.gdx.ApplicationListener;\r
+import com.badlogic.gdx.Gdx;\r
+\r
+/** Wraps an {@link LwjglCanvas} in a resizable {@link JFrame}. */\r
+public class LwjglFrame extends JFrame {\r
+       public LwjglFrame (ApplicationListener listener, String title, int width, int height, boolean useGL2) {\r
+               super(title);\r
+               setDefaultCloseOperation(EXIT_ON_CLOSE);\r
+               setSize(width, height);\r
+               setLocationRelativeTo(null);\r
+\r
+               final LwjglCanvas lwjglCanvas = new LwjglCanvas(listener, useGL2);\r
+               getContentPane().add(lwjglCanvas.getCanvas());\r
+\r
+               addWindowListener(new WindowAdapter() {\r
+                       public void windowClosing (WindowEvent e) {\r
+                               lwjglCanvas.stop();\r
+                       }\r
+               });\r
+       }\r
+}\r
index fecf2af..4363359 100644 (file)
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and\r
  * limitations under the License.\r
  ******************************************************************************/\r
+\r
 package com.badlogic.gdx.backends.openal;\r
 \r
 import java.nio.ByteBuffer;\r
@@ -27,9 +28,7 @@ import com.badlogic.gdx.utils.GdxRuntimeException;
 \r
 import static org.lwjgl.openal.AL10.*;\r
 \r
-/**\r
- * @author Nathan Sweet\r
- */\r
+/** @author Nathan Sweet */\r
 public abstract class OpenALMusic implements Music {\r
        static private final int bufferSize = 4096 * 10;\r
        static private final int bufferCount = 3;\r
@@ -51,10 +50,6 @@ public abstract class OpenALMusic implements Music {
                this.audio = audio;\r
                this.file = file;\r
 \r
-               buffers = BufferUtils.createIntBuffer(bufferCount);\r
-               alGenBuffers(buffers);\r
-               if (alGetError() != AL_NO_ERROR) throw new GdxRuntimeException("Unabe to allocate audio buffers.");\r
-\r
                audio.music.add(this);\r
        }\r
 \r
@@ -68,6 +63,11 @@ public abstract class OpenALMusic implements Music {
                if (sourceID == -1) {\r
                        sourceID = audio.obtainSource(true);\r
                        if (sourceID == -1) return;\r
+                       if (buffers == null) {\r
+                               buffers = BufferUtils.createIntBuffer(bufferCount);\r
+                               alGenBuffers(buffers);\r
+                               if (alGetError() != AL_NO_ERROR) throw new GdxRuntimeException("Unabe to allocate audio buffers.");\r
+                       }\r
                        alSourcei(sourceID, AL_LOOPING, AL_FALSE);\r
                        alSourcef(sourceID, AL_GAIN, volume);\r
                        for (int i = 0; i < bufferCount; i++) {\r
@@ -121,15 +121,11 @@ public abstract class OpenALMusic implements Music {
                return renderedSeconds + alGetSourcef(sourceID, AL11.AL_SEC_OFFSET);\r
        }\r
 \r
-       /**\r
-        * Fills as much of the buffer as possible and returns the number of bytes filled. Returns <= 0 to indicate the end of the\r
-        * stream.\r
-        */\r
+       /** Fills as much of the buffer as possible and returns the number of bytes filled. Returns <= 0 to indicate the end of the\r
+        * stream. */\r
        abstract protected int read (byte[] buffer);\r
 \r
-       /**\r
-        * Resets the stream to the beginning.\r
-        */\r
+       /** Resets the stream to the beginning. */\r
        abstract protected void reset ();\r
 \r
        public void update () {\r
index fd20489..57e3684 100644 (file)
@@ -102,7 +102,7 @@ public class Gdx2DPixmap implements Disposable {
                if(pixelPtr == null)\r
                        throw new IllegalArgumentException("couldn't load pixmap");\r
                \r
-               System.arraycopy(nativeData, 0, this.nativeData, 0, 4);\r
+               System.arraycopy(nativeData, 0, Gdx2DPixmap.nativeData, 0, 4);\r
                this.basePtr = nativeData[0];\r
                this.width = (int)nativeData[1];\r
                this.height = (int)nativeData[2];\r
index d5c6bba..8a935da 100644 (file)
@@ -96,7 +96,7 @@ public class ETC1 {
                \r
                /**\r
                 * Writes the ETC1Data with a PKM header to the given file.\r
-                * @param handle the file.\r
+                * @param file the file.\r
                 */\r
                public void write(FileHandle file) {\r
                        DataOutputStream write = null;\r
diff --git a/gdx/src/com/badlogic/gdx/math/FileProcessor.java b/gdx/src/com/badlogic/gdx/math/FileProcessor.java
new file mode 100644 (file)
index 0000000..9896456
--- /dev/null
@@ -0,0 +1,151 @@
+\r
+package com.badlogic.gdx.math;\r
+\r
+import java.io.File;\r
+import java.io.FilenameFilter;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.HashMap;\r
+import java.util.Map.Entry;\r
+\r
+public class FileProcessor {\r
+       FilenameFilter inputFilter;\r
+       Comparator<File> comparator;\r
+       String inputSuffix;\r
+       String outputSuffix;\r
+       ArrayList<InputFile> outputFiles = new ArrayList();\r
+       boolean recursive = true;\r
+       boolean flattenOutput;\r
+\r
+       public FileProcessor () {\r
+       }\r
+\r
+       public FileProcessor (String inputSuffix) {\r
+               this.inputSuffix = inputSuffix;\r
+       }\r
+\r
+       Comparator<InputFile> inputFileComparator = new Comparator<InputFile>() {\r
+               public int compare (InputFile o1, InputFile o2) {\r
+                       return comparator.compare(o1.inputFile, o2.inputFile);\r
+               }\r
+       };\r
+\r
+       public void setInputFilter (FilenameFilter inputFilter) {\r
+               this.inputFilter = inputFilter;\r
+       }\r
+\r
+       public void setComparator (Comparator<File> comparator) {\r
+               this.comparator = comparator;\r
+       }\r
+\r
+       public void setInputSuffix (String inputSuffix) {\r
+               this.inputSuffix = inputSuffix;\r
+       }\r
+\r
+       public void setOutputSuffix (String outputSuffix) {\r
+               this.outputSuffix = outputSuffix;\r
+       }\r
+\r
+       public void setFlattenOutput (boolean flattenOutput) {\r
+               this.flattenOutput = flattenOutput;\r
+       }\r
+\r
+       public void setRecursive (boolean recursive) {\r
+               this.recursive = recursive;\r
+       }\r
+\r
+       public void process (File inputFile, File outputRoot) throws Exception {\r
+               if (inputFile.isFile())\r
+                       process(new File[] {inputFile}, outputRoot);\r
+               else\r
+                       process(inputFile.listFiles(), outputRoot);\r
+       }\r
+\r
+       public ArrayList<InputFile> process (File[] files, File outputRoot) throws Exception {\r
+               outputFiles.clear();\r
+\r
+               HashMap<File, ArrayList<InputFile>> dirToEntries = new HashMap();\r
+               process(files, outputRoot, outputRoot, dirToEntries, 0);\r
+\r
+               ArrayList<InputFile> allInputFiles = new ArrayList();\r
+               for (Entry<File, ArrayList<InputFile>> entry : dirToEntries.entrySet()) {\r
+                       ArrayList<InputFile> dirInputFiles = entry.getValue();\r
+                       if (comparator != null) Collections.sort(dirInputFiles, inputFileComparator);\r
+\r
+                       File inputDir = entry.getKey();\r
+                       File newOutputDir = flattenOutput ? outputRoot : dirInputFiles.get(0).outputDir;\r
+                       String outputName = inputDir.getName();\r
+                       if (outputSuffix != null) outputName += outputSuffix;\r
+\r
+                       InputFile inputFile = new InputFile();\r
+                       inputFile.inputFile = entry.getKey();\r
+                       inputFile.outputDir = newOutputDir;\r
+                       inputFile.outputFile = new File(newOutputDir, outputName);\r
+\r
+                       processDir(inputFile, dirInputFiles);\r
+                       allInputFiles.addAll(dirInputFiles);\r
+               }\r
+\r
+               if (comparator != null) Collections.sort(allInputFiles, inputFileComparator);\r
+               for (InputFile inputFile : allInputFiles)\r
+                       processFile(inputFile);\r
+\r
+               return outputFiles;\r
+       }\r
+\r
+       private void process (File[] files, File outputRoot, File outputDir, HashMap<File, ArrayList<InputFile>> dirToEntries,\r
+               int depth) {\r
+               for (File file : files) {\r
+                       if (file.isFile()) {\r
+                               if (inputSuffix != null && !file.getName().endsWith(inputSuffix)) continue;\r
+\r
+                               File dir = file.getParentFile();\r
+                               if (inputFilter != null && !inputFilter.accept(dir, file.getName())) continue;\r
+\r
+                               String outputName = file.getName();\r
+                               if (outputSuffix != null) outputName = outputName.replaceAll("(.*)\\..*", "$1") + outputSuffix;\r
+\r
+                               InputFile inputFile = new InputFile();\r
+                               inputFile.depth = depth;\r
+                               inputFile.inputFile = file;\r
+                               inputFile.outputDir = outputDir;\r
+                               inputFile.outputFile = flattenOutput ? new File(outputRoot, outputName) : new File(outputDir, outputName);\r
+                               ArrayList<InputFile> inputFiles = dirToEntries.get(dir);\r
+                               if (inputFiles == null) {\r
+                                       inputFiles = new ArrayList();\r
+                                       dirToEntries.put(dir, inputFiles);\r
+                               }\r
+                               inputFiles.add(inputFile);\r
+                       }\r
+                       if (recursive && file.isDirectory())\r
+                               process(file.listFiles(inputFilter), outputRoot, new File(outputDir, file.getName()), dirToEntries, depth + 1);\r
+               }\r
+       }\r
+\r
+       protected void processFile (InputFile inputFile) throws Exception {\r
+       }\r
+\r
+       protected void processDir (InputFile inputDir, ArrayList<InputFile> value) throws Exception {\r
+       }\r
+\r
+       protected void addOutputFile (InputFile inputFile) {\r
+               outputFiles.add(inputFile);\r
+       }\r
+\r
+       static public class InputFile {\r
+               public File inputFile;\r
+               public File outputDir;\r
+               public File outputFile;\r
+               public int depth;\r
+\r
+               public InputFile () {\r
+               }\r
+\r
+               public InputFile (File inputFile, File outputFile) {\r
+                       this.inputFile = inputFile;\r
+                       this.outputFile = outputFile;\r
+               }\r
+       }\r
+}\r
\ No newline at end of file
index abaf755..1631def 100644 (file)
@@ -137,6 +137,7 @@ public class Label extends Widget {
        \r
        public void setText(String text) {\r
                this.label = text;\r
+               layout();\r
                invalidateHierarchy();\r
        }\r
 }\r
index ee72ef4..3c3aa81 100644 (file)
@@ -504,14 +504,14 @@ abstract public class BaseTableLayout<C, T extends C, L extends BaseTableLayout,
 \r
        /** Sets the alignment of the table within the widget being laid out to {@link #BOTTOM}. */\r
        public L bottom () {\r
-               align = BOTTOM;\r
+               align |= BOTTOM;\r
                align &= ~TOP;\r
                return (L)this;\r
        }\r
 \r
        /** Sets the alignment of the table within the widget being laid out to {@link #RIGHT}. */\r
        public L right () {\r
-               align = RIGHT;\r
+               align |= RIGHT;\r
                align &= ~LEFT;\r
                return (L)this;\r
        }\r