OSDN Git Service

[changed] More BitmapFont refactoring. It is getting very nice now!
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sat, 27 Nov 2010 02:51:19 +0000 (02:51 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sat, 27 Nov 2010 02:51:19 +0000 (02:51 +0000)
extensions/particle-editor/src/com/badlogic/gdx/graphics/particles/ParticleEditor.java
extensions/twl/gdx-twl/src/com/badlogic/gdx/twl/renderer/GdxFont.java
gdx/src/com/badlogic/gdx/graphics/BitmapFont.java
gdx/src/com/badlogic/gdx/graphics/BitmapFontCache.java
tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontAlignmentTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontFlipTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/BitmapFontTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/Box2DTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/FilesTest.java
tests/gdx-tests/src/com/badlogic/gdx/tests/MD5Test.java
tests/gdx-tests/src/com/badlogic/gdx/tests/PixelsPerInchTest.java

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