org.newdawn.slick
Class GameContainer

java.lang.Object
  extended by org.newdawn.slick.GameContainer
All Implemented Interfaces:
GUIContext
Direct Known Subclasses:
AppGameContainer, AppletGameContainer.Container

public abstract class GameContainer
extends java.lang.Object
implements GUIContext

A generic game container that handles the game loop, fps recording and managing the input system

Author:
kevin

Field Summary
protected  boolean alwaysRender
          True if we should render when not focused
protected  boolean clearEachFrame
          True if we should clear the screen each frame
protected  boolean forceExit
          True if we should force exit
protected  int fps
          The current count of FPS
protected  Game game
          The game being managed
protected static SGL GL
          The renderer to use for all GL operations
protected  int height
          The height of the display
protected  Input input
          The input system to pass to the game
protected  long lastFPS
          The last time the FPS recorded
protected  long lastFrame
          The time the last frame was rendered
protected  Game lastGame
          The last game started
protected  long maximumLogicInterval
          The maximum logic update interval
protected  long minimumLogicInterval
          The minimum logic update interval
protected  boolean paused
          True if the game is paused
protected  int recordedFPS
          The last recorded FPS
protected  boolean running
          True if we're currently running the game loop
protected  int samples
          The number of samples we'll attempt through hardware
protected static org.lwjgl.opengl.Drawable SHARED_DRAWABLE
          The shared drawable if any
protected  boolean smoothDeltas
          Smoothed deltas requested
protected static boolean stencil
          True if we require stencil bits
protected  long storedDelta
          The stored delta
protected  boolean supportsMultiSample
          True if this context supports multisample
protected  int targetFPS
          The FPS we want to lock to
protected  boolean vsync
          True if vsync has been requested
protected  int width
          The width of the display
 
Constructor Summary
protected GameContainer(Game game)
          Create a new game container wrapping a given game
 
Method Summary
static void enableSharedContext()
          Enable shared OpenGL context.
static void enableStencil()
           
protected  void enterOrtho()
          Enter the orthographic mode
protected  void enterOrtho(int xsize, int ysize)
          Enter the orthographic mode
 void exit()
          Cause the game to exit and shutdown cleanly
 boolean getAlwaysRender()
          True if this container should render when it has focus
 float getAspectRatio()
          Get the aspect ratio of the screen
static int getBuildVersion()
          Get the build number of slick
 Font getDefaultFont()
          Get the default system font
protected  int getDelta()
          Retrieve the time taken to render the last frame, i.e.
 int getFPS()
          Get the current recorded FPS (frames per second)
 Graphics getGraphics()
          Get the graphics context used by this container.
 int getHeight()
          Get the height of the game canvas
 Input getInput()
          Get the input system
 float getMusicVolume()
          Retrieve the current default volume for music
 int getSamples()
          The number of samples we're attempting to performing using hardware multisampling
abstract  int getScreenHeight()
          Get the height of the standard screen resolution
abstract  int getScreenWidth()
          Get the width of the standard screen resolution
static org.lwjgl.opengl.Drawable getSharedContext()
          Get the context shared by all containers
 float getSoundVolume()
          Retrieve the current default volume for sound fx
 long getTime()
          Get the accurate system time
 int getWidth()
          Get the width of the game canvas
abstract  boolean hasFocus()
          Check if the game currently has focus
protected  void initGL()
          Initialise the GL context
protected  void initSystem()
          Initialise the system components, OpenGL and OpenAL.
 boolean isFullscreen()
          Check if the display is in fullscreen mode
abstract  boolean isMouseGrabbed()
          Check if the mouse cursor is current grabbed.
 boolean isMusicOn()
          Check if music is enabled
 boolean isPaused()
          Check if the container is currently paused.
 boolean isShowingFPS()
          Check if the FPS is currently showing
 boolean isSoundOn()
          Check if sound effects are enabled
 boolean isUpdatingOnlyWhenVisible()
          Check if this game is only updating when visible to the user (default = true)
 boolean isVSyncRequested()
          True if vsync is requested
 void pause()
          Pause the game - i.e.
 void reinit()
          Renitialise the game and the context in which it's being rendered
 void resume()
          Resumt the game - i.e.
protected  boolean running()
          True if the game is running
 void setAlwaysRender(boolean alwaysRender)
          Indicate whether we want this container to render when it has focus
 void setAnimatedMouseCursor(java.lang.String ref, int x, int y, int width, int height, int[] cursorDelays)
          Get a cursor based on a image reference on the classpath.
 void setClearEachFrame(boolean clear)
          Indicate if we should clear the screen at the beginning of each frame.
 void setDefaultFont(Font font)
          Set the default font that will be intialised in the graphics held in this container
