|
From: <ka...@us...> - 2010-04-08 22:53:51
|
Revision: 3315
http://java-game-lib.svn.sourceforge.net/java-game-lib/?rev=3315&view=rev
Author: kappa1
Date: 2010-04-08 22:53:45 +0000 (Thu, 08 Apr 2010)
Log Message:
-----------
Example on how to allow Space Invader Test Application to work as an applet with minimal code change.
Modified Paths:
--------------
trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/Game.java
Added Paths:
-----------
trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/GameApplet.java
Modified: trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/Game.java
===================================================================
--- trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/Game.java 2010-04-07 20:06:59 UTC (rev 3314)
+++ trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/Game.java 2010-04-08 22:53:45 UTC (rev 3315)
@@ -167,6 +167,9 @@
/** Mouse movement on x axis */
private int mouseX;
+
+ /** Is this an application or applet */
+ private static boolean isApplication = false;
/**
* Construct our game and set it running.
@@ -210,13 +213,15 @@
public void initialize() {
// initialize the window beforehand
try {
- setDisplayMode();
- Display.setTitle(WINDOW_TITLE);
- Display.setFullscreen(fullscreen);
- Display.create();
-
+ setDisplayMode();
+ Display.setTitle(WINDOW_TITLE);
+ Display.setFullscreen(fullscreen);
+ Display.create();
+
// grab the mouse, dont want that hideous cursor when we're playing!
- Mouse.setGrabbed(true);
+ if (isApplication) {
+ Mouse.setGrabbed(true);
+ }
// enable textures since we're going to use these for our sprites
GL11.glEnable(GL11.GL_TEXTURE_2D);
@@ -426,6 +431,10 @@
// update window contents
Display.update();
}
+
+ // clean up
+ soundManager.destroy();
+ Display.destroy();
}
/**
@@ -542,7 +551,7 @@
}
// if escape has been pressed, stop the game
- if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
+ if ((Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) && isApplication) {
Game.gameRunning = false;
}
}
@@ -579,6 +588,7 @@
* @param argv The arguments that are passed into our game
*/
public static void main(String argv[]) {
+ isApplication = true;
System.out.println("Use -fullscreen for fullscreen mode");
new Game((argv.length > 0 && argv[0].equalsIgnoreCase("-fullscreen"))).execute();
System.exit(0);
@@ -587,10 +597,8 @@
/**
*
*/
- private void execute() {
+ public void execute() {
gameLoop();
- soundManager.destroy();
- Display.destroy();
}
/**
Added: trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/GameApplet.java
===================================================================
--- trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/GameApplet.java (rev 0)
+++ trunk/LWJGL/src/java/org/lwjgl/examples/spaceinvaders/GameApplet.java 2010-04-08 22:53:45 UTC (rev 3315)
@@ -0,0 +1,104 @@
+package org.lwjgl.examples.spaceinvaders;
+
+
+import java.applet.Applet;
+import java.awt.BorderLayout;
+import java.awt.Canvas;
+import org.lwjgl.LWJGLException;
+import org.lwjgl.opengl.Display;
+
+public class GameApplet extends Applet {
+
+ /** The Canvas where the LWJGL Display is added */
+ Canvas display_parent;
+
+ /** Thread which runs the main game loop */
+ Thread gameThread;
+
+ /** The Game instance */
+ Game game;
+
+ /**
+ * Once the Canvas is created its add notify method will call this method to
+ * start the LWJGL Display and game loop in another thread.
+ */
+ public void startLWJGL() {
+ gameThread = new Thread() {
+ public void run() {
+
+ try {
+ Display.setParent(display_parent);
+
+ } catch (LWJGLException e) {
+ e.printStackTrace();
+ }
+ // start game
+ game = new Game(false);
+ game.execute();
+ }
+ };
+ gameThread.start();
+ }
+
+
+ /**
+ * Tell game loop to stop running, after which the LWJGL Display will be destoryed.
+ * The main thread will wait for the Display.destroy() to complete
+ */
+ private void stopLWJGL() {
+ Game.gameRunning = false;
+ try {
+ gameThread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void start() {
+
+ }
+
+ public void stop() {
+
+ }
+
+ /**
+ * Applet Destroy method will remove the canvas, before canvas is destroyed it will notify
+ * stopLWJGL() to stop main game loop and to destroy the Display
+ */
+ public void destroy() {
+ remove(display_parent);
+ super.destroy();
+ System.out.println("Clear up");
+ }
+
+ /**
+ * initialise applet by adding a canvas to it, this canvas will start the LWJGL Display and game loop
+ * in another thread. It will also stop the game loop and destroy the display on canvas removal when
+ * applet is destroyed.
+ */
+ public void init() {
+ setLayout(new BorderLayout());
+ try {
+ display_parent = new Canvas() {
+ public final void addNotify() {
+ super.addNotify();
+ startLWJGL();
+ }
+ public final void removeNotify() {
+ stopLWJGL();
+ super.removeNotify();
+ }
+ };
+ display_parent.setSize(getWidth(),getHeight());
+ add(display_parent);
+ display_parent.setFocusable(true);
+ display_parent.requestFocus();
+ display_parent.setIgnoreRepaint(true);
+ setVisible(true);
+ } catch (Exception e) {
+ System.err.println(e);
+ throw new RuntimeException("Unable to create display");
+ }
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|