Path: blob/master/SLICK_HOME/src/org/newdawn/slick/CanvasGameContainer.java
1456 views
package org.newdawn.slick;12import java.awt.Canvas;34import javax.swing.SwingUtilities;56import org.lwjgl.LWJGLException;7import org.lwjgl.opengl.Display;8import org.newdawn.slick.util.Log;910/**11* A game container that displays the game on an AWT Canvas.12*13* @author kevin14*/15public class CanvasGameContainer extends Canvas {16/** The actual container implementation */17protected Container container;18/** The game being held in this container */19protected Game game;2021/**22* Create a new panel23*24* @param game The game being held25* @throws SlickException Indicates a failure during creation of the container26*/27public CanvasGameContainer(Game game) throws SlickException {28this(game, false);29}3031/**32* Create a new panel33*34* @param game The game being held35* @param shared True if shared GL context should be enabled. This allows multiple panels36* to share textures and other GL resources.37* @throws SlickException Indicates a failure during creation of the container38*/39public CanvasGameContainer(Game game, boolean shared) throws SlickException {40super();4142this.game = game;43setIgnoreRepaint(true);44requestFocus();45setSize(500,500);4647container = new Container(game, shared);48container.setForceExit(false);49}5051/**52* Start the game container rendering53*54* @throws SlickException Indicates a failure during game execution55*/56public void start() throws SlickException {57SwingUtilities.invokeLater(new Runnable() {58public void run() {59try {60Input.disableControllers();6162try {63Display.setParent(CanvasGameContainer.this);64} catch (LWJGLException e) {65throw new SlickException("Failed to setParent of canvas", e);66}6768container.setup();69scheduleUpdate();70} catch (SlickException e) {71e.printStackTrace();72System.exit(0);73}74}75});76}7778/**79* Schedule an update on the EDT80*/81private void scheduleUpdate() {82if (!isVisible()) {83return;84}8586SwingUtilities.invokeLater(new Runnable() {87public void run() {88try {89container.gameLoop();90} catch (SlickException e) {91e.printStackTrace();92}93container.checkDimensions();94scheduleUpdate();95}96});97}98/**99* Dispose the container and any resources it holds100*/101public void dispose() {102}103104/**105* Get the GameContainer providing this canvas106*107* @return The game container providing this canvas108*/109public GameContainer getContainer() {110return container;111}112113/**114* A game container to provide the canvas context115*116* @author kevin117*/118private class Container extends AppGameContainer {119/**120* Create a new container wrapped round the game121*122* @param game123* The game to be held in this container124* @param shared True if shared GL context should be enabled. This allows multiple panels125* to share textures and other GL resources.126* @throws SlickException Indicates a failure to initialise127*/128public Container(Game game, boolean shared) throws SlickException {129super(game, CanvasGameContainer.this.getWidth(), CanvasGameContainer.this.getHeight(), false);130131width = CanvasGameContainer.this.getWidth();132height = CanvasGameContainer.this.getHeight();133134if (shared) {135enableSharedContext();136}137}138139/**140* Updated the FPS counter141*/142protected void updateFPS() {143super.updateFPS();144}145146/**147* @see org.newdawn.slick.GameContainer#running()148*/149protected boolean running() {150return super.running() && CanvasGameContainer.this.isDisplayable();151}152153/**154* @see org.newdawn.slick.GameContainer#getHeight()155*/156public int getHeight() {157return CanvasGameContainer.this.getHeight();158}159160/**161* @see org.newdawn.slick.GameContainer#getWidth()162*/163public int getWidth() {164return CanvasGameContainer.this.getWidth();165}166167/**168* Check the dimensions of the canvas match the display169*/170public void checkDimensions() {171if ((width != CanvasGameContainer.this.getWidth()) ||172(height != CanvasGameContainer.this.getHeight())) {173174try {175setDisplayMode(CanvasGameContainer.this.getWidth(),176CanvasGameContainer.this.getHeight(), false);177} catch (SlickException e) {178Log.error(e);179}180}181}182}183}184185186