abstract  void setDefaultMouseCursor()
          Set the default mouse cursor - i.e.
 void setForceExit(boolean forceExit)
          Indicate if we should force exitting the VM at the end of the game (default = true)
 void setFullscreen(boolean fullscreen)
          Indicate whether we want to be in fullscreen mode.
abstract  void setIcon(java.lang.String ref)
          Set the icon to be displayed if possible in this type of container
abstract  void setIcons(java.lang.String[] refs)
          Set the icons to be used for this application.
 void setMaximumLogicUpdateInterval(int interval)
          Set the maximum amount of time in milliseconds that can passed into the update method.
 void setMinimumLogicUpdateInterval(int interval)
          Set the minimum amount of time in milliseonds that has to pass before update() is called on the container game.
abstract  void setMouseCursor(org.lwjgl.input.Cursor cursor, int hotSpotX, int hotSpotY)
          Set the mouse cursor to be displayed - this is a hardware cursor and hence shouldn't have any impact on FPS.
abstract  void setMouseCursor(ImageData data, int hotSpotX, int hotSpotY)
          Set the mouse cursor to be displayed - this is a hardware cursor and hence shouldn't have any impact on FPS.
abstract  void setMouseCursor(Image image, int hotSpotX, int hotSpotY)
          Set the mouse cursor based on the contents of the image.
abstract  void setMouseCursor(java.lang.String ref, int hotSpotX, int hotSpotY)
          Set the mouse cursor to be displayed - this is a hardware cursor and hence shouldn't have any impact on FPS.
abstract  void setMouseGrabbed(boolean grabbed)
          Indicate whether mouse cursor should be grabbed or not
 void setMultiSample(int samples)
          Indicate whether we want to try to use fullscreen multisampling.
 void setMusicOn(boolean on)
          Indicate whether music should be enabled
 void setMusicVolume(float volume)
          Set the default volume for music
 void setPaused(boolean paused)
          Indicates if the game should be paused, i.e.
 void setShowFPS(boolean show)
          Indicate whether the container should show the FPS
 void setSmoothDeltas(boolean smoothDeltas)
          Indicate if we want to smooth deltas.
 void setSoundOn(boolean on)
          Indicate whether sound effects should be enabled
 void setSoundVolume(float volume)
          Set the default volume for sound fx
 void setTargetFrameRate(int fps)
          Set the target fps we're hoping to get
 void setUpdateOnlyWhenVisible(boolean updateOnlyWhenVisible)
          Indicate if the display should update only when the game is visible (the default is true)
 void setVerbose(boolean verbose)
          Inidcate we want verbose logging
 void setVSync(boolean vsync)
          Indicate whether the display should be synced to the vertical refresh (stops tearing)
 void sleep(int milliseconds)
          Sleep for a given period
 boolean supportsMultiSample()
          Check if this hardware can support multi-sampling
protected  void updateAndRender(int delta)
          Update and render the game
protected  void updateFPS()
          Updated the FPS counter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GL

protected static SGL GL
The renderer to use for all GL operations


SHARED_DRAWABLE

protected static org.lwjgl.opengl.Drawable SHARED_DRAWABLE
The shared drawable if any


lastFrame

protected long lastFrame
The time the last frame was rendered


lastFPS

protected long lastFPS
The last time the FPS recorded


recordedFPS

protected int recordedFPS
The last recorded FPS


fps

protected int fps
The current count of FPS


running

protected boolean running
True if we're currently running the game loop


width

protected int width
The width of the display


height

protected int height
The height of the display


game

protected Game game
The game being managed


input

protected Input input
The input system to pass to the game


targetFPS

protected int targetFPS
The FPS we want to lock to


minimumLogicInterval

protected long minimumLogicInterval
The minimum logic update interval


storedDelta

protected long storedDelta
The stored delta


maximumLogicInterval

protected long maximumLogicInterval
The maximum logic update interval


lastGame

protected Game lastGame
The last game started


clearEachFrame

protected boolean clearEachFrame
True if we should clear the screen each frame


paused

protected boolean paused
True if the game is paused


forceExit

protected boolean forceExit
True if we should force exit


vsync

protected boolean vsync
True if vsync has been requested


smoothDeltas

protected boolean smoothDeltas
Smoothed deltas requested


samples

protected int samples
The number of samples we'll attempt through hardware


supportsMultiSample

protected boolean supportsMultiSample
True if this context supports multisample


