superwaba.ext.xplat.game
Class TextRenderer
java.lang.Object
|
+--superwaba.ext.xplat.game.TextRenderer
- public class TextRenderer
- extends Object
A game specific, fast text renderer (no memory allocs during runtime).
Important! The text displayed must not reach the screen limits!
It renders the text and the digits in image buffers and the display
functions of this class just copies the image buffers back to the
graphic context.
The text buffer is copied first, then the configured amount of digits
of the optional integer are copied next to the text with leading zeros.
This class can be used to display game information such as scores, levels, etc.
You can find a complete game API sample named 'Ping' in the SW examples folder.
Here is some sample code:
import superwaba.ext.xplat.game.*;
import superwaba.ext.xplat.util.props.*;
...
public class Ping extends GameEngine {
// 2 text renderers to quickly display level and score values
private TextRenderer levelRenderer;
private TextRenderer scoreRenderer;
//---------------------------------------------------------
// overload the API's game init event.
// this function is called when the game is launched.
//---------------------------------------------------------
public void onGameInit() {
// create two text renderers, one for the 'level' and one for the 'score'
// we use the current font, the text should be black on the game background
// level display has 2 digits: max value is 99
levelRenderer=createTextRenderer(getFont(),Color.BLACK,"level:",2);
// score display has 5 digits: max value is 99999
scoreRenderer=createTextRenderer(getFont(),Color.BLACK,"score:",5);
}
//---------------------------------------------------------
// overload SuperWaba's onPaint function.
//---------------------------------------------------------
public void onPaint(Graphics gfx) {
...
if (gameIsRunning) { // game engine's running state
...
// display the game level & score, both enable transparency
levelRenderer.display(15,2,level,true);
scoreRenderer.display(80,2,score,true);
...
}
...
}
Field Summary |
protected int |
fmH
height in pixels of the drawing (= font metrics height). |
protected int |
textWidth
width in pixels of the text. |
Method Summary |
void |
display(int x,
int y,
boolean transparent)
Display a text rendering.
|
void |
display(int x,
int y,
int value,
boolean transparent)
Display a text rendering.
|
int |
getHeight()
Returns the height of the current font |
int |
getWidth()
Returns the maximum width of the text plus the number of digits |
fmH
protected int fmH
- height in pixels of the drawing (= font metrics height).
textWidth
protected int textWidth
- width in pixels of the text.
TextRenderer
protected TextRenderer(ISurface surface,
Font font,
Color foreColor,
Color backColor,
String text,
int maxDigits)
- Creates a text renderer with the given parameters.
- Parameters:
surface
- to display the text onfont
- to useforeColor
- text foreground colorbackColor
- text background colortext
- to be displayed before the digits (or null if none)maxDigits
- the number of digits to display. E.g.: 4 means the max value shown will be 9999.
display
public void display(int x,
int y,
boolean transparent)
- Display a text rendering.
This function just draws the text to the
GameEngineMainWindow at the specified x,y position.
- Parameters:
x
- position.y
- position.transparent
- should the background be preserved.
display
public void display(int x,
int y,
int value,
boolean transparent)
- Display a text rendering.
This function just copies back the image buffer of the text to the
GameEngineMainWindow at the specified x,y position and displays the
integer value next to the text by using pre-rendered digit image buffers.
- Parameters:
x
- position.y
- position.value
- positive integer value to draw next to the text.transparent
- should the background be preserved.
getWidth
public int getWidth()
- Returns the maximum width of the text plus the number of digits
getHeight
public int getHeight()
- Returns the height of the current font