org.jrabbit.base.graphics.font.renderer
Class AcceleratedFontRenderer

java.lang.Object
  extended by org.jrabbit.base.graphics.font.renderer.AcceleratedFontRenderer
All Implemented Interfaces:
Createable, DataController, Destroyable, FontRenderer
Direct Known Subclasses:
AngelCodeRenderer, UnicodeRenderer

public abstract class AcceleratedFontRenderer
extends java.lang.Object
implements FontRenderer

AcceleratedFontRenderer provides a default abstract FontRenderer class that uses DisplayLists to hasten rendering. Both default FontRenderers extend this class, but have DisplayList acceleration turned off by default. This is to allow them to support rapidly changing text more efficiently.

Author:
Chris Molini

Nested Class Summary
protected  class AcceleratedFontRenderer.TextDL
          Represents a display list that renders text.
 
Field Summary
protected  boolean accelerate
          This class can compile display lists to "remember" Strings that have been drawn.
protected  int baseDL
          When the DisplayLists are created, the ID of the first one is stored here.
protected  java.util.LinkedHashMap<java.lang.String,AcceleratedFontRenderer.TextDL> displayLists
          The display list cache for rendered text.
protected  AcceleratedFontRenderer.TextDL eldestDL
          The eldest display list.
protected  int eldestDLID
          The eldest display list ID.
protected  int maxDL
          The maximum number of display lists stored.
 
Constructor Summary
AcceleratedFontRenderer()
           
 
Method Summary
 void create()
          If acceleration is enabled, create() will allocate the desired display lists.
protected abstract  void drawString(java.lang.String text)
          Renders the supplied text.
protected abstract  float measureString(java.lang.String text)
          Directly measures the width of the supplied String.
 void render(float x, float y, java.lang.String text)
          First checks to see if display lists are being used.
 void useAcceleration(boolean accel)
          Tells the font renderer whether or not it should attempt to hasten rendering of text.
 float widthOf(java.lang.String text)
          Measures the width of a String of text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jrabbit.base.graphics.font.renderer.FontRenderer
lineHeight
 
Methods inherited from interface org.jrabbit.base.data.DataController
valid
 
Methods inherited from interface org.jrabbit.base.data.Destroyable
destroy
 

Field Detail

accelerate

protected boolean accelerate
This class can compile display lists to "remember" Strings that have been drawn. Doing so can potentially increase rendering speed significantly - IF only static strings are being drawn. If the Strings being drawn are constantly changing, then every String creates a new display list, and you end up never using any display lists to speed up rendering and yet are burdened with the overhead of creating them. For this reason, display lists are initially turned off.


maxDL

protected int maxDL
The maximum number of display lists stored.


baseDL

protected int baseDL
When the DisplayLists are created, the ID of the first one is stored here.


eldestDLID

protected int eldestDLID
The eldest display list ID. If the cache of display lists becomes full, then the oldest DisplayList is removed to make room for the newest one.


eldestDL

protected AcceleratedFontRenderer.TextDL eldestDL
The eldest display list. Used to simplify replacing the oldest DisplayList with a new one.


displayLists

protected java.util.LinkedHashMap<java.lang.String,AcceleratedFontRenderer.TextDL> displayLists
The display list cache for rendered text. It's slightly modified to make it easier to dynamically replace old display lists when the cache is full.

Constructor Detail

AcceleratedFontRenderer

public AcceleratedFontRenderer()
Method Detail

useAcceleration

public void useAcceleration(boolean accel)
Tells the font renderer whether or not it should attempt to hasten rendering of text. If a particular font is only going to be rendering large, static amounts of text, it's strongly recommended to turn this on. Likewise, if the text being rendered is dynamic and expected to change, turn this OFF.

Parameters:
accel - Whether or not to speed up rendering with display lists.

create

public void create()
If acceleration is enabled, create() will allocate the desired display lists.

Specified by:
create in interface Createable

render

public void render(float x,
                   float y,
                   java.lang.String text)
First checks to see if display lists are being used. If so, then checks to see if a Display List has been defined for rendering. If no DisplayLists have been defined, then the Renderer compiles and executes one via drawString(), and stores it for later use. If display lists are disabled, this method delegates rendering to drawString().

Specified by:
render in interface FontRenderer
Parameters:
x - The x coordinate to render at.
y - The y coordinate to render at.
text - The text to render.

widthOf

public float widthOf(java.lang.String text)
Measures the width of a String of text. Attempts to utilize the cached display lists to learn the width of the String; if no such information is stored, then delegates to measureString().

Specified by:
widthOf in interface FontRenderer
Parameters:
text - The String to measure.
Returns:
The width of the text when rendered.

measureString

protected abstract float measureString(java.lang.String text)
Directly measures the width of the supplied String.

Parameters:
text - The String to measure.
Returns:
The width of the text when rendered.

drawString

protected abstract void drawString(java.lang.String text)
Renders the supplied text. The current location of the Modelview matrix is the top-left of the text drawn.

Parameters:
text - The text to draw.