From: nathan.sweet Date: Sun, 14 Aug 2011 00:44:07 +0000 (+0000) Subject: [updated] LwjglCanvas to shutdown properly when stop() is called. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=7829e573e951926091b8cd2453b2dcc3980e1a2a;p=mikumikustudio%2Flibgdx-mikumikustudio.git [updated] LwjglCanvas to shutdown properly when stop() is called. [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). --- diff --git a/backends/gdx-backend-lwjgl/.classpath b/backends/gdx-backend-lwjgl/.classpath index c5d281c40..606a7cdf5 100644 --- a/backends/gdx-backend-lwjgl/.classpath +++ b/backends/gdx-backend-lwjgl/.classpath @@ -2,7 +2,7 @@ - + diff --git a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.java b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.java index 7acd1356c..78a0c6aee 100644 --- a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.java +++ b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglCanvas.java @@ -39,11 +39,9 @@ import com.badlogic.gdx.Preferences; import com.badlogic.gdx.backends.openal.OpenALAudio; import com.badlogic.gdx.utils.GdxRuntimeException; -/** - * An OpenGL surface on an AWT Canvas, allowing OpenGL to be embedded in a Swing application. All OpenGL calls are done on the +/** An OpenGL surface on an AWT Canvas, allowing OpenGL to be embedded in a Swing application. All OpenGL calls are done on the * EDT. This is slightly less efficient then a dedicated thread, but greatly simplifies synchronization. - * @author Nathan Sweet - */ + * @author Nathan Sweet */ public class LwjglCanvas implements Application { final LwjglGraphics graphics; final OpenALAudio audio; @@ -54,7 +52,7 @@ public class LwjglCanvas implements Application { final List runnables = new ArrayList(); boolean running = true; int logLevel = LOG_INFO; - + public LwjglCanvas (ApplicationListener listener, boolean useGL2) { LwjglNativesLoader.load(); @@ -98,27 +96,33 @@ public class LwjglCanvas implements Application { return canvas; } - @Override public Audio getAudio () { + @Override + public Audio getAudio () { return audio; } - @Override public Files getFiles () { + @Override + public Files getFiles () { return files; } - @Override public Graphics getGraphics () { + @Override + public Graphics getGraphics () { return graphics; } - @Override public Input getInput () { + @Override + public Input getInput () { return input; } - @Override public ApplicationType getType () { + @Override + public ApplicationType getType () { return ApplicationType.Desktop; } - @Override public int getVersion () { + @Override + public int getVersion () { return 0; } @@ -132,7 +136,7 @@ public class LwjglCanvas implements Application { listener.create(); listener.resize(Math.max(1, graphics.getWidth()), Math.max(1, graphics.getHeight())); - final Runnable runnable = new Runnable() { + EventQueue.invokeLater(new Runnable() { int lastWidth = Math.max(1, graphics.getWidth()); int lastHeight = Math.max(1, graphics.getHeight()); @@ -159,45 +163,37 @@ public class LwjglCanvas implements Application { audio.update(); Display.update(); if (graphics.vsync) Display.sync(60); + if (running && !Display.isCloseRequested()) EventQueue.invokeLater(this); } - }; - - new Thread("LWJGL Canvas") { - public void run () { - while (running && !Display.isCloseRequested()) { - try { - EventQueue.invokeAndWait(runnable); - } catch (Exception ex) { - throw new GdxRuntimeException(ex); - } - } - } - }.start(); + }); } public void stop () { + if (!running) return; + running = false; + Display.destroy(); EventQueue.invokeLater(new Runnable() { public void run () { - if (!running) return; - running = false; listener.pause(); listener.dispose(); - Display.destroy(); } }); } - @Override public long getJavaHeap () { + @Override + public long getJavaHeap () { return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); } - @Override public long getNativeHeap () { + @Override + public long getNativeHeap () { return getJavaHeap(); } Map preferences = new HashMap(); - @Override public Preferences getPreferences (String name) { + @Override + public Preferences getPreferences (String name) { if (preferences.containsKey(name)) { return preferences.get(name); } else { @@ -207,51 +203,56 @@ public class LwjglCanvas implements Application { } } - @Override public void postRunnable (Runnable runnable) { + @Override + public void postRunnable (Runnable runnable) { synchronized (runnables) { runnables.add(runnable); } } - public void log(String tag, String message) { - if(logLevel >= LOG_INFO) { - System.out.println(tag + ":" + message); + public void log (String tag, String message) { + if (logLevel >= LOG_INFO) { + System.out.println(tag + ":" + message); } - } + } - @Override public void log (String tag, String message, Exception exception) { - if(logLevel >= LOG_INFO) { + @Override + public void log (String tag, String message, Exception exception) { + if (logLevel >= LOG_INFO) { System.out.println(tag + ":" + message); exception.printStackTrace(System.out); } } - - @Override public void error (String tag, String message) { - if(logLevel >= LOG_ERROR) { - System.err.println(tag + ":" + message); + + @Override + public void error (String tag, String message) { + if (logLevel >= LOG_ERROR) { + System.err.println(tag + ":" + message); } } - - @Override public void error (String tag, String message, Exception exception) { - if(logLevel >= LOG_ERROR) { + @Override + public void error (String tag, String message, Exception exception) { + if (logLevel >= LOG_ERROR) { System.err.println(tag + ":" + message); exception.printStackTrace(System.err); } } - - @Override public void setLogLevel (int logLevel) { + @Override + public void setLogLevel (int logLevel) { this.logLevel = logLevel; } - - @Override public void exit () { + + @Override + public void exit () { postRunnable(new Runnable() { - @Override public void run () { + @Override + public void run () { LwjglCanvas.this.listener.pause(); LwjglCanvas.this.listener.dispose(); System.exit(-1); - } + } }); } } 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 index 000000000..bbe7c86e0 --- /dev/null +++ b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/lwjgl/LwjglFrame.java @@ -0,0 +1,29 @@ + +package com.badlogic.gdx.backends.lwjgl; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JFrame; + +import com.badlogic.gdx.ApplicationListener; +import com.badlogic.gdx.Gdx; + +/** Wraps an {@link LwjglCanvas} in a resizable {@link JFrame}. */ +public class LwjglFrame extends JFrame { + public LwjglFrame (ApplicationListener listener, String title, int width, int height, boolean useGL2) { + super(title); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setSize(width, height); + setLocationRelativeTo(null); + + final LwjglCanvas lwjglCanvas = new LwjglCanvas(listener, useGL2); + getContentPane().add(lwjglCanvas.getCanvas()); + + addWindowListener(new WindowAdapter() { + public void windowClosing (WindowEvent e) { + lwjglCanvas.stop(); + } + }); + } +} diff --git a/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALMusic.java b/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALMusic.java index fecf2af64..4363359c0 100644 --- a/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALMusic.java +++ b/backends/gdx-openal/src/com/badlogic/gdx/backends/openal/OpenALMusic.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ + package com.badlogic.gdx.backends.openal; import java.nio.ByteBuffer; @@ -27,9 +28,7 @@ import com.badlogic.gdx.utils.GdxRuntimeException; import static org.lwjgl.openal.AL10.*; -/** - * @author Nathan Sweet - */ +/** @author Nathan Sweet */ public abstract class OpenALMusic implements Music { static private final int bufferSize = 4096 * 10; static private final int bufferCount = 3; @@ -51,10 +50,6 @@ public abstract class OpenALMusic implements Music { this.audio = audio; this.file = file; - buffers = BufferUtils.createIntBuffer(bufferCount); - alGenBuffers(buffers); - if (alGetError() != AL_NO_ERROR) throw new GdxRuntimeException("Unabe to allocate audio buffers."); - audio.music.add(this); } @@ -68,6 +63,11 @@ public abstract class OpenALMusic implements Music { if (sourceID == -1) { sourceID = audio.obtainSource(true); if (sourceID == -1) return; + if (buffers == null) { + buffers = BufferUtils.createIntBuffer(bufferCount); + alGenBuffers(buffers); + if (alGetError() != AL_NO_ERROR) throw new GdxRuntimeException("Unabe to allocate audio buffers."); + } alSourcei(sourceID, AL_LOOPING, AL_FALSE); alSourcef(sourceID, AL_GAIN, volume); for (int i = 0; i < bufferCount; i++) { @@ -121,15 +121,11 @@ public abstract class OpenALMusic implements Music { return renderedSeconds + alGetSourcef(sourceID, AL11.AL_SEC_OFFSET); } - /** - * Fills as much of the buffer as possible and returns the number of bytes filled. Returns <= 0 to indicate the end of the - * stream. - */ + /** Fills as much of the buffer as possible and returns the number of bytes filled. Returns <= 0 to indicate the end of the + * stream. */ abstract protected int read (byte[] buffer); - /** - * Resets the stream to the beginning. - */ + /** Resets the stream to the beginning. */ abstract protected void reset (); public void update () { diff --git a/gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java b/gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java index fd20489f2..57e3684b0 100644 --- a/gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java +++ b/gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java @@ -102,7 +102,7 @@ public class Gdx2DPixmap implements Disposable { if(pixelPtr == null) throw new IllegalArgumentException("couldn't load pixmap"); - System.arraycopy(nativeData, 0, this.nativeData, 0, 4); + System.arraycopy(nativeData, 0, Gdx2DPixmap.nativeData, 0, 4); this.basePtr = nativeData[0]; this.width = (int)nativeData[1]; this.height = (int)nativeData[2]; diff --git a/gdx/src/com/badlogic/gdx/graphics/glutils/ETC1.java b/gdx/src/com/badlogic/gdx/graphics/glutils/ETC1.java index d5c6bba51..8a935da96 100644 --- a/gdx/src/com/badlogic/gdx/graphics/glutils/ETC1.java +++ b/gdx/src/com/badlogic/gdx/graphics/glutils/ETC1.java @@ -96,7 +96,7 @@ public class ETC1 { /** * Writes the ETC1Data with a PKM header to the given file. - * @param handle the file. + * @param file the file. */ public void write(FileHandle file) { DataOutputStream write = null; diff --git a/gdx/src/com/badlogic/gdx/math/FileProcessor.java b/gdx/src/com/badlogic/gdx/math/FileProcessor.java new file mode 100644 index 000000000..9896456b0 --- /dev/null +++ b/gdx/src/com/badlogic/gdx/math/FileProcessor.java @@ -0,0 +1,151 @@ + +package com.badlogic.gdx.math; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map.Entry; + +public class FileProcessor { + FilenameFilter inputFilter; + Comparator comparator; + String inputSuffix; + String outputSuffix; + ArrayList outputFiles = new ArrayList(); + boolean recursive = true; + boolean flattenOutput; + + public FileProcessor () { + } + + public FileProcessor (String inputSuffix) { + this.inputSuffix = inputSuffix; + } + + Comparator inputFileComparator = new Comparator() { + public int compare (InputFile o1, InputFile o2) { + return comparator.compare(o1.inputFile, o2.inputFile); + } + }; + + public void setInputFilter (FilenameFilter inputFilter) { + this.inputFilter = inputFilter; + } + + public void setComparator (Comparator comparator) { + this.comparator = comparator; + } + + public void setInputSuffix (String inputSuffix) { + this.inputSuffix = inputSuffix; + } + + public void setOutputSuffix (String outputSuffix) { + this.outputSuffix = outputSuffix; + } + + public void setFlattenOutput (boolean flattenOutput) { + this.flattenOutput = flattenOutput; + } + + public void setRecursive (boolean recursive) { + this.recursive = recursive; + } + + public void process (File inputFile, File outputRoot) throws Exception { + if (inputFile.isFile()) + process(new File[] {inputFile}, outputRoot); + else + process(inputFile.listFiles(), outputRoot); + } + + public ArrayList process (File[] files, File outputRoot) throws Exception { + outputFiles.clear(); + + HashMap> dirToEntries = new HashMap(); + process(files, outputRoot, outputRoot, dirToEntries, 0); + + ArrayList allInputFiles = new ArrayList(); + for (Entry> entry : dirToEntries.entrySet()) { + ArrayList dirInputFiles = entry.getValue(); + if (comparator != null) Collections.sort(dirInputFiles, inputFileComparator); + + File inputDir = entry.getKey(); + File newOutputDir = flattenOutput ? outputRoot : dirInputFiles.get(0).outputDir; + String outputName = inputDir.getName(); + if (outputSuffix != null) outputName += outputSuffix; + + InputFile inputFile = new InputFile(); + inputFile.inputFile = entry.getKey(); + inputFile.outputDir = newOutputDir; + inputFile.outputFile = new File(newOutputDir, outputName); + + processDir(inputFile, dirInputFiles); + allInputFiles.addAll(dirInputFiles); + } + + if (comparator != null) Collections.sort(allInputFiles, inputFileComparator); + for (InputFile inputFile : allInputFiles) + processFile(inputFile); + + return outputFiles; + } + + private void process (File[] files, File outputRoot, File outputDir, HashMap> dirToEntries, + int depth) { + for (File file : files) { + if (file.isFile()) { + if (inputSuffix != null && !file.getName().endsWith(inputSuffix)) continue; + + File dir = file.getParentFile(); + if (inputFilter != null && !inputFilter.accept(dir, file.getName())) continue; + + String outputName = file.getName(); + if (outputSuffix != null) outputName = outputName.replaceAll("(.*)\\..*", "$1") + outputSuffix; + + InputFile inputFile = new InputFile(); + inputFile.depth = depth; + inputFile.inputFile = file; + inputFile.outputDir = outputDir; + inputFile.outputFile = flattenOutput ? new File(outputRoot, outputName) : new File(outputDir, outputName); + ArrayList inputFiles = dirToEntries.get(dir); + if (inputFiles == null) { + inputFiles = new ArrayList(); + dirToEntries.put(dir, inputFiles); + } + inputFiles.add(inputFile); + } + if (recursive && file.isDirectory()) + process(file.listFiles(inputFilter), outputRoot, new File(outputDir, file.getName()), dirToEntries, depth + 1); + } + } + + protected void processFile (InputFile inputFile) throws Exception { + } + + protected void processDir (InputFile inputDir, ArrayList value) throws Exception { + } + + protected void addOutputFile (InputFile inputFile) { + outputFiles.add(inputFile); + } + + static public class InputFile { + public File inputFile; + public File outputDir; + public File outputFile; + public int depth; + + public InputFile () { + } + + public InputFile (File inputFile, File outputFile) { + this.inputFile = inputFile; + this.outputFile = outputFile; + } + } +} + \ No newline at end of file diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Label.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Label.java index abaf75565..1631defda 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Label.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Label.java @@ -137,6 +137,7 @@ public class Label extends Widget { public void setText(String text) { this.label = text; + layout(); invalidateHierarchy(); } } diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/tablelayout/BaseTableLayout.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/tablelayout/BaseTableLayout.java index ee72ef418..3c3aa81a0 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/tablelayout/BaseTableLayout.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/tablelayout/BaseTableLayout.java @@ -504,14 +504,14 @@ abstract public class BaseTableLayout