From 8efe962d07f322286c616eb5110ff91a54283580 Mon Sep 17 00:00:00 2001 From: badlogicgames Date: Tue, 2 Nov 2010 02:55:32 +0000 Subject: [PATCH] i give up for today, Nate you take over. Shit doesn'T compile so make sure we have a compileable SVN trunk at the end of the day... --- .../gdx/backends/desktop/LwjglApplication.java | 8 +- .../badlogic/gdx/backends/desktop/LwjglCanvas.java | 179 +++++++++++++++++++++ .../gdx/backends/desktop/LwjglGraphics.java | 35 +++- .../hiero/src/com/badlogic/gdx/hiero/Hiero.java | 32 ++-- 4 files changed, 220 insertions(+), 34 deletions(-) create mode 100644 backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglCanvas.java diff --git a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglApplication.java b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglApplication.java index 55763dddf..16c6653ee 100644 --- a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglApplication.java +++ b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglApplication.java @@ -24,12 +24,8 @@ public class LwjglApplication implements Application { Thread mainLoopThread; boolean running = true; - public LwjglApplication(ApplicationListener listener, String title, int width, int height, boolean useGL2) { - this(listener, title, width, height, useGL2, null); - } - - public LwjglApplication(ApplicationListener listener, String title, int width, int height, boolean useGL2, Canvas canvas) { - graphics = new LwjglGraphics(title, width, height, useGL2, canvas); + public LwjglApplication(ApplicationListener listener, String title, int width, int height, boolean useGL2) { + graphics = new LwjglGraphics(title, width, height, useGL2); audio = new LwjglAudio(); files = new LwjglFiles(); input = new LwjglInput(); diff --git a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglCanvas.java b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglCanvas.java new file mode 100644 index 000000000..8b2b30583 --- /dev/null +++ b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglCanvas.java @@ -0,0 +1,179 @@ +package com.badlogic.gdx.backends.desktop; + +import java.awt.Canvas; +import java.awt.Dimension; +import java.lang.reflect.InvocationTargetException; + +import javax.swing.SwingUtilities; + +import org.lwjgl.LWJGLException; +import org.lwjgl.input.Keyboard; +import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.DisplayMode; + +import com.badlogic.gdx.Application; +import com.badlogic.gdx.ApplicationListener; +import com.badlogic.gdx.Audio; +import com.badlogic.gdx.Files; +import com.badlogic.gdx.Graphics; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.utils.GdxRuntimeException; + +public class LwjglCanvas implements Application { + LwjglGraphics graphics; + LwjglAudio audio; + LwjglFiles files; + LwjglInput input; + final ApplicationListener listener; + Thread mainLoopThread; + boolean running = true; + Canvas canvas; + + public LwjglCanvas( ApplicationListener listener, boolean useGL2) { + LwjglNativesLoader.load(); + + canvas = new Canvas() { + private final Dimension minSize = new Dimension(); + + public final void addNotify () { + super.addNotify(); + try { + graphics.setupDisplay(); + start(); + } catch (LWJGLException e) { + throw new GdxRuntimeException(e); + } + } + + public Dimension getMinimumSize () { + return minSize; + } + }; + + canvas.setSize(100, 100); + canvas.setMinimumSize(new Dimension(2,2)); + graphics = new LwjglGraphics( canvas, useGL2); + audio = new LwjglAudio(); + files = new LwjglFiles(); + input = new LwjglInput(); + this.listener = listener; + } + + public Canvas getCanvas() { + return canvas; + } + + @Override + public Audio getAudio() { + return audio; + } + + @Override + public Files getFiles() { + return files; + } + + @Override + public Graphics getGraphics() { + return graphics; + } + + @Override + public Input getInput() { + return input; + } + + @Override + public ApplicationType getType() { + return ApplicationType.Desktop; + } + + @Override + public int getVersion() { + return 0; + } + + @Override + public void log(String tag, String message) { + System.out.println( tag + ": " + message ); + } + + private void start() { + LwjglNativesLoader.load(); + mainLoopThread = new Thread("LWJGL Application") { + @SuppressWarnings("synthetic-access") + public void run () { + LwjglCanvas.this.mainLoop(); + } + }; + mainLoopThread.start(); + } + + private void mainLoop( ) { + Keyboard.enableRepeatEvents(true); + try { + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + listener.create(); + listener.resize(graphics.getWidth(), graphics.getHeight()); + } + }); + } catch (Exception e1) { + throw new GdxRuntimeException(e1); + } + + Runnable runnable = new Runnable() { + int lastWidth = graphics.getWidth(); + int lastHeight = graphics.getHeight(); + + @Override + public void run() { + graphics.updateTime(); + input.update(); + + if( lastWidth != graphics.getWidth() || lastHeight != graphics.getHeight() ) { + lastWidth = graphics.getWidth(); + lastHeight = graphics.getHeight(); + try { + Display.setDisplayMode(new DisplayMode(lastWidth, lastHeight)); + } catch (LWJGLException e) { + throw new GdxRuntimeException(e); + } + listener.resize(lastWidth, lastHeight); + } + + listener.render(); + input.processEvents(null); + Display.update(); + Display.sync(60); + } + }; + + while(running) { + SwingUtilities.invokeLater(runnable); + try { + Thread.sleep(16); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + SwingUtilities.invokeLater(new Runnable() { + public void run() { + listener.pause(); + listener.destroy(); + Display.destroy(); + } + }); + } + + public void stop() { + running = false; + try { + mainLoopThread.join(); + } + catch(Exception ex) { + } + } +} diff --git a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglGraphics.java b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglGraphics.java index 2e05c0340..4d5f025ca 100644 --- a/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglGraphics.java +++ b/backends/gdx-backend-lwjgl/src/com/badlogic/gdx/backends/desktop/LwjglGraphics.java @@ -58,11 +58,18 @@ public final class LwjglGraphics implements Graphics{ String title; Canvas canvas; - LwjglGraphics (String title, int width, int height, boolean useGL2IfAvailable, Canvas canvas) { + LwjglGraphics (String title, int width, int height, boolean useGL2IfAvailable) { useGL2 = useGL2IfAvailable; this.title = title; this.width = width; - this.height = height; + this.height = height; + } + + LwjglGraphics(Canvas canvas, boolean useGL2IfAvailable) { + useGL2 = useGL2IfAvailable; + this.title = ""; + this.width = canvas.getWidth(); + this.height = canvas.getHeight(); this.canvas = canvas; } @@ -79,11 +86,17 @@ public final class LwjglGraphics implements Graphics{ } public int getHeight () { - return height; + if(canvas!=null) + return canvas.getHeight(); + else + return height; } public int getWidth () { - return width; + if(canvas!=null) + return canvas.getWidth(); + else + return width; } public boolean isGL11Available () { @@ -177,10 +190,16 @@ public final class LwjglGraphics implements Graphics{ } protected void setupDisplay () throws LWJGLException { - if(canvas!=null) Display.setParent(canvas); - Display.setDisplayMode(new DisplayMode(width, height)); - Display.setFullscreen(false); - Display.setTitle(title); + if(canvas!=null) { + Display.setParent(canvas); + Display.setDisplayMode(new DisplayMode(canvas.getWidth(), canvas.getHeight())); + Display.setFullscreen(false); + } + else { + Display.setDisplayMode(new DisplayMode(width, height)); + Display.setFullscreen(false); + Display.setTitle(title); + } int samples = 0; try { Display.create(new PixelFormat(8, 8, 0, samples)); diff --git a/extensions/hiero/src/com/badlogic/gdx/hiero/Hiero.java b/extensions/hiero/src/com/badlogic/gdx/hiero/Hiero.java index a7b2df544..a67bc3e7a 100644 --- a/extensions/hiero/src/com/badlogic/gdx/hiero/Hiero.java +++ b/extensions/hiero/src/com/badlogic/gdx/hiero/Hiero.java @@ -86,6 +86,7 @@ import javax.swing.JWindow; import javax.swing.KeyStroke; import javax.swing.ScrollPaneConstants; import javax.swing.SpinnerNumberModel; +import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; @@ -99,6 +100,7 @@ import org.lwjgl.opengl.GL11; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.backends.desktop.LwjglApplication; +import com.badlogic.gdx.backends.desktop.LwjglCanvas; import com.badlogic.gdx.graphics.BitmapFont; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Texture; @@ -181,6 +183,9 @@ public class Hiero extends JFrame { Splash splash = new Splash(this, "/splash.jpg", 2000); initialize(); splash.close(); + + LwjglCanvas lwjglCanvas = new LwjglCanvas(new Renderer(), false); + gamePanel.add(lwjglCanvas.getCanvas()); prefs = Preferences.userNodeForPackage(Hiero.class); java.awt.Color backgroundColor = EffectUtil.fromString(prefs.get("background", "000000")); @@ -201,25 +206,6 @@ public class Hiero extends JFrame { effectsListModel.addElement(new ShadowEffect()); new EffectPanel(colorEffect); - gamePanel.add(glCanvas = new Canvas() { - private final Dimension minSize = new Dimension(); - - public final void addNotify () { - super.addNotify(); - app = new LwjglApplication(new Renderer(), "Hiero", 200, 200, false, glCanvas); - - addWindowListener(new WindowAdapter() { - public void windowClosed (WindowEvent event) { - app.stop(); - } - }); - } - - public Dimension getMinimumSize () { - return minSize; - } - }); - setVisible(true); } @@ -1308,6 +1294,12 @@ public class Hiero extends JFrame { // break; // } // } - new Hiero(); + SwingUtilities.invokeLater( new Runnable() { + + @Override + public void run() { + new Hiero(); + } + }); } } -- 2.11.0