Example on how to allow Space Invader Test Application to work as an applet with minimal code change.

This commit is contained in:
kappa1 2010-04-08 22:53:45 +00:00
parent b42040537c
commit 72c3da7aeb
2 changed files with 122 additions and 10 deletions

View File

@ -167,6 +167,9 @@ public class Game {
/** 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 class Game {
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 @@ public class Game {
// update window contents
Display.update();
}
// clean up
soundManager.destroy();
Display.destroy();
}
/**
@ -542,7 +551,7 @@ public class Game {
}
// 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 @@ public class Game {
* @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 @@ public class Game {
/**
*
*/
private void execute() {
public void execute() {
gameLoop();
soundManager.destroy();
Display.destroy();
}
/**

View File

@ -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");
}
}
}