From a115dd10128240c0f8c7784b2153f2840948aedd Mon Sep 17 00:00:00 2001 From: "nathan.sweet" Date: Sat, 27 Nov 2010 02:51:19 +0000 Subject: [PATCH] [changed] More BitmapFont refactoring. It is getting very nice now! --- .../gdx/graphics/particles/ParticleEditor.java | 8 +- .../src/com/badlogic/gdx/twl/renderer/GdxFont.java | 26 +- gdx/src/com/badlogic/gdx/graphics/BitmapFont.java | 269 +++++++++------------ .../com/badlogic/gdx/graphics/BitmapFontCache.java | 170 +++++-------- .../gdx/tests/BitmapFontAlignmentTest.java | 32 +-- .../com/badlogic/gdx/tests/BitmapFontFlipTest.java | 45 ++-- .../src/com/badlogic/gdx/tests/BitmapFontTest.java | 39 +-- .../src/com/badlogic/gdx/tests/Box2DTest.java | 3 +- .../src/com/badlogic/gdx/tests/FilesTest.java | 2 +- .../src/com/badlogic/gdx/tests/MD5Test.java | 3 +- .../com/badlogic/gdx/tests/PixelsPerInchTest.java | 2 +- 11 files changed, 275 insertions(+), 324 deletions(-) diff --git a/extensions/particle-editor/src/com/badlogic/gdx/graphics/particles/ParticleEditor.java b/extensions/particle-editor/src/com/badlogic/gdx/graphics/particles/ParticleEditor.java index a38beb2f8..085dff820 100644 --- a/extensions/particle-editor/src/com/badlogic/gdx/graphics/particles/ParticleEditor.java +++ b/extensions/particle-editor/src/com/badlogic/gdx/graphics/particles/ParticleEditor.java @@ -297,10 +297,10 @@ public class ParticleEditor extends JFrame { // gl.drawLine(mouseX, mouseY - 5, mouseX, mouseY + 6); } - font.draw(spriteBatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 10, Color.WHITE); - font.draw(spriteBatch, "Count: " + activeCount, 10, 30, Color.WHITE); - font.draw(spriteBatch, "Max: " + lastMaxActive, 10, 50, Color.WHITE); - font.draw(spriteBatch, (int)(getEmitter().getPercentComplete() * 100) + "%", 10, 70, Color.WHITE); + font.draw(spriteBatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 10); + font.draw(spriteBatch, "Count: " + activeCount, 10, 30); + font.draw(spriteBatch, "Max: " + lastMaxActive, 10, 50); + font.draw(spriteBatch, (int)(getEmitter().getPercentComplete() * 100) + "%", 10, 70); spriteBatch.end(); diff --git a/extensions/twl/gdx-twl/src/com/badlogic/gdx/twl/renderer/GdxFont.java b/extensions/twl/gdx-twl/src/com/badlogic/gdx/twl/renderer/GdxFont.java index 7bbde5f06..c560c880a 100644 --- a/extensions/twl/gdx-twl/src/com/badlogic/gdx/twl/renderer/GdxFont.java +++ b/extensions/twl/gdx-twl/src/com/badlogic/gdx/twl/renderer/GdxFont.java @@ -54,7 +54,7 @@ class GdxFont implements Font { public GdxFont (GdxRenderer renderer, BitmapFont bitmapFont, Map params, Collection condParams) { this.bitmapFont = bitmapFont; this.renderer = renderer; - yOffset = bitmapFont.getLineHeight() - bitmapFont.getCapHeight() - bitmapFont.getBaseLine(); + yOffset = -bitmapFont.getCapHeight(); ArrayList states = new ArrayList(); for (FontParameter p : condParams) { @@ -74,8 +74,8 @@ class GdxFont implements Font { FontState fontState = evalFontState(as); x += fontState.offsetX; y += fontState.offsetY + yOffset; - com.badlogic.gdx.graphics.Color color = renderer.getColor(fontState.color); - return (int)bitmapFont.draw(renderer.spriteBatch, str, x, y, color, start, end); + bitmapFont.setColor(renderer.getColor(fontState.color)); + return bitmapFont.draw(renderer.spriteBatch, str, x, y, start, end).width; } public int drawMultiLineText (AnimationState as, int x, int y, CharSequence str, int width, @@ -83,8 +83,8 @@ class GdxFont implements Font { FontState fontState = evalFontState(as); x += fontState.offsetX; y += fontState.offsetY + yOffset; - com.badlogic.gdx.graphics.Color color = renderer.getColor(fontState.color); - return bitmapFont.drawMultiLine(renderer.spriteBatch, str, x, y, color, width, gdxAlignment[align.ordinal()]); + bitmapFont.setColor(renderer.getColor(fontState.color)); + return bitmapFont.drawMultiLine(renderer.spriteBatch, str, x, y, width, gdxAlignment[align.ordinal()]).width; } public FontCache cacheText (FontCache cache, CharSequence str) { @@ -94,14 +94,16 @@ class GdxFont implements Font { public FontCache cacheText (FontCache cache, CharSequence str, int start, int end) { if (cache == null) cache = new GdxFontCache(); GdxFontCache bitmapCache = (GdxFontCache)cache; - bitmapCache.setText(str, 0, yOffset, com.badlogic.gdx.graphics.Color.WHITE, start, end); + bitmapFont.setColor(com.badlogic.gdx.graphics.Color.WHITE); + bitmapCache.setText(str, 0, yOffset, start, end); return cache; } public FontCache cacheMultiLineText (FontCache cache, CharSequence str, int width, de.matthiasmann.twl.HAlignment align) { if (cache == null) cache = new GdxFontCache(); GdxFontCache bitmapCache = (GdxFontCache)cache; - bitmapCache.setMultiLineText(str, 0, yOffset, com.badlogic.gdx.graphics.Color.WHITE, width, gdxAlignment[align.ordinal()]); + bitmapFont.setColor(com.badlogic.gdx.graphics.Color.WHITE); + bitmapCache.setMultiLineText(str, 0, yOffset, width, gdxAlignment[align.ordinal()]); return cache; } @@ -110,7 +112,7 @@ class GdxFont implements Font { } public int getBaseLine () { - return bitmapFont.getBaseLine(); + return bitmapFont.getCapHeight(); } public int getLineHeight () { @@ -184,6 +186,14 @@ class GdxFont implements Font { draw(renderer.spriteBatch); } + public int getWidth () { + return getBounds().width; + } + + public int getHeight () { + return getBounds().height; + } + public void destroy () { } } diff --git a/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java b/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java index b2c2c883a..296bd1809 100644 --- a/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java +++ b/gdx/src/com/badlogic/gdx/graphics/BitmapFont.java @@ -22,36 +22,30 @@ package com.badlogic.gdx.graphics; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Files.FileType; import com.badlogic.gdx.files.FileHandle; -import com.badlogic.gdx.files.FileHandleStream; import com.badlogic.gdx.graphics.Texture.TextureFilter; import com.badlogic.gdx.graphics.Texture.TextureWrap; import com.badlogic.gdx.utils.GdxRuntimeException; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.StringTokenizer; - /** - * Loads and renders AngleCode BMFont files. The bitmap font consists of 2 files: the .fnt file which must be saved with text - * encoding (not xml or binary!) and the bitmap file holding the glyphs, usually in .png format.
+ * Renders bitmap fonts. The bitmap font consists of 2 files: an image file (or {@link Sprite}) containing the glyphs and a file + * in the AngleCode BMFont text format that describes where each glyph is on the image. Currently only a single image of glyphs is + * supported.
*
- * This implementation currently only supports a single glyph page.
+ * Text is drawn using a {@link SpriteBatch}. Text can be cached in a {@link BitmapFontCache} for faster rendering of static text, + * which saves needing to compute the location of each glyph each frame.
*
- * To draw text with this class you need to call one of the draw() methods together with a {@link SpriteBatch}. The SpriteBatch - * must be in rendering mode, that is, {@link SpriteBatch#begin()} must have been called before drawing.
+ * * The texture for a BitmapFont loaded from a file is managed. {@link #dispose()} must be called to free the texture when no + * longer needed. A BitmapFont loaded using a sprite is managed if the sprite's texture is managed. Disposing the BitmapFont + * disposes the sprite's texture, which may not be desirable if the texture is still being used elsewhere.
*
- * Text can be cached in a {@link BitmapFontCache} for faster rendering of static text.
- *
- * A BitmapFont loaded from a file is managed. {@link #dispose()} must be called to free the backing texture when no longer - * needed. A BitmapFont loaded using a sprite is managed if the sprite's texture is managed. Disposing the BitmapFont disposes the - * sprite's texture, which may not be desirable if the texture is still being used elsewhere.
- *
- * The code is heavily based on Matthias Mann's TWL BitmapFont class. Thanks for sharing, Matthias! :) + * The code is based on Matthias Mann's TWL BitmapFont class. Thanks for sharing, Matthias! :) * @author Nathan Sweet * @author Matthias Mann */ @@ -67,14 +61,14 @@ public class BitmapFont { int down; private final Glyph[][] glyphs = new Glyph[PAGES][]; - private int baseLine; private int spaceWidth; private int xHeight; private final TextBounds textBounds = new TextBounds(); + private float color = Color.WHITE.toFloatBits(); /** - * Creates a new BitmapFont using the default 15pt Arial font included in the gdx jar file. This is here to get you up and - * running quickly. + * Creates a new BitmapFont using the default 15pt Arial font included in the libgdx jar file. This is convenient to easy + * display some text without bothering with generating a bitmap font. */ public BitmapFont () { this(Gdx.files.classpath("com/badlogic/gdx/utils/arial-15.fnt"), @@ -83,7 +77,8 @@ public class BitmapFont { /** * Creates a new BitmapFont with the glyphs relative to the specified sprite. - * @param sprite The sprite containing the glyphs. It must NOT be flipped. + * @param sprite The sprite containing the glyphs. The glyphs must be relative to the lower left corner (ie, the sprite should + * not be flipped). * @param flip If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner. */ public BitmapFont (FileHandle fontFile, Sprite sprite, boolean flip) { @@ -91,10 +86,7 @@ public class BitmapFont { } /** - * Creates a new BitmapFont instance based on a .fnt file and an image file holding the page with glyphs. Currently only - * supports single page AngleCode fonts. - * @param fontFile The font file - * @param imageFile The image file + * Creates a new BitmapFont instance based on a BMFont file and an image file holding the page with glyphs. * @param flip If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner. */ public BitmapFont (FileHandle fontFile, FileHandle imageFile, boolean flip) { @@ -109,9 +101,6 @@ public class BitmapFont { float invTexHeight = 1.0f / sprite.getTexture().getHeight(); float uSprite = sprite.getTextureRegionX(); float vSprite = sprite.getTextureRegionY(); - float u2Sprite = uSprite + sprite.getTextureRegionWidth(); - float v2Sprite = vSprite + sprite.getTextureRegionHeight(); - BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512); try { reader.readLine(); // info @@ -123,7 +112,7 @@ public class BitmapFont { lineHeight = Integer.parseInt(common[1].substring(11)); if (!common[2].startsWith("base=")) throw new GdxRuntimeException("Invalid font file: " + fontFile); - baseLine = Integer.parseInt(common[2].substring(5)); + int baseLine = Integer.parseInt(common[2].substring(5)); reader.readLine(); // page while (true) { @@ -141,7 +130,7 @@ public class BitmapFont { if (ch <= Character.MAX_VALUE) { Glyph[] page = glyphs[ch / PAGE_SIZE]; if (page == null) glyphs[ch / PAGE_SIZE] = page = new Glyph[PAGE_SIZE]; - page[ch & (PAGE_SIZE - 1)] = glyph; + page[ch & PAGE_SIZE - 1] = glyph; } else continue; tokens.nextToken(); @@ -149,7 +138,7 @@ public class BitmapFont { tokens.nextToken(); float srcY = Integer.parseInt(tokens.nextToken()); tokens.nextToken(); - glyph.width = (Integer.parseInt(tokens.nextToken())); + glyph.width = Integer.parseInt(tokens.nextToken()); tokens.nextToken(); glyph.height = Integer.parseInt(tokens.nextToken()); tokens.nextToken(); @@ -192,7 +181,7 @@ public class BitmapFont { } Glyph g = getGlyph(' '); - spaceWidth = (g != null) ? g.xadvance + g.width : 1; + spaceWidth = g != null ? g.xadvance + g.width : 1; g = getGlyph('x'); xHeight = g != null ? g.height : 1; @@ -218,39 +207,32 @@ public class BitmapFont { Glyph getGlyph (char ch) { Glyph[] page = glyphs[ch / PAGE_SIZE]; - if (page != null) return page[ch & (PAGE_SIZE - 1)]; + if (page != null) return page[ch & PAGE_SIZE - 1]; return null; } /** - * Draws the given string at the given position with the given color. You can only call this between - * {@link SpriteBatch#begin()}/ {@link SpriteBatch#end()}. - * @param spriteBatch The {@link SpriteBatch} to use - * @param str The string - * @param x The x position of the left most character - * @param y The y position of the left most character's top left corner - * @param color The color - * @return the width of the rendered string + * Draws a string at the specified position and color. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link #getCapHeight() cap height}). + * @return The bounds of the rendered string (the height is the distance from y to the baseline). Note the same TextBounds + * instance is used for all methods that return TextBounds. */ - public int draw (SpriteBatch spriteBatch, CharSequence str, float x, float y, Color color) { - return draw(spriteBatch, str, x, y, color, 0, str.length()); + public TextBounds draw (SpriteBatch spriteBatch, CharSequence str, float x, float y) { + return draw(spriteBatch, str, x, y, 0, str.length()); } /** - * Draws the given string at the given position with the given color. You can only call this between - * {@link SpriteBatch#begin()}/ {@link SpriteBatch#end()}. - * @param spriteBatch The {@link SpriteBatch} to use - * @param str The string - * @param x The x position of the left most character - * @param y The y position of the left most character's top left corner - * @param tint The color - * @param start the first character of the string to draw - * @param end the last character of the string to draw - * @return the width of the rendered string + * Draws a substring at the specified position. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link #getCapHeight() cap height}). + * @param start The first character of the string to draw. + * @param end The last character of the string to draw (exclusive). + * @return The bounds of the rendered string (the height is the distance from y to the baseline). Note the same TextBounds + * instance is used for all methods that return TextBounds. */ - public int draw (SpriteBatch spriteBatch, CharSequence str, float x, float y, Color tint, int start, int end) { + public TextBounds draw (SpriteBatch spriteBatch, CharSequence str, float x, float y, int start, int end) { final Texture texture = sprite.getTexture(); - final float color = tint.toFloatBits(); y += yOffset; float startX = x; Glyph lastGlyph = null; @@ -273,47 +255,37 @@ public class BitmapFont { lastGlyph.v, lastGlyph.u2, lastGlyph.v2, color); x += g.xadvance; } - return (int)(x - startX); + textBounds.width = (int)(x - startX); + textBounds.height = capHeight; + return textBounds; } /** - * Draws the given string at the given position with the given color. The position coincides with the top left corner of the - * first line's glyph. This method interprets new lines. You can only call this between {@link SpriteBatch#begin()}/ - * {@link SpriteBatch#end()}. - * @param spriteBatch The {@link SpriteBatch} to use - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param color The color - * @return The height of the rendered string + * Draws a string, which may contain newlines (\n), at the specified position. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link #getCapHeight() cap height}). + * @return The bounds of the rendered string (the height is the distance from y to the baseline of the last line). Note the + * same TextBounds instance is used for all methods that return TextBounds. */ - public int drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y, Color color) { - return drawMultiLine(spriteBatch, str, x, y, color, 0, HAlignment.LEFT); + public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y) { + return drawMultiLine(spriteBatch, str, x, y, 0, HAlignment.LEFT); } /** - * Draws the given string at the given position with the given color. The position coincides with the top left corner of the - * first line's glyph. The method interprets new lines. You can only call this between {@link SpriteBatch#begin()}/ - * {@link SpriteBatch#end()}.
- *
- * You can specify the horizontal alignment of the text with the alignmentWidth and alignment - * parameters. The first parameter specifies the width of the rectangle the text should be aligned in (x to x + - * alignmentWidth). The second parameter specifies the alignment itself. - * @param spriteBatch The {@link SpriteBatch} to use - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param color The color - * @param alignmentWidth The alignment width - * @param alignment The horizontal alignment - * @return The height of the multiline text + * Draws a string, which may contain newlines (\n), at the specified position and alignment. Each line is aligned horizontally + * within a rectangle of the specified width. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link #getCapHeight() cap height}). + * @return The bounds of the rendered string (the height is the distance from y to the baseline of the last line). Note the + * same TextBounds instance is used for all methods that return TextBounds. */ - public int drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y, Color color, float alignmentWidth, + public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment) { int down = this.down; int start = 0; int numLines = 0; int length = str.length(); + int maxWidth = 0; while (start < length) { int lineEnd = indexOf(str, '\n', start); float xOffset = 0; @@ -322,51 +294,44 @@ public class BitmapFont { xOffset = alignmentWidth - lineWidth; if (alignment == HAlignment.CENTER) xOffset /= 2; } - draw(spriteBatch, str, x + xOffset, y, color, start, lineEnd); + int lineWidth = draw(spriteBatch, str, x + xOffset, y, start, lineEnd).width; + maxWidth = Math.max(maxWidth, lineWidth); start = lineEnd + 1; y += down; numLines++; } - return capHeight + (numLines - 1) * lineHeight; + textBounds.width = maxWidth; + textBounds.height = capHeight + (numLines - 1) * lineHeight; + return textBounds; } /** - * Draws the given string at the given position with the given color. The position coincides with the top left corner of the - * first line's glyph. This method interprets new lines and causes the text to wrap at spaces based on the given - * wrapWidth. You can only call this between {@link SpriteBatch#begin()}/ {@link SpriteBatch#end()}. - * @param spriteBatch The {@link SpriteBatch} to use - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param color The color - * @param wrapWidth The wrap width - * @return the height of the rendered string + * Draws a string, which may contain newlines (\n), with the specified position and color. Each line is automatically wrapped + * to keep it within a rectangle of the specified width. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link #getCapHeight() cap height}). + * @return The bounds of the rendered string (the height is the distance from y to the baseline of the last line). Note the + * same TextBounds instance is used for all methods that return TextBounds. */ - public int drawWrapped (SpriteBatch spriteBatch, CharSequence str, float x, float y, Color color, float wrapWidth) { - return drawWrapped(spriteBatch, str, x, y, color, wrapWidth, HAlignment.LEFT); + public TextBounds drawWrapped (SpriteBatch spriteBatch, CharSequence str, float x, float y, float wrapWidth) { + return drawWrapped(spriteBatch, str, x, y, wrapWidth, HAlignment.LEFT); } /** - * Draws the given string at the given position with the given color. The position coincides with the top left corner of the - * first line's glyph. This method interprets new lines and causes the text to wrap at spaces based on the given - * wrapWidth. You can only call this between {@link SpriteBatch#begin()}/ {@link SpriteBatch#end()}.
- *
- * You can specify the horizontal alignment of the text within the wrapWidth by using the alignment - * parameter. - * @param spriteBatch The {@link SpriteBatch} to use - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param color The color - * @param wrapWidth The wrap width - * @return the height of the rendered string + * Draws a string, which may contain newlines (\n), with the specified position and color. Each line is automatically wrapped + * to keep it within a rectangle of the specified width, and aligned horizontally within that rectangle. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link #getCapHeight() cap height}). + * @return The bounds of the rendered string (the height is the distance from y to the baseline of the last line). Note the + * same TextBounds instance is used for all methods that return TextBounds. */ - public int drawWrapped (SpriteBatch spriteBatch, CharSequence str, float x, float y, Color color, float wrapWidth, + public TextBounds drawWrapped (SpriteBatch spriteBatch, CharSequence str, float x, float y, float wrapWidth, HAlignment alignment) { int down = this.down; int start = 0; int numLines = 0; int length = str.length(); + int maxWidth = 0; while (start < length) { int lineEnd = start + computeVisibleGlpyhs(str, start, indexOf(str, '\n', start), wrapWidth); if (lineEnd < length) { @@ -383,29 +348,32 @@ public class BitmapFont { xOffset = wrapWidth - lineWidth; if (alignment == HAlignment.CENTER) xOffset /= 2; } - draw(spriteBatch, str, x + xOffset, y, color, start, lineEnd); + int lineWidth = draw(spriteBatch, str, x + xOffset, y, start, lineEnd).width; + maxWidth = Math.max(maxWidth, lineWidth); start = lineEnd + 1; y += down; numLines++; } - return capHeight + (numLines - 1) * lineHeight; + textBounds.width = maxWidth; + textBounds.height = capHeight + (numLines - 1) * lineHeight; + return textBounds; } /** - * Computes the width of the string. - * @param str The string - * @return the width + * Returns the size of the specified string. The height is the distance from the top of most capital letters in the font (the + * {@link #getCapHeight() cap height}) to the baseline. Note the same TextBounds instance is used for all methods that return + * TextBounds. */ public TextBounds getBounds (CharSequence str) { return getBounds(str, 0, str.length()); } /** - * Computes the width of the string. - * @param str the string - * @param start The first character index - * @param end The last character index (exclusive) - * @return The string width + * Returns the size of the specified substring. The height is the distance from the top of most capital letters in the font + * (the {@link #getCapHeight() cap height}) to the baseline. Note the same TextBounds instance is used for all methods that + * return TextBounds. + * @param start The first character of the string. + * @param end The last character of the string (exclusive). */ public TextBounds getBounds (CharSequence str, int start, int end) { int width = 0; @@ -432,9 +400,9 @@ public class BitmapFont { } /** - * Computes the maximum width of the string, respecting newlines. - * @param str The string - * @return The maximum width + * Returns the size of the specified string, which may contain newlines. The height is the distance from the top of most + * capital letters in the font (the {@link #getCapHeight() cap height}) to the baseline of the last line of text. Note the same + * TextBounds instance is used for all methods that return TextBounds. */ public TextBounds getMultiLineBounds (CharSequence str) { int start = 0; @@ -453,6 +421,12 @@ public class BitmapFont { return textBounds; } + /** + * Returns the size of the specified string, which may contain newlines and is wrapped to keep it within a rectangle of the + * specified width. The height is the distance from the top of most capital letters in the font (the {@link #getCapHeight() cap + * height}) to the baseline of the last line of text. Note the same TextBounds instance is used for all methods that return + * TextBounds. + */ public TextBounds getWrappedBounds (CharSequence str, float wrapWidth) { int start = 0; int maxWidth = 0; @@ -479,12 +453,9 @@ public class BitmapFont { } /** - * Returns the number of characters that can be rendered given the available width. - * @param str The string - * @param start The start index of the first character - * @param end The index of the last character (exclusive) - * @param availableWidth the available width - * @return The number of characters that fit into availableWdith + * Returns the number of glyphs from the substring that can be rendered in the specified width. + * @param start The first character of the string. + * @param end The last character of the string (exclusive). */ public int computeVisibleGlpyhs (CharSequence str, int start, int end, float availableWidth) { int index = start; @@ -503,43 +474,43 @@ public class BitmapFont { return index - start; } - /** - * @return The glyph sprite - */ - public Sprite getSprite () { - return sprite; + public void setColor (Color tint) { + this.color = tint.toFloatBits(); } - /** - * @return The baseline offset, which is the distance from the drawing position to the line that most glyphs sit on - */ - public int getBaseLine () { - return baseLine; + public void setColor (float r, float g, float b, float a) { + int intBits = (int)(255 * a) << 24 | (int)(255 * b) << 16 | (int)(255 * g) << 8 | (int)(255 * r); + color = Float.intBitsToFloat(intBits); + } + + public Sprite getSprite () { + return sprite; } /** - * @return The line height, which is the distance from one line of text to the next + * Returns the line height, which is the distance from one line of text to the next. */ public int getLineHeight () { return lineHeight; } /** - * @return The width of the space character + * Returns the width of the space character. */ public int getSpaceWidth () { return spaceWidth; } /** - * @return The x-height, which is the typical height of lowercase characters + * Returns the x-height, which is the distance from the top of most lowercase characters to the basline. */ public int getXHeight () { return xHeight; } /** - * @return The cap height, which is the typical height of uppercase characters + * Returns the cap height, which is the distance from the top of most uppercase characters to the basline. Since the drawing + * position is the cap height of the first line, the cap height can be used to get the location of the baseline. */ public int getCapHeight () { return capHeight; @@ -562,7 +533,7 @@ public class BitmapFont { int getKerning (char ch) { if (kerning != null) { byte[] page = kerning[ch >>> LOG2_PAGE_SIZE]; - if (page != null) return page[ch & (PAGE_SIZE - 1)]; + if (page != null) return page[ch & PAGE_SIZE - 1]; } return 0; } @@ -571,7 +542,7 @@ public class BitmapFont { if (kerning == null) kerning = new byte[PAGES][]; byte[] page = kerning[ch >>> LOG2_PAGE_SIZE]; if (page == null) kerning[ch >>> LOG2_PAGE_SIZE] = page = new byte[PAGE_SIZE]; - page[ch & (PAGE_SIZE - 1)] = (byte)value; + page[ch & PAGE_SIZE - 1] = (byte)value; } } diff --git a/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java b/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java index 4c2f313bc..2f06e248d 100644 --- a/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java +++ b/gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java @@ -24,21 +24,21 @@ package com.badlogic.gdx.graphics; import com.badlogic.gdx.graphics.BitmapFont.Glyph; import com.badlogic.gdx.graphics.BitmapFont.HAlignment; +import com.badlogic.gdx.graphics.BitmapFont.TextBounds; /** - * A BitmapFontCache caches glyph geometry for a BitmapFont, providing a fast way to render static text.
- *
- * The code is heavily based on Matthias Mann's TWL BitmapFont class. Thanks for sharing, Matthias! :) + * Caches glyph geometry for a BitmapFont, providing a fast way to render static text. This saves needing to compute the location + * of each glyph each frame. * @author Nathan Sweet * @author Matthias Mann */ public class BitmapFontCache { private final BitmapFont font; - private float[] vertices; + private float[] vertices = new float[0]; private int idx; - int width, height; private float x, y; - private float color; + private float color = Color.WHITE.toFloatBits(); + private final TextBounds textBounds = new TextBounds(); public BitmapFontCache (BitmapFont font) { this.font = font; @@ -69,10 +69,6 @@ public class BitmapFontCache { } } - /** - * Sets the tint color of the text. - * @param tint The {@link Color} - */ public void setColor (Color tint) { final float color = tint.toFloatBits(); if (color == this.color) return; @@ -82,9 +78,6 @@ public class BitmapFontCache { vertices[i] = color; } - /** - * Sets the tint color of the text. - */ public void setColor (float r, float g, float b, float a) { int intBits = ((int)(255 * a) << 24) | ((int)(255 * b) << 16) | ((int)(255 * g) << 8) | ((int)(255 * r)); float color = Float.intBitsToFloat(intBits); @@ -95,16 +88,11 @@ public class BitmapFontCache { vertices[i] = color; } - /** - * Draws the contents of the cache via a {@link SpriteBatch}. Must be called between a {@link SpriteBatch#begin()}/ - * {@link SpriteBatch#end()} pair. - * @param spriteBatch The SpriteBatch - */ public void draw (SpriteBatch spriteBatch) { spriteBatch.draw(font.getSprite().getTexture(), vertices, 0, idx); } - void reset (int glyphCount) { + private void reset (int glyphCount) { x = 0; y = 0; idx = 0; @@ -113,14 +101,14 @@ public class BitmapFontCache { if (vertices == null || vertices.length < vertexCount) vertices = new float[vertexCount]; } - private int addToCache (CharSequence str, float x, float y, float color, int start, int end) { + private int addToCache (CharSequence str, float x, float y, int start, int end) { float startX = x; BitmapFont font = this.font; Glyph lastGlyph = null; while (start < end) { lastGlyph = font.getGlyph(str.charAt(start++)); if (lastGlyph != null) { - addGlyph(lastGlyph, x, y, color); + addGlyph(lastGlyph, x, y); x += lastGlyph.xadvance; break; } @@ -131,14 +119,14 @@ public class BitmapFontCache { if (g != null) { x += lastGlyph.getKerning(ch); lastGlyph = g; - addGlyph(lastGlyph, x, y, color); + addGlyph(lastGlyph, x, y); x += g.xadvance; } } return (int)(x - startX); } - void addGlyph (Glyph glyph, float x, float y, float color) { + private void addGlyph (Glyph glyph, float x, float y) { x += glyph.xoffset; y += glyph.yoffset; final float x2 = x + glyph.width; @@ -175,66 +163,54 @@ public class BitmapFontCache { } /** - * Caches the given string at the given position with the given color in the provided {@link BitmapFontCache}. - * @param str The string - * @param x The x position of the left most character - * @param y The y position of the left most character's top left corner - * @param tint The color + * Caches a string with the specified position. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). + * @return The bounds of the cached string (the height is the distance from y to the baseline). */ - public void setText (CharSequence str, float x, float y, Color tint) { - setText(str, x, y, tint, 0, str.length()); + public TextBounds setText (CharSequence str, float x, float y) { + return setText(str, x, y, 0, str.length()); } /** - * Caches the given string at the given position with the given color in the provided {@link BitmapFontCache}. - * @param str The string - * @param x The x position of the left most character - * @param y The y position of the left most character's top left corner - * @param tint The color - * @param start The first character of the string to draw - * @param end The last character of the string to draw + * Caches a substring with the specified position. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). + * @param start The first character of the string to draw. + * @param end The last character of the string to draw (exclusive). + * @return The bounds of the cached string (the height is the distance from y to the baseline). */ - public void setText (CharSequence str, float x, float y, Color tint, int start, int end) { - final float color = tint.toFloatBits(); + public TextBounds setText (CharSequence str, float x, float y, int start, int end) { reset(end - start); y += font.yOffset; - width = addToCache(str, x, y, color, start, end); - height = font.capHeight; + textBounds.width = addToCache(str, x, y, start, end); + textBounds.height = font.capHeight; + return textBounds; } /** - * Caches the given string at the given position with the given color in the provided {@link BitmapFontCache}. The position - * coincides with the top left corner of the first line's glyph. The method interprets new lines. - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param tint The color + * Caches a string, which may contain newlines (\n), with the specified position. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). + * @return The bounds of the cached string (the height is the distance from y to the baseline of the last line). */ - public void setMultiLineText (CharSequence str, float x, float y, Color tint) { - setMultiLineText(str, x, y, tint, 0, HAlignment.LEFT); + public TextBounds setMultiLineText (CharSequence str, float x, float y) { + return setMultiLineText(str, x, y, 0, HAlignment.LEFT); } /** - * Caches the given string at the given position with the given color in the provided {@link BitmapFontCache}. The position - * coincides with the top left corner of the first line's glyph. The method interprets new lines.
- *
- * You can specify the horizontal alignment of the text with the alignmentWidth and alignment - * parameters. The first parameter specifies the width of the rectangle the text should be aligned in (x to x + - * alignmentWidth). The second parameter specifies the alignment itself. - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param tint The color - * @param alignmentWidth The alignment width - * @param alignment The horizontal alignment + * Caches a string, which may contain newlines (\n), with the specified position and alignment. Each line is aligned + * horizontally within a rectangle of the specified width. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). + * @return The bounds of the cached string (the height is the distance from y to the baseline of the last line). */ - public void setMultiLineText (CharSequence str, float x, float y, Color tint, float alignmentWidth, HAlignment alignment) { + public TextBounds setMultiLineText (CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment) { BitmapFont font = this.font; int length = str.length(); reset(length); - final float color = tint.toFloatBits(); y += font.yOffset; int down = font.down; @@ -250,50 +226,41 @@ public class BitmapFontCache { xOffset = alignmentWidth - lineWidth; if (alignment == HAlignment.CENTER) xOffset /= 2; } - int lineWidth = addToCache(str, x + xOffset, y, color, start, lineEnd); + int lineWidth = addToCache(str, x + xOffset, y, start, lineEnd); maxWidth = Math.max(maxWidth, lineWidth); start = lineEnd + 1; y += down; numLines++; } - width = maxWidth; - height = font.capHeight + (numLines - 1) * font.lineHeight; + textBounds.width = maxWidth; + textBounds.height = font.capHeight + (numLines - 1) * font.lineHeight; + return textBounds; } /** - * Caches the given string at the given position with the given color in the provided {@link BitmapFontCache}. The position - * coincides with the top left corner of the first line's glyph. This method interprets new lines and causes the text to wrap - * at spaces based on the given wrapWidth. The wrapped text is left aligned. - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param tint The color - * @param wrapWidth The wrap width + * Caches a string, which may contain newlines (\n), with the specified position. Each line is automatically wrapped to keep it + * within a rectangle of the specified width. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). + * @return The bounds of the cached string (the height is the distance from y to the baseline of the last line). */ - public void setWrappedText (CharSequence str, float x, float y, Color tint, float wrapWidth) { - setWrappedText(str, x, y, tint, wrapWidth, HAlignment.LEFT); + public TextBounds setWrappedText (CharSequence str, float x, float y, float wrapWidth) { + return setWrappedText(str, x, y, wrapWidth, HAlignment.LEFT); } /** - * Caches the given string at the given position with the given color in the provided {@link BitmapFontCache}. The position - * coincides with the top left corner of the first line's glyph. This method interprets new lines and causes the text to wrap - * at spaces based on the given wrapWidth.
- *
- * You can specify the horizontal alignment of the text within the wrapWidth by using the alignment - * parameter. - * @param str The string - * @param x The x position of the left most character of the first line - * @param y The y position of the left most character's top left corner of the first line - * @param tint The color - * @param wrapWidth The wrap width + * Caches a string, which may contain newlines (\n), with the specified position. Each line is automatically wrapped to keep it + * within a rectangle of the specified width, and aligned horizontally within that rectangle. + * @param x The x position for the left most character. + * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). + * @return The bounds of the cached string (the height is the distance from y to the baseline of the last line). */ - public void setWrappedText (CharSequence str, float x, float y, Color tint, float wrapWidth, HAlignment alignment) { + public TextBounds setWrappedText (CharSequence str, float x, float y, float wrapWidth, HAlignment alignment) { BitmapFont font = this.font; int length = str.length(); reset(length); - final float color = tint.toFloatBits(); y += font.yOffset; int down = font.down; @@ -316,39 +283,34 @@ public class BitmapFontCache { xOffset = wrapWidth - lineWidth; if (alignment == HAlignment.CENTER) xOffset /= 2; } - int lineWidth = addToCache(str, x + xOffset, y, color, start, lineEnd); + int lineWidth = addToCache(str, x + xOffset, y, start, lineEnd); maxWidth = Math.max(maxWidth, lineWidth); start = lineEnd + 1; y += down; numLines++; } - width = maxWidth; - height = font.capHeight + (numLines - 1) * font.lineHeight; - } - - /** - * @return The width of the contained text - */ - public int getWidth () { - return width; + textBounds.width = maxWidth; + textBounds.height = font.capHeight + (numLines - 1) * font.lineHeight; + return textBounds; } /** - * @return The height of the contained text + * Returns the size of the cached string. The height is the distance from the top of most capital letters in the font (the + * {@link BitmapFont#getCapHeight() cap height}) to the baseline of the last line of text. */ - public int getHeight () { - return height; + public TextBounds getBounds () { + return textBounds; } /** - * @return The x coordinate of the contained text, relative to the position when the cached text was created + * Returns the x position of the cached string, relative to the position when the string was cached. */ public float getX () { return x; } /** - * @return The y coordinate of the contained text, relative to the position when the cached text was created + * Returns the y position of the cached string, relative to the position when the string was cached. */ public float getY () { return y; diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontAlignmentTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontAlignmentTest.java index bffb43a4f..f4a9d8b27 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontAlignmentTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontAlignmentTest.java @@ -84,7 +84,7 @@ public class BitmapFontAlignmentTest extends GdxTest { x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; - font.draw(spriteBatch, text, x, y, Color.WHITE); + font.draw(spriteBatch, text, x, y); } private void renderSingleLineCached () { @@ -95,10 +95,10 @@ public class BitmapFontAlignmentTest extends GdxTest { float height = logoSprite.getHeight(); // Obviously you wouldn't set the cache text every frame in real code. - cache.setMultiLineText(text, 0, 0, Color.WHITE); + TextBounds bounds = cache.setMultiLineText(text, 0, 0); - x += width / 2 - cache.getWidth() / 2; - y += height / 2 + cache.getHeight() / 2; + x += width / 2 - bounds.width / 2; + y += height / 2 + bounds.height / 2; cache.setPosition(x, y); cache.draw(spriteBatch); @@ -115,10 +115,10 @@ public class BitmapFontAlignmentTest extends GdxTest { x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; - font.drawWrapped(spriteBatch, text, x, y, Color.WHITE, width); + font.drawWrapped(spriteBatch, text, x, y, width); // Note that wrapped text can be aligned: - // font.drawWrapped(spriteBatch, text, x, y, Color.WHITE, width, HAlignment.CENTER); + // font.drawWrapped(spriteBatch, text, x, y, width, HAlignment.CENTER); } private void renderWrappedCached () { @@ -129,13 +129,13 @@ public class BitmapFontAlignmentTest extends GdxTest { float height = logoSprite.getHeight(); // Obviously you wouldn't set the cache text every frame in real code. - cache.setWrappedText(text, 0, 0, Color.WHITE, width); + TextBounds bounds = cache.setWrappedText(text, 0, 0, width); // Note that wrapped text can be aligned: - // cache.setWrappedText(text, 0, 0, Color.WHITE, width, HAlignment.CENTER); + // cache.setWrappedText(text, 0, 0, width, HAlignment.CENTER); - x += width / 2 - cache.getWidth() / 2; - y += height / 2 + cache.getHeight() / 2; + x += width / 2 - bounds.width / 2; + y += height / 2 + bounds.height / 2; cache.setPosition(x, y); cache.draw(spriteBatch); @@ -152,10 +152,10 @@ public class BitmapFontAlignmentTest extends GdxTest { x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; - font.drawMultiLine(spriteBatch, text, x, y, Color.WHITE); + font.drawMultiLine(spriteBatch, text, x, y); // Note that multi line text can be aligned: - // font.drawMultiLine(spriteBatch, text, x, y, Color.WHITE, width, HAlignment.CENTER); + // font.drawMultiLine(spriteBatch, text, x, y, width, HAlignment.CENTER); } private void renderMultiLineCached () { @@ -167,13 +167,13 @@ public class BitmapFontAlignmentTest extends GdxTest { float height = logoSprite.getHeight(); // Obviously you wouldn't set the cache text every frame in real code. - cache.setMultiLineText(text, 0, 0, Color.WHITE); + TextBounds bounds = cache.setMultiLineText(text, 0, 0); // Note that multi line text can be aligned: - // cache.setMultiLineText(text, 0, 0, Color.WHITE, width, HAlignment.CENTER); + // cache.setMultiLineText(text, 0, 0, width, HAlignment.CENTER); - x += width / 2 - cache.getWidth() / 2; - y += height / 2 + cache.getHeight() / 2; + x += width / 2 - bounds.width / 2; + y += height / 2 + bounds.height / 2; cache.setPosition(x, y); cache.draw(spriteBatch); diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontFlipTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontFlipTest.java index 2c9b3631c..45f05075c 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontFlipTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontFlipTest.java @@ -24,11 +24,9 @@ public class BitmapFontFlipTest extends GdxTest { private Color red = new Color(1, 0, 0, 0.5f); private BitmapFontCache cache1, cache2, cache3, cache4, cache5; int renderMode; - private float alpha; private InputProcessor inputProcessor; - @Override - public void create () { + @Override public void create () { spriteBatch = new SpriteBatch(); spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho(0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0, 0, 1)); @@ -55,24 +53,26 @@ public class BitmapFontFlipTest extends GdxTest { cache4 = new BitmapFontCache(font); cache5 = new BitmapFontCache(font); - cache1.setText("(cached)", 10, 320 - 66, Color.WHITE); + cache1.setText("(cached)", 10, 320 - 66); String text = "Sphinx of black quartz,\njudge my vow."; - cache2.setMultiLineText(text, 5, 320 - 300, Color.RED); + cache2.setColor(Color.RED); + cache2.setMultiLineText(text, 5, 320 - 300); text = "How quickly\ndaft jumping zebras vex."; - cache3.setMultiLineText(text, 5, 320 - 200, Color.BLUE, 470, BitmapFont.HAlignment.CENTER); + cache3.setColor(Color.BLUE); + cache3.setMultiLineText(text, 5, 320 - 200, 470, BitmapFont.HAlignment.CENTER); text = "Kerning: LYA moo"; - cache4.setText(text, 210, 320 - 66, Color.WHITE, 0, text.length() - 3); + cache4.setText(text, 210, 320 - 66, 0, text.length() - 3); text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze."; - cache5.setWrappedText(text, 0, 320 - 300, red, 480, HAlignment.CENTER); + cache5.setColor(red); + cache5.setWrappedText(text, 0, 320 - 300, 480, HAlignment.CENTER); } - @Override - public void render () { - alpha = (alpha + Gdx.graphics.getDeltaTime() * 0.1f) % 1; + @Override public void render () { + red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1; GL10 gl = Gdx.graphics.getGL10(); gl.glClear(GL10.GL_COLOR_BUFFER_BIT); @@ -86,36 +86,39 @@ public class BitmapFontFlipTest extends GdxTest { renderCached(); break; } - spriteBatch.end(); + spriteBatch.end(); } private void renderNormal () { String text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze."; - red.a = alpha; - font.drawWrapped(spriteBatch, text, 0, 320 - 300, red, 480, HAlignment.CENTER); + font.setColor(red); + font.drawWrapped(spriteBatch, text, 0, 320 - 300, 480, HAlignment.CENTER); - font.draw(spriteBatch, "(normal)", 10, 320 - 66, Color.WHITE); + font.setColor(Color.WHITE); + font.draw(spriteBatch, "(normal)", 10, 320 - 66); - if (alpha > 0.6f) return; + if (red.a > 0.6f) return; text = "Sphinx of black quartz,\njudge my vow."; - font.drawMultiLine(spriteBatch, text, 5, 320 - 300, Color.RED); + font.setColor(Color.RED); + font.drawMultiLine(spriteBatch, text, 5, 320 - 300); text = "How quickly\ndaft jumping zebras vex."; - font.drawMultiLine(spriteBatch, text, 5, 320 - 200, Color.BLUE, 470, BitmapFont.HAlignment.RIGHT); + font.setColor(Color.BLUE); + font.drawMultiLine(spriteBatch, text, 5, 320 - 200, 470, BitmapFont.HAlignment.RIGHT); text = "Kerning: LYA moo"; - font.draw(spriteBatch, text, 210, 320 - 66, Color.WHITE, 0, text.length() - 3); + font.setColor(Color.WHITE); + font.draw(spriteBatch, text, 210, 320 - 66, 0, text.length() - 3); } private void renderCached () { - red.a = alpha; cache5.setColor(red); cache5.draw(spriteBatch); cache1.draw(spriteBatch); - if (alpha > 0.6f) return; + if (red.a > 0.6f) return; cache2.draw(spriteBatch); cache3.draw(spriteBatch); diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontTest.java index 59a08eac1..a5971ee92 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontTest.java @@ -21,10 +21,9 @@ public class BitmapFontTest extends GdxTest { private SpriteBatch spriteBatch; private BitmapFont font; private Sprite logoSprite; - private Color red = new Color(1, 0, 0, 0.5f); + private Color red = new Color(1, 0, 0, 1); private BitmapFontCache cache1, cache2, cache3, cache4, cache5; int renderMode; - private float alpha; InputProcessor inputProcessor; @Override public void create () { @@ -59,23 +58,26 @@ public class BitmapFontTest extends GdxTest { cache4 = new BitmapFontCache(font); cache5 = new BitmapFontCache(font); - cache1.setText("(cached)", 10, 66, Color.WHITE); + cache1.setText("(cached)", 10, 66); String text = "Sphinx of black quartz,\njudge my vow."; - cache2.setMultiLineText(text, 5, 300, Color.RED); + cache2.setColor(Color.RED); + cache2.setMultiLineText(text, 5, 300); text = "How quickly\ndaft jumping zebras vex."; - cache3.setMultiLineText(text, 5, 200, Color.BLUE, 470, BitmapFont.HAlignment.CENTER); + cache3.setColor(Color.BLUE); + cache3.setMultiLineText(text, 5, 200, 470, BitmapFont.HAlignment.CENTER); text = "Kerning: LYA moo"; - cache4.setText(text, 210, 66, Color.WHITE, 0, text.length() - 3); + cache4.setText(text, 210, 66, 0, text.length() - 3); text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze."; - cache5.setWrappedText(text, 0, 300, red, 480, HAlignment.CENTER); + cache5.setColor(red); + cache5.setWrappedText(text, 0, 300, 480, HAlignment.CENTER); } @Override public void render () { - alpha = (alpha + Gdx.graphics.getDeltaTime() * 0.1f) % 1; + red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1; GL10 gl = Gdx.graphics.getGL10(); gl.glClear(GL10.GL_COLOR_BUFFER_BIT); @@ -94,31 +96,34 @@ public class BitmapFontTest extends GdxTest { private void renderNormal () { String text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze."; - red.a = alpha; - font.drawWrapped(spriteBatch, text, 0, 300, red, 480, HAlignment.CENTER); + font.setColor(red); + font.drawWrapped(spriteBatch, text, 0, 300, 480, HAlignment.CENTER); - font.draw(spriteBatch, "(normal)", 10, 66, Color.WHITE); + font.setColor(Color.WHITE); + font.draw(spriteBatch, "(normal)", 10, 66); - if (alpha > 0.6f) return; + if (red.a > 0.6f) return; text = "Sphinx of black quartz,\njudge my vow."; - font.drawMultiLine(spriteBatch, text, 5, 300, Color.RED); + font.setColor(Color.RED); + font.drawMultiLine(spriteBatch, text, 5, 300); text = "How quickly\ndaft jumping zebras vex."; - font.drawMultiLine(spriteBatch, text, 5, 200, Color.BLUE, 470, BitmapFont.HAlignment.RIGHT); + font.setColor(Color.BLUE); + font.drawMultiLine(spriteBatch, text, 5, 200, 470, BitmapFont.HAlignment.RIGHT); text = "Kerning: LYA moo"; - font.draw(spriteBatch, text, 210, 66, Color.WHITE, 0, text.length() - 3); + font.setColor(Color.WHITE); + font.draw(spriteBatch, text, 210, 66, 0, text.length() - 3); } private void renderCached () { - red.a = alpha; cache5.setColor(red); cache5.draw(spriteBatch); cache1.draw(spriteBatch); - if (alpha > 0.6f) return; + if (red.a > 0.6f) return; cache2.draw(spriteBatch); cache3.draw(spriteBatch); diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java index 2f29765e4..07e66cdae 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java @@ -82,6 +82,7 @@ public class Box2DTest extends GdxTest implements InputProcessor { // next we create a SpriteBatch and a font batch = new SpriteBatch(); font = new BitmapFont(); + font.setColor(Color.RED); // next we create out physics world. createPhysicsWorld(); @@ -205,7 +206,7 @@ public class Box2DTest extends GdxTest implements InputProcessor { // finally we render the time it took to update the world batch.begin(); font.draw( batch, "fps: " + Gdx.graphics.getFramesPerSecond() + " update time: " + updateTime, 0, - 20, Color.RED); + 20); batch.end(); } diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java index 6429d7013..61585bd38 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java @@ -264,7 +264,7 @@ public class FilesTest extends GdxTest { @Override public void render () { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.begin(); - font.drawMultiLine(batch, message, 20, Gdx.graphics.getHeight() - 20, Color.WHITE); + font.drawMultiLine(batch, message, 20, Gdx.graphics.getHeight() - 20); batch.end(); } diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/MD5Test.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/MD5Test.java index 111612140..b3c359271 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/MD5Test.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/MD5Test.java @@ -98,8 +98,7 @@ public class MD5Test extends GdxTest implements InputProcessor { batch.begin(); font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond() - + (renderer.isJniUsed() ? ", jni" : ", java") + ", render time: " + renderTime + ", skin time: " + skinTime, 10, 20, - Color.WHITE); + + (renderer.isJniUsed() ? ", jni" : ", java") + ", render time: " + renderTime + ", skin time: " + skinTime, 10, 20); batch.end(); } diff --git a/tests/gdx-tests/src/com/badlogic/gdx/tests/PixelsPerInchTest.java b/tests/gdx-tests/src/com/badlogic/gdx/tests/PixelsPerInchTest.java index dcd9dae7f..8e1166d13 100644 --- a/tests/gdx-tests/src/com/badlogic/gdx/tests/PixelsPerInchTest.java +++ b/tests/gdx-tests/src/com/badlogic/gdx/tests/PixelsPerInchTest.java @@ -33,7 +33,7 @@ public class PixelsPerInchTest extends GdxTest { float width = (int)(Gdx.graphics.getPpcX() * 2); float height = (int)(Gdx.graphics.getPpcY() * 1); batch.draw(texture, 10, 100, width, height, 0, 0, 64, 32, Color.WHITE, false, false ); - font.draw(batch, "button is 2x1 cm (" + width + "x" + height + "px), ppi: (" + Gdx.graphics.getPpiX() + "," + Gdx.graphics.getPpiY() +"), ppc: (" + Gdx.graphics.getPpcX() + "," + Gdx.graphics.getPpcY()+ ")", 10, 50, Color.WHITE); + font.draw(batch, "button is 2x1 cm (" + width + "x" + height + "px), ppi: (" + Gdx.graphics.getPpiX() + "," + Gdx.graphics.getPpiY() +"), ppc: (" + Gdx.graphics.getPpcX() + "," + Gdx.graphics.getPpcY()+ ")", 10, 50); batch.end(); } -- 2.11.0