org.jrabbit.standard.game.world.background
Class Background

java.lang.Object
  extended by org.jrabbit.standard.game.world.background.Background
All Implemented Interfaces:
Renderable, Updateable, DynamicSkinned, Skinned, Colored, Dimensioned

public class Background
extends java.lang.Object
implements Updateable, Renderable, Colored, DynamicSkinned

Just as 3D games tend to have skyboxes, jRabbit's 2D Worlds have Backgrounds. Before a world renders its layers, its background is rendered over the entire scene to clear the display of last frame's view. This background can either be a solid color (the default) or it can be any Skin. Backgrounds are fairly straightforward, but there are a few useful tips for using them: 1: If a Background is using a Skin, the Skin should most likely render a complete, opaque rectangle (it should fill its entire dimension when rendered). Otherwise, sections of the scene will not be covered by the background, and old frame data will bleed through. So, if an Image is being used to render the background, it should not have any transparent regions. 2: The fact that old frame data persists can actually be used to create a simple "motion blur" effect. If the background is rendered with an alpha value less than 1, the old frame will not completely be wiped out, and the viewer will see old objects fade out over time. 3: If a Background is supplied with a Skin that is Updateable (like an AnimationSkin) it will update it over time, as desired. However, the dimensions of the AnimationSkin should not change. So, a fiery, burning background with flickering flames (or something similar) is fully supported, as long as all the Images used to render the scene have the same dimensions.

Author:
Chris Molini

Field Summary
protected  Color color
          The Color used to render the Background.
protected  boolean keepAspect
          Whether the Skin (if any) should be rendered at its original aspect ratio or it it should be squashed into the smallest dimensions possible.
protected  boolean rotateWithCamera
          Whether or not the Background should be rotated as the active Camera is rotated.
protected  float scaleX
          The scale of the x-axis at which to render the current Skin (if any).
protected  float scaleY
          The scale of the y-axis at which to render the current Skin (if any).
protected  float sceneHeight
          The height of the scene being rendered.
protected  float sceneWidth
          The width of the scene being rendered.
protected  Skin skin
          The Skin being used to render the Background.
 
Constructor Summary
Background()
          Creates a default Background - solid black, non-skinned.
Background(Color color)
          Creates a Background that uses the indicated Color.
Background(float red, float green, float blue)
          Creates a Background that uses the indicated RGB values.
Background(float red, float green, float blue, float alpha)
          Creates a Background that uses the indicated RGBA values.
Background(Skin skin)
          Creates a Background that renders the supplied Skin.
Background(java.lang.String imageReference)
          Creates a Background that renders the referenced Image.
 
Method Summary
 void clear()
          Sets the Skin reference to null and makes the Background simply render solid black.
 void clear(Color color)
          Sets the Skin reference to null and makes the Background simply render the indicated Color.
 Color color()
          Accesses the current Color.
 float height()
          Accesses the dimensions of the Background.
 void keepAspect(boolean keepAspect)
          Sets whether or not the Background should keep the aspect ratio of the Skins it uses to render.
 void render()
          Renders the Background.
private  void rescale()
          This method recalculates the scaling of the Background.
 void rotateWithCamera(boolean rotate)
          Sets whether or not the Background should rotate with the active camera as when renders.
 void setSkin(Skin skin)
          Redefines the current Skin.
 void setSkin(java.lang.String imageReference)
          Redefines the current Skin to render the Image with the indicated reference.
 Skin skin()
          Accesses the active Skin.
 void update(int delta)
          If the current Skin is both not null and is Updateable, this updates the Skin.
 float width()
          Accesses the dimensions of the Background.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sceneWidth

protected float sceneWidth
The width of the scene being rendered.


sceneHeight

protected float sceneHeight
The height of the scene being rendered.


scaleX

protected float scaleX
The scale of the x-axis at which to render the current Skin (if any).


scaleY

protected float scaleY
The scale of the y-axis at which to render the current Skin (if any).


keepAspect

protected boolean keepAspect
Whether the Skin (if any) should be rendered at its original aspect ratio or it it should be squashed into the smallest dimensions possible. By default, this is true.


rotateWithCamera

protected boolean rotateWithCamera
Whether or not the Background should be rotated as the active Camera is rotated. By default this is true.


skin

protected Skin skin
The Skin being used to render the Background. If this is null, then a solid color is used for the background.


color

protected Color color
The Color used to render the Background.

Constructor Detail

Background

public Background()
Creates a default Background - solid black, non-skinned.


Background

public Background(java.lang.String imageReference)
Creates a Background that renders the referenced Image.

Parameters:
imageReference - The identifier of the Image to render.

Background

public Background(Skin skin)
Creates a Background that renders the supplied Skin.

Parameters:
skin - The Skin to use in rendering the background.

Background

public Background(float red,
                  float green,
                  float blue)
Creates a Background that uses the indicated RGB values.

Parameters:
red - The red value of the desired color.
green - The green value of the desired color.
blue - The blue value of the desired color.

Background

public Background(float red,
                  float green,
                  float blue,
                  float alpha)
Creates a Background that uses the indicated RGBA values.

Parameters:
red - The red value of the desired color.
green - The green value of the desired color.
blue - The blue value of the desired color.
alpha - The alpha value of the desired color.

Background

public Background(Color color)
Creates a Background that uses the indicated Color.

Parameters:
color - The Color with the desired RGBA settings.
Method Detail

width

public float width()
Accesses the dimensions of the Background.

Specified by:
width in interface Dimensioned
Returns:
The width of the Background.

height

public float height()
Accesses the dimensions of the Background.

Specified by:
height in interface Dimensioned
Returns:
The height of the Background.

color

public Color color()
Accesses the current Color.

Specified by:
color in interface Colored
Returns:
The Color being used to render the Background.

skin

public Skin skin()
Accesses the active Skin.

Specified by:
skin in interface Skinned
Returns:
The Skin being used to render the Background.

setSkin

public void setSkin(java.lang.String imageReference)
Redefines the current Skin to render the Image with the indicated reference.

Parameters:
imageReference - The indicator of the Image in the Cache to use.

setSkin

public void setSkin(Skin skin)
Redefines the current Skin.

Specified by:
setSkin in interface DynamicSkinned
Parameters:
skin - The new Skin to use for rendering.

clear

public void clear()
Sets the Skin reference to null and makes the Background simply render solid black.


clear

public void clear(Color color)
Sets the Skin reference to null and makes the Background simply render the indicated Color.


rotateWithCamera

public void rotateWithCamera(boolean rotate)
Sets whether or not the Background should rotate with the active camera as when renders. Keeping this enabled will make the scene more believable if the camera rotates. By default, background rotation is enabled.

Parameters:
rotate - Whether or not rotation should be allowed.

keepAspect

public void keepAspect(boolean keepAspect)
Sets whether or not the Background should keep the aspect ratio of the Skins it uses to render. By default, aspect maintenance is enabled.

Parameters:
keepAspect - True if the Skin's aspect ratio should be preserved, false if all rendered Skins should be squashed into the smallest dimensions possible.

rescale

private void rescale()
This method recalculates the scaling of the Background.


update

public void update(int delta)
If the current Skin is both not null and is Updateable, this updates the Skin.

Specified by:
update in interface Updateable
Parameters:
delta - The number of microseconds that has passed.

render

public void render()
Renders the Background. Calling this also resets the OpenGL Modelview Matrix and sets the bound color to white.

Specified by:
render in interface Renderable