de.matthiasmann.twl.renderer.lwjgl
Class LWJGLRenderer

java.lang.Object
  extended by de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer
All Implemented Interfaces:
LineRenderer, Renderer

public class LWJGLRenderer
extends java.lang.Object
implements Renderer, LineRenderer

A renderer using only GL11 features.

For correct rendering the OpenGL viewport size must be synchronized.

See Also:
syncViewportSize()

Field Summary
protected  Rect clipRectTemp
           
protected  ClipStack clipStack
           
static FontParameter.Parameter<java.lang.Integer> FONTPARAM_OFFSET_X
           
static FontParameter.Parameter<java.lang.Integer> FONTPARAM_OFFSET_Y
           
static FontParameter.Parameter<java.lang.Integer> FONTPARAM_UNDERLINE_OFFSET
           
static AnimationState.StateKey STATE_LEFT_MOUSE_BUTTON
           
static AnimationState.StateKey STATE_MIDDLE_MOUSE_BUTTON
           
static AnimationState.StateKey STATE_RIGHT_MOUSE_BUTTON
           
protected  TintStack tintStack
           
 
Constructor Summary
LWJGLRenderer()
           
 
Method Summary
 void clipEnter(int x, int y, int w, int h)
          Enters a clip region.
 void clipEnter(Rect rect)
          Enters a clip region.
 boolean clipIsEmpty()
          Checks if the active clip region is empty (nothing will render).
 void clipLeave()
          Leaves a clip region creeated by #clipEnter
 DynamicImage createDynamicImage(int width, int height)
          Creates a dynamic image with undefined content.
 Image createGradient(Gradient gradient)
           
 CacheContext createNewCacheContext()
          Creates a new cache context.
 void drawLine(float[] pts, int numPts, float width, Color color, boolean drawAsLoop)
          Draws a line with the specified width and color.
 void endRendering()
          Clean up after rendering TWL.
 CacheContext getActiveCacheContext()
          Returns the active cache context.
 boolean getClipRect(Rect rect)
          Retrieves the active clip region from the top of the stack
 FontMapper getFontMapper()
          Returns the font mapper object if one is available.
 int getHeight()
          Returns the height of the renderable surface
 Input getInput()
          Creates a new instance of LWJGLInput
 LineRenderer getLineRenderer()
          Returns the line renderer.
 OffscreenRenderer getOffscreenRenderer()
          Returns the offscreen renderer.
 long getTimeMillis()
          Returns the elapsed time in milliseconds.
protected  void getTintedColor(Color color, float[] result)
           
protected  void getTintedColor(float[] color, float[] result)
          Computes the tinted color from the given color.
 int getViewportX()
          Retrieves the X position of the OpenGL viewport (index 0 of GL_VIEWPORT)
 int getViewportY()
          Retrieves the Y position of the OpenGL viewport (index 1 of GL_VIEWPORT)
 int getWidth()
          Returns the width of the renderable surface
protected  boolean isMouseInsideWindow()
           
 boolean isUseQuadsForLines()
           
 boolean isUseSWMouseCursors()
           
 LWJGLTexture load(java.net.URL textureUrl, LWJGLTexture.Format fmt, LWJGLTexture.Filter filter)
           
 LWJGLTexture load(java.net.URL textureUrl, LWJGLTexture.Format fmt, LWJGLTexture.Filter filter, TexturePostProcessing tpp)
           
 Font loadFont(java.net.URL url, StateSelect select, FontParameter... parameterList)
          Loads a font.
 Texture loadTexture(java.net.URL url, java.lang.String formatStr, java.lang.String filterStr)
          Loads a texture.
 void pauseRendering()
          Call to revert the GL state to the state before calling startRendering().
 void popGlobalTintColor()
           
protected  void prepareForRendering()
           
 void pushGlobalTintColor(float r, float g, float b, float a)
          Pushes a new tint color on the tint stack.
 void pushGlobalTintColorReset()
          Pushes a white entry on the tint stack which ignores the previous tint color.
protected  void renderSWCursor()
           
 void resumeRendering()
          Resume rendering after a call to pauseRendering().
protected  void revertGLState()
           
 void setActiveCacheContext(CacheContext cc)
          Sets the active cache context.
protected  void setClipRect()
           
 void setColor(Color color)
          Calls GL11.glColor4f() with the specified color multiplied by the current global tint color.
 void setCursor(MouseCursor cursor)
           
 void setFontMapper(FontMapper fontMapper)
          Installs a font mapper.
 void setMouseButton(int button, boolean state)
          Sets the mouse button state for SW mouse cursor rendering
 void setMousePosition(int mouseX, int mouseY)
          Sets the mouse position for SW mouse cursor rendering