alwaysRender

protected boolean alwaysRender
True if we should render when not focused


stencil

protected static boolean stencil
True if we require stencil bits

Constructor Detail

GameContainer

protected GameContainer(Game game)
Create a new game container wrapping a given game

Parameters:
game - The game to be wrapped
Method Detail

enableStencil

public static void enableStencil()

setDefaultFont

public void setDefaultFont(Font font)
Set the default font that will be intialised in the graphics held in this container

Parameters:
font - The font to use as default

setMultiSample

public void setMultiSample(int samples)
Indicate whether we want to try to use fullscreen multisampling. This will give antialiasing across the whole scene using a hardware feature.

Parameters:
samples - The number of samples to attempt (2 is safe)

supportsMultiSample

public boolean supportsMultiSample()
Check if this hardware can support multi-sampling

Returns:
True if the hardware supports multi-sampling

getSamples

public int getSamples()
The number of samples we're attempting to performing using hardware multisampling

Returns:
The number of samples requested

setForceExit

public void setForceExit(boolean forceExit)
Indicate if we should force exitting the VM at the end of the game (default = true)

Parameters:
forceExit - True if we should force the VM exit

setSmoothDeltas

public void setSmoothDeltas(boolean smoothDeltas)
Indicate if we want to smooth deltas. This feature will report a delta based on the FPS not the time passed. This works well with vsync.

Parameters:
smoothDeltas - True if we should report smooth deltas

isFullscreen

public boolean isFullscreen()
Check if the display is in fullscreen mode

Returns:
True if the display is in fullscreen mode

getAspectRatio

public float getAspectRatio()
Get the aspect ratio of the screen

Returns:
The aspect ratio of the display

setFullscreen

public void setFullscreen(boolean fullscreen)
                   throws SlickException
Indicate whether we want to be in fullscreen mode. Note that the current display mode must be valid as a fullscreen mode for this to work

Parameters:
fullscreen - True if we want to be in fullscreen mode
Throws:
SlickException - Indicates we failed to change the display mode

enableSharedContext

public static void enableSharedContext()
                                throws SlickException
Enable shared OpenGL context. After calling this all containers created will shared a single parent context

Throws:
SlickException - Indicates a failure to create the shared drawable

getSharedContext

public static org.lwjgl.opengl.Drawable getSharedContext()
Get the context shared by all containers

Returns:
The context shared by all the containers or null if shared context isn't enabled

setClearEachFrame

public void setClearEachFrame(boolean clear)
Indicate if we should clear the screen at the beginning of each frame. If you're rendering to the whole screen each frame then setting this to false can give some performance improvements

Parameters:
clear - True if the the screen should be cleared each frame

reinit

public void reinit()
            throws SlickException
Renitialise the game and the context in which it's being rendered

Throws:
SlickException - Indicates a failure rerun initialisation routines

pause

public void pause()
Pause the game - i.e. suspend updates


resume

public void resume()
Resumt the game - i.e. continue updates


isPaused

public boolean isPaused()
Check if the container is currently paused.

Returns:
True if the container is paused

setPaused

public void setPaused(boolean paused)
Indicates if the game should be paused, i.e. if updates should be propogated through to the game.

Parameters:
paused - True if the game should be paused

getAlwaysRender

public boolean getAlwaysRender()
True if this container should render when it has focus

Returns:
True if this container should render when it has focus

setAlwaysRender

public void setAlwaysRender(boolean alwaysRender)
Indicate whether we want this container to render when it has focus

Parameters:
alwaysRender - True if this container should render when it has focus

getBuildVersion

public static int getBuildVersion()
Get the build number of slick

Returns:
The build number of slick

getDefaultFont

public Font getDefaultFont()
Get the default system font

Specified by:
getDefaultFont in interface GUIContext
Returns:
The default system font

isSoundOn

public boolean isSoundOn()
Check if sound effects are enabled

Returns:
True if sound effects are enabled

isMusicOn

public boolean isMusicOn()
Check if music is enabled

Returns:
True if music is enabled

setMusicOn

public void setMusicOn(boolean on)
Indicate whether music should be enabled

Parameters:
on - True if music should be enabled

setSoundOn

public void setSoundOn(boolean on)
Indicate whether sound effects should be enabled

Parameters:
on - True if sound effects should be enabled

getMusicVolume

public float getMusicVolume()
Retrieve the current default volume for music

Returns:
the current default volume for music

getSoundVolume

public float getSoundVolume()
Retrieve the current default volume for sound fx

Returns:
the current default volume for sound fx

setSoundVolume

public void setSoundVolume(float volume)
Set the default volume for sound fx

Parameters:
volume - the new default value for sound fx volume

setMusicVolume

public void setMusicVolume(float volume)
Set the default volume for music

Parameters:
volume - the new default value for music volume

getScreenWidth

public abstract int getScreenWidth()
Get the width of the standard screen resolution

Specified by:
getScreenWidth in interface GUIContext
Returns:
The screen width

getScreenHeight

public abstract int getScreenHeight()
Get the height of the standard screen resolution

Specified by:
getScreenHeight in interface GUIContext
Returns:
The screen height

getWidth

public int getWidth()
Get the width of the game canvas

Specified by:
getWidth in interface GUIContext
Returns:
The width of the game canvas

getHeight

public int getHeight()
Get the height of the game canvas

Specified by:
getHeight in interface GUIContext
Returns:
The height of the game canvas

setIcon

public abstract void setIcon(java.lang.String ref)
                      throws SlickException
Set the icon to be displayed if possible in this type of container

Parameters:
ref - The reference to the icon to be displayed
Throws:
SlickException - Indicates a failure to load the icon

setIcons

public abstract void setIcons(java.lang.String[] refs)
                       throws SlickException
Set the icons to be used for this application. Note that the size of the icon defines how it will be used. Important ones to note Windows window icon must be 16x16 Windows alt-tab icon must be 24x24 or 32x32 depending on Windows version (XP=32)

Parameters:
refs - The reference to the icon to be displayed
Throws:
SlickException - Indicates a failure to load the icon

getTime

public long getTime()
Get the accurate system time

Specified by:
getTime in interface GUIContext
Returns:
The system time in milliseconds

sleep

public void sleep(int milliseconds)
Sleep for a given period

Parameters:
milliseconds - The period to sleep for in milliseconds

setMouseCursor

public abstract void setMouseCursor(java.lang.String ref,
                                    int hotSpotX,
                                    int hotSpotY)
                             throws SlickException
Set the mouse cursor to be displayed - this is a hardware cursor and hence shouldn't have any impact on FPS.

Specified by:
setMouseCursor in interface GUIContext
Parameters:
ref - The location of the image to be loaded for the cursor
hotSpotX - The x coordinate of the hotspot within the cursor image
hotSpotY - The y coordinate of the hotspot within the cursor image
Throws:
SlickException - Indicates a failure to load the cursor image or create the hardware cursor

setMouseCursor

public abstract void setMouseCursor(ImageData data,
                                    int hotSpotX,
                                    int hotSpotY)
                             throws SlickException
Set the mouse cursor to be displayed - this is a hardware cursor and hence shouldn't have any impact on FPS.

Specified by:
setMouseCursor in interface GUIContext
Parameters:
data - The image data from which the cursor can be construted
hotSpotX - The x coordinate of the hotspot within the cursor image
hotSpotY - The y coordinate of the hotspot within the cursor image
Throws:
SlickException - Indicates a failure to load the cursor image or create the hardware cursor

setMouseCursor

public abstract void setMouseCursor(Image image,
                                    int hotSpotX,
                                    int hotSpotY)
                             throws SlickException
Set the mouse cursor based on the contents of the image. Note that this will not take account of render state type changes to images (rotation and such). If these effects are required it is recommended that an offscreen buffer be used to produce an appropriate image. An offscreen buffer will always be used to produce the new cursor and as such this operation an be very expensive

Parameters:
image - The image to use as the cursor
hotSpotX - The x coordinate of the hotspot within the cursor image
hotSpotY - The y coordinate of the hotspot within the cursor image
Throws:
SlickException - Indicates a failure to load the cursor image or create the hardware cursor

setMouseCursor

public abstract void setMouseCursor(org.lwjgl.input.Cursor cursor,
                                    int hotSpotX,
                                    int hotSpotY)
                             throws SlickException
Set the mouse cursor to be displayed - this is a hardware cursor and hence shouldn't have any impact on FPS.

Specified by:
setMouseCursor in interface GUIContext
Parameters:
cursor - The cursor to use
hotSpotX - The x coordinate of the hotspot within the cursor image
hotSpotY - The y coordinate of the hotspot within the cursor image
Throws:
SlickException - Indicates a failure to load the cursor image or create the hardware cursor

setAnimatedMouseCursor

public void setAnimatedMouseCursor(java.lang.String ref,
                                   int x,
                                   int y,
                                   int width,
                                   int height,
                                   int[] cursorDelays)
                            throws SlickException
Get a cursor based on a image reference on the classpath. The image is assumed to be a set/strip of cursor animation frames running from top to bottom.

Parameters:
ref - The reference to the image to be loaded
x - The x-coordinate of the cursor hotspot (left -> right)
y - The y-coordinate of the cursor hotspot (bottom -> top)
width - The x width of the cursor
height - The y height of the cursor
cursorDelays - image delays between changing frames in animation
Throws:
SlickException - Indicates a failure to load the image or a failure to create the hardware cursor

setDefaultMouseCursor

public abstract void setDefaultMouseCursor()
Set the default mouse cursor - i.e. the original cursor before any native cursor was set

Specified by:
setDefaultMouseCursor in interface GUIContext

getInput

public Input getInput()
Get the input system

Specified by:
getInput in interface GUIContext
Returns:
The input system available to this game container

getFPS

public int getFPS()
Get the current recorded FPS (frames per second)

Returns:
The current FPS

setMouseGrabbed

public abstract void setMouseGrabbed(boolean grabbed)
Indicate whether mouse cursor should be grabbed or not

Parameters:
grabbed - True if mouse cursor should be grabbed

isMouseGrabbed

public abstract boolean isMouseGrabbed()
Check if the mouse cursor is current grabbed. This will cause it not to be seen.

Returns:
True if the mouse is currently grabbed

getDelta

protected int getDelta()
Retrieve the time taken to render the last frame, i.e. the change in time - delta.

Returns:
The time taken to render the last frame

updateFPS

protected void updateFPS()
Updated the FPS counter


setMinimumLogicUpdateInterval

public void setMinimumLogicUpdateInterval(int interval)
Set the minimum amount of time in milliseonds that has to pass before update() is called on the container game. This gives a way to limit logic updates compared to renders.

Parameters:
interval - The minimum interval between logic updates

setMaximumLogicUpdateInterval

public void setMaximumLogicUpdateInterval(int interval)
Set the maximum amount of time in milliseconds that can passed into the update method. Useful for collision detection without sweeping.

Parameters:
interval - The maximum interval between logic updates

updateAndRender

protected void updateAndRender(int delta)
                        throws SlickException
Update and render the game

Parameters:
delta - The change in time since last update and render
Throws:
SlickException - Indicates an internal fault to the game.

setUpdateOnlyWhenVisible

public void setUpdateOnlyWhenVisible(boolean updateOnlyWhenVisible)
Indicate if the display should update only when the game is visible (the default is true)

Parameters:
updateOnlyWhenVisible - True if we should updated only when the display is visible

isUpdatingOnlyWhenVisible

public boolean isUpdatingOnlyWhenVisible()
Check if this game is only updating when visible to the user (default = true)

Returns:
True if the game is only updated when the display is visible

initGL

protected void initGL()
Initialise the GL context


initSystem

protected void initSystem()
                   throws SlickException
Initialise the system components, OpenGL and OpenAL.

Throws:
SlickException - Indicates a failure to create a native handler

enterOrtho

protected void enterOrtho()
Enter the orthographic mode


setShowFPS

public void setShowFPS(boolean show)
Indicate whether the container should show the FPS

Parameters:
show - True if the container should show the FPS

isShowingFPS

public boolean isShowingFPS()
Check if the FPS is currently showing

Returns:
True if the FPS is showing

setTargetFrameRate

public void setTargetFrameRate(int fps)
Set the target fps we're hoping to get

Parameters:
fps - The target fps we're hoping to get

setVSync

public void setVSync(boolean vsync)
Indicate whether the display should be synced to the vertical refresh (stops tearing)

Parameters:
vsync - True if we want to sync to vertical refresh

isVSyncRequested

public boolean isVSyncRequested()
True if vsync is requested

Returns:
True if vsync is requested

running

protected boolean running()
True if the game is running

Returns:
True if the game is running

setVerbose

public void setVerbose(boolean verbose)
Inidcate we want verbose logging

Parameters:
verbose - True if we want verbose logging (INFO and DEBUG)

exit

public void exit()
Cause the game to exit and shutdown cleanly


hasFocus

public abstract boolean hasFocus()
Check if the game currently has focus

Returns:
True if the game currently has focus

getGraphics

public Graphics getGraphics()
Get the graphics context used by this container. Note that this value may vary over the life time of the game.

Returns:
The graphics context used by this container

enterOrtho

protected void enterOrtho(int xsize,
                          int ysize)
Enter the orthographic mode

Parameters:
xsize - The size of the panel being used
ysize - The size of the panel being used


Copyright © 2006 New Dawn Software. All Rights Reserved.