protected  void setNativeCursor(org.lwjgl.input.Cursor cursor)
           
protected  void setupGLState()
           
 void setUseQuadsForLines(boolean useQuadsForLines)
           
 void setUseSWMouseCursors(boolean useSWMouseCursors)
          Controls if the mouse cursor is rendered via SW or HW cursors.
 void setViewport(int x, int y, int width, int height)
          Sets the viewport size & position.
 boolean startRendering()
          Setup GL to start rendering the GUI.
 void syncViewportSize()
          Queries the current view port size & position and updates all related internal state.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_LEFT_MOUSE_BUTTON

public static final AnimationState.StateKey STATE_LEFT_MOUSE_BUTTON

STATE_MIDDLE_MOUSE_BUTTON

public static final AnimationState.StateKey STATE_MIDDLE_MOUSE_BUTTON

STATE_RIGHT_MOUSE_BUTTON

public static final AnimationState.StateKey STATE_RIGHT_MOUSE_BUTTON

FONTPARAM_OFFSET_X

public static final FontParameter.Parameter<java.lang.Integer> FONTPARAM_OFFSET_X

FONTPARAM_OFFSET_Y

public static final FontParameter.Parameter<java.lang.Integer> FONTPARAM_OFFSET_Y

FONTPARAM_UNDERLINE_OFFSET

public static final FontParameter.Parameter<java.lang.Integer> FONTPARAM_UNDERLINE_OFFSET

tintStack

protected TintStack tintStack

clipStack

protected final ClipStack clipStack

clipRectTemp

protected final Rect clipRectTemp
Constructor Detail

LWJGLRenderer

public LWJGLRenderer()
              throws org.lwjgl.LWJGLException
Throws:
org.lwjgl.LWJGLException
Method Detail

isUseQuadsForLines

public boolean isUseQuadsForLines()

setUseQuadsForLines

public void setUseQuadsForLines(boolean useQuadsForLines)

isUseSWMouseCursors

public boolean isUseSWMouseCursors()

setUseSWMouseCursors

public void setUseSWMouseCursors(boolean useSWMouseCursors)
Controls if the mouse cursor is rendered via SW or HW cursors. HW cursors have reduced support for transparency and cursor size. This must be set before loading a theme !

Parameters:
useSWMouseCursors -

createNewCacheContext

public CacheContext createNewCacheContext()
Description copied from interface: Renderer
Creates a new cache context. Call setActiveCacheContext to activate it.

Specified by:
createNewCacheContext in interface Renderer
Returns:
a new CacheContext
See Also:
Renderer.setActiveCacheContext(de.matthiasmann.twl.renderer.CacheContext)

getActiveCacheContext

public CacheContext getActiveCacheContext()
Description copied from interface: Renderer
Returns the active cache context. If no valid cache context is active then a new one is created and activated.

Specified by:
getActiveCacheContext in interface Renderer
Returns:
the active CacheContext object

setActiveCacheContext

public void setActiveCacheContext(CacheContext cc)
                           throws java.lang.IllegalStateException
Description copied from interface: Renderer
Sets the active cache context. It will be used for all future load operations.

Specified by:
setActiveCacheContext in interface Renderer
Parameters:
cc - The CacheContext object
Throws:
java.lang.IllegalStateException - when the CacheContext object is invalid

syncViewportSize

public void syncViewportSize()

Queries the current view port size & position and updates all related internal state.

It is important that the internal state matches the OpenGL viewport or clipping won't work correctly.

This method should only be called when the viewport size has changed. It can have negative impact on performance to call every frame.

See Also:
getWidth(), getHeight()

setViewport

public void setViewport(int x,
                        int y,
                        int width,
                        int height)
Sets the viewport size & position.

This method is preferred over syncViewportSize() as it avoids calling GL11.glGetInteger(int, java.nio.IntBuffer).

Parameters:
x - the X position (GL_VIEWPORT index 0)
y - the Y position (GL_VIEWPORT index 1)
width - the width (GL_VIEWPORT index 2)
height - the height (GL_VIEWPORT index 3)

getTimeMillis

public long getTimeMillis()
Description copied from interface: Renderer
Returns the elapsed time in milliseconds.

Specified by:
getTimeMillis in interface Renderer
Returns:
the elapsed time in milliseconds.

getInput

public Input getInput()
Creates a new instance of LWJGLInput

Specified by:
getInput in interface Renderer
Returns:
a new instance of LWJGLInput

setupGLState

protected void setupGLState()

revertGLState

protected void revertGLState()

startRendering

public boolean startRendering()
Setup GL to start rendering the GUI. It assumes default GL state.

Specified by:
startRendering in interface Renderer
Returns:
true if rendering was started, false otherwise

endRendering

public void endRendering()
Description copied from interface: Renderer
Clean up after rendering TWL. Only call this method when Renderer.startRendering() returned true

Specified by:
endRendering in interface Renderer

pauseRendering

public void pauseRendering()
Call to revert the GL state to the state before calling startRendering().

See Also:
resumeRendering()

resumeRendering

public void resumeRendering()
Resume rendering after a call to pauseRendering().


getHeight

public int getHeight()
Description copied from interface: Renderer
Returns the height of the renderable surface

Specified by:
getHeight in interface Renderer
Returns:
the height of the renderable surface

getWidth

public int getWidth()
Description copied from interface: Renderer
Returns the width of the renderable surface

Specified by:
getWidth in interface Renderer
Returns:
the width of the renderable surface

getViewportX

public int getViewportX()
Retrieves the X position of the OpenGL viewport (index 0 of GL_VIEWPORT)

Returns:
the X position of the OpenGL viewport

getViewportY

public int getViewportY()
Retrieves the Y position of the OpenGL viewport (index 1 of GL_VIEWPORT)

Returns:
the Y position of the OpenGL viewport

loadFont

public Font loadFont(java.net.URL url,
                     StateSelect select,
                     FontParameter... parameterList)
              throws java.io.IOException
Description copied from interface: Renderer
Loads a font.

Specified by:
loadFont in interface Renderer
Parameters:
url - the base URL that can be used to load font data
select - the StateSelect object
parameterList - the font parameters - must be exactly 1 more then the number of expressions in the select object
Returns:
a Font object
Throws:
java.io.IOException - if the font could not be loaded

loadTexture

public Texture loadTexture(java.net.URL url,
                           java.lang.String formatStr,
                           java.lang.String filterStr)
                    throws java.io.IOException
Description copied from interface: Renderer
Loads a texture. Textures are used to create images.

Specified by:
loadTexture in interface Renderer
Parameters:
url - the URL of the texture file
formatStr - a format description - depends on the implementation
filterStr - how the texture should be filtered - should support "nearest" and linear"
Returns:
a Texture object
Throws:
java.io.IOException - if the texture could not be loaded

getLineRenderer

public LineRenderer getLineRenderer()
Description copied from interface: Renderer
Returns the line renderer. If line rendering is not supported then this method returns null. This is an optional operation.

Specified by:
getLineRenderer in interface Renderer
Returns:
the line renderer or null if not supported.

getOffscreenRenderer

public OffscreenRenderer getOffscreenRenderer()
Description copied from interface: Renderer
Returns the offscreen renderer. If offscreen rendering is not supported then this method returns null. This is an optional operation.

Specified by:
getOffscreenRenderer in interface Renderer
Returns:
the offscreen renderer or null if not supported.

getFontMapper

public FontMapper getFontMapper()
Description copied from interface: Renderer
Returns the font mapper object if one is available. This is an optional operation.

Specified by:
getFontMapper in interface Renderer
Returns:
the font mapper or null if not supported.

setFontMapper

public void setFontMapper(FontMapper fontMapper)
Installs a font mapper. It is the responsibility of the font mapper to manage the OpenGL state correctly so that normal rendering by LWJGLRenderer is not disturbed.

Parameters:
fontMapper - the font mapper object - can be null.

createDynamicImage

public DynamicImage createDynamicImage(int width,
                                       int height)
Description copied from interface: Renderer
Creates a dynamic image with undefined content. This is an optional operation.

Specified by:
createDynamicImage in interface Renderer
Parameters:
width - the width of the image
height - the height of the image
Returns:
a new dynamic image or null if the image could not be created

createGradient

public Image createGradient(Gradient gradient)
Specified by:
createGradient in interface Renderer

clipEnter

public void clipEnter(int x,
                      int y,
                      int w,
                      int h)
Description copied from interface: Renderer
Enters a clip region. The new clip region is the intersection of the current clip region with the specified coordinates.

Specified by:
clipEnter in interface Renderer
Parameters:
x - the left edge
y - the top edge
w - the width
h - the height

clipEnter

public void clipEnter(Rect rect)
Description copied from interface: Renderer
Enters a clip region. The new clip region is the intersection of the current clip region with the specified coordinates.

Specified by:
clipEnter in interface Renderer
Parameters:
rect - the coordinates

clipLeave

public void clipLeave()
Description copied from interface: Renderer
Leaves a clip region creeated by #clipEnter

Specified by:
clipLeave in interface Renderer
See Also:
Renderer.clipEnter(int, int, int, int), Renderer.clipEnter(de.matthiasmann.twl.Rect)

clipIsEmpty

public boolean clipIsEmpty()
Description copied from interface: Renderer
Checks if the active clip region is empty (nothing will render).

Specified by:
clipIsEmpty in interface Renderer
Returns:
true if the active clip region is empty

setCursor

public void setCursor(MouseCursor cursor)
Specified by:
setCursor in interface Renderer

setMousePosition

public void setMousePosition(int mouseX,
                             int mouseY)
Description copied from interface: Renderer
Sets the mouse position for SW mouse cursor rendering

Specified by:
setMousePosition in interface Renderer
Parameters:
mouseX - X mouse position
mouseY - Y mouse position

setMouseButton

public void setMouseButton(int button,
                           boolean state)
Description copied from interface: Renderer
Sets the mouse button state for SW mouse cursor rendering

Specified by:
setMouseButton in interface Renderer
Parameters:
button - the mouse button
state - true if the mouse button is pressed
See Also:
Event.MOUSE_LBUTTON, Event.MOUSE_MBUTTON, Event.MOUSE_RBUTTON

load

public LWJGLTexture load(java.net.URL textureUrl,
                         LWJGLTexture.Format fmt,
                         LWJGLTexture.Filter filter)
                  throws java.io.IOException
Throws:
java.io.IOException

load

public LWJGLTexture load(java.net.URL textureUrl,
                         LWJGLTexture.Format fmt,
                         LWJGLTexture.Filter filter,
                         TexturePostProcessing tpp)
                  throws java.io.IOException
Throws:
java.io.IOException

pushGlobalTintColor

public void pushGlobalTintColor(float r,
                                float g,
                                float b,
                                float a)
Description copied from interface: Renderer
Pushes a new tint color on the tint stack. The current tint color is multiplied by the new tint color. For every call of pushGlobalTintColor a call to popGlobalTintColor must be made.

Specified by:
pushGlobalTintColor in interface Renderer
Parameters:
r - red, must be 0.0f <= r <= 1.0f
g - green, must be 0.0f <= g <= 1.0f
b - blue, must be 0.0f <= b <= 1.0f
a - alpha, must be 0.0f <= a <= 1.0f

popGlobalTintColor

public void popGlobalTintColor()
Specified by:
popGlobalTintColor in interface Renderer

pushGlobalTintColorReset

public void pushGlobalTintColorReset()
Pushes a white entry on the tint stack which ignores the previous tint color. It must be removed by calling popGlobalTintColor().

This is useful when rendering to texture


setColor

public void setColor(Color color)
Calls GL11.glColor4f() with the specified color multiplied by the current global tint color.

Parameters:
color - the color to set

drawLine

public void drawLine(float[] pts,
                     int numPts,
                     float width,
                     Color color,
                     boolean drawAsLoop)
Description copied from interface: LineRenderer
Draws a line with the specified width and color. Support for line width other then 1.0 is optional.

Specified by:
drawLine in interface LineRenderer
Parameters:
pts - An array of xy coordinate pairs
numPts - Number of points to draw
width - the line width.
color - the Color of the line.
drawAsLoop - true if a line should be rendered from last to first vertex.

prepareForRendering

protected void prepareForRendering()

renderSWCursor

protected void renderSWCursor()

setNativeCursor

protected void setNativeCursor(org.lwjgl.input.Cursor cursor)
                        throws org.lwjgl.LWJGLException
Throws:
org.lwjgl.LWJGLException

isMouseInsideWindow

protected boolean isMouseInsideWindow()

getTintedColor

protected void getTintedColor(Color color,
                              float[] result)

getTintedColor

protected void getTintedColor(float[] color,
                              float[] result)
Computes the tinted color from the given color.

Parameters:
color - the input color in RGBA order, value range is 0.0 (black) to 255.0 (white).
result - the tinted color in RGBA order, can be the same array as color.

setClipRect

protected void setClipRect()

getClipRect

public boolean getClipRect(Rect rect)
Retrieves the active clip region from the top of the stack

Parameters:
rect - the rect coordinates - may not be updated when clipping is disabled
Returns:
true if clipping is active, false if clipping is disabled