Depends on your use case. In my case I was rendering UI and scores in full screen OpenGL games. The scores changed frequently, so rendering them to a bitmap and uploading that as a texture would be terrible for the frame rate compared to never having to change the textures. Similarly, overlaying standard Android UI stuff over the GLSurfaceView hurt the frame rate as well; weird layer locking messages in the logs.
On Mar 22, 5:22 am, a a <[email protected]> wrote: > sorry, it seams better and light to use canvas to draw the text on the > bitmap. anyway, thanks for your help. > > 2011/3/22 Lance Nanek <[email protected]>: > > > I just draw a character at a time, so I can draw anything. I use > > char[] instead of String so I can preallocate and avoid garbage > > collection. You asked for code, so here it is. Mine's horrendously > > ugly, but I wrote it once and it never needed much revising, so I > > never really needed to clean it up. Anyway the methods look like this: > > > /** > > * Draw a character array. > > * > > * @param chars char[] to draw > > * @param offset int offset into the array specifying where to start > > * @param count int number of characters to draw from the array > > * @param startX int x position to start drawing from > > * @param bottom int y position of bottom of first line of text > > * @param alignedLeft boolean true to draw text toward the right, > > false to draw toward the left > > * @return y position to use to draw a line below the drawn text > > */ > > public int drawChars(final char[] chars, final int offset, final int > > count, > > final Text font, final int startX, final int bottom, > > final boolean alignedLeft, final boolean nextLineDown, > > final int scaleFP, final byte alpha) { > > > final int scaledFontHeightFP = FP.Mul(scaleFP, > > font.mHeightFP); > > final int scaledFontWidthFP = FP.Mul(scaleFP, font.mWidthFP); > > final int scaledFontHeightDifference = scaledFontHeightFP - > > font.mHeightFP; > > > final int lineIncrement = scaledFontHeightFP; > > > final int limit = offset + count; > > int drawingX = startX; > > int drawingBottom = bottom - scaledFontHeightDifference; > > > final int charsStart = alignedLeft ? offset : limit - 1; > > final int charsEnd = alignedLeft ? limit : offset - 1; > > final int charsInc = alignedLeft ? 1 : -1; > > > for(int i = charsStart; i != charsEnd; i += charsInc) { > > final char c = chars[i]; > > if ( '\n' == c ) { > > if ( LOG ) Log.i(TAG, "Line return detected, > > bottom = " + bottom); > > drawingBottom -= lineIncrement; > > if ( LOG ) Log.i(TAG, "Line return detected, > > bottom = " + bottom); > > drawingX = startX; > > > continue; > > } > > > final int charTexture = font.texture(c); > > > if ( !alignedLeft ) { > > drawingX -= scaledFontWidthFP; > > } > > > quad(drawingX, drawingBottom, scaledFontWidthFP, > > scaledFontHeightFP, font.mAtlas, charTexture, alpha); > > > if ( alignedLeft ) { > > drawingX += scaledFontWidthFP; > > } > > } > > > drawingBottom -= lineIncrement; > > if ( nextLineDown ) { > > return drawingBottom - lineIncrement; > > } > > > return drawingBottom + lineIncrement + scaledFontHeightFP; > > } > > > public int drawChars(final char[] chars, final int offset, final int > > count, > > final Text font, > > final int startX, final int bottom, final boolean > > alignedLeft, final > > boolean nextLineDown) { > > > return drawChars(chars, offset, count, font, startX, bottom, > > alignedLeft, nextLineDown, FP.ONE, ColorBytes.COMPONENT_MAX); > > } > > > public void number(final int number, final Text font, final int > > startX, final int bottom, > > final boolean alignedLeft) { > > > number(number, font, startX, bottom, alignedLeft, FP.ONE); > > } > > > public void number(final int number, final Text font, final int > > startX, final int bottom, > > final boolean alignedLeft, final int scaleFP) { > > > int numberStart = CharUtil.prepend(number, > > charBuffer.length, 1, > > charBuffer); > > > int count = charBuffer.length - numberStart; > > drawChars(charBuffer, numberStart, count, font, startX, > > bottom, > > alignedLeft, true, scaleFP, ColorBytes.COMPONENT_MAX); > > } > > > public void quad(final int left, final int bottom, final Atlas atlas, > > final int texture) { > > quad(left, bottom, atlas.widthFP[texture], > > atlas.heightFP[texture], > > atlas, texture, ColorBytes.COMPONENT_MAX); > > } > > > public void quad(final int left, final int bottom, final Atlas atlas, > > final int texture, boolean flipHorizontal) { > > final int textureWFP = atlas.widthFP[texture]; > > final int adjustedLeft = flipHorizontal ? left + textureWFP > > : left; > > final int wFP = flipHorizontal ? -textureWFP : textureWFP; > > quad(adjustedLeft, bottom, wFP, atlas.heightFP[texture], > > atlas, > > texture, ColorBytes.COMPONENT_MAX); > > } > > > public void quad(final int leftFP, final int bottomFP, final int wFP, > > final int hFP, final Atlas atlas, final int texture, final byte alpha) > > { > > > ensureCapacity(VERTS_PER_QUAD); > > > int dimsOffset = vertCount * DIMS_PER_VERT; > > > final int leftRoundedFP = mRoundVertexPositions ? > > FP.round(leftFP) : > > leftFP; > > final int bottomAdjustedFP = mVerticalOffset + bottomFP; > > final int bottomAdjustedRoundedFP = mRoundVertexPositions ? > > FP.round(bottomAdjustedFP) : bottomAdjustedFP; > > > final int rightFP = leftRoundedFP + wFP; > > final int topAdjustedFP = bottomAdjustedRoundedFP + hFP; > > final int zFP = 0; > > > //Top left. > > dimsOffset = addVertexDims(dimsOffset, topAdjustedFP, > > leftRoundedFP, > > zFP); > > > //Bottom left. > > dimsOffset = addVertexDims(dimsOffset, > > bottomAdjustedRoundedFP, > > leftRoundedFP, zFP); > > > //Top right. > > dimsOffset = addVertexDims(dimsOffset, topAdjustedFP, > > rightFP, zFP); > > > //Bottom right. > > dimsOffset = addVertexDims(dimsOffset, > > bottomAdjustedRoundedFP, > > rightFP, zFP); > > > int colorsOffset = vertCount * BYTES_PER_COLOR; > > for( int i = 0; i < VERTS_PER_QUAD; i++ ) { > > mColors[colorsOffset++] = ColorBytes.COMPONENT_MAX; > > mColors[colorsOffset++] = ColorBytes.COMPONENT_MAX; > > mColors[colorsOffset++] = ColorBytes.COMPONENT_MAX; > > mColors[colorsOffset++] = alpha; > > } > > > final int texCoordsOffset = vertCount * TEX_COORDS_PER_VERT; > > System.arraycopy(atlas.coords, texture*TEX_COORDS_PER_QUAD, > > texCoords, texCoordsOffset, TEX_COORDS_PER_QUAD); > > > vertCount += VERTS_PER_QUAD; > > } > > > I posted a simplified version of the class they are in here: > >https://github.com/lnanek/SpriteMethodTest2/blob/master/src/com/andro... > > > The Text and Atlas classes were removed for the simplified version, > > but are used in the character drawing version. They just hold data > > about what characters for a particular font are available, and what > > images are inside a particular texture atlas. My texture atlases are > > generated by a script that glues all the separate images into big ones > > and also generates the Atlas subclasses with where they are in the big > > images (denoted by constants in the subclasses that say where in the > > data arrays in the superclass the data for them is). Anyway, they look > > like this: > > > public class Text { > > > private static final int EXTRA_LINE_SPACING = 2; > > > public int mFirstTexture; > > > public char mFirst; > > > public int mCount; > > > public int mWidthFP; > > > public int mHeightFP; > > > public Atlas mAtlas; > > > public Text(Atlas atlas, int firstTexture, char firstChar, int count) > > { > > mAtlas = atlas; > > mFirstTexture = firstTexture; > > mWidthFP = atlas.widthFP[firstTexture]; > > mHeightFP = atlas.heightFP[firstTexture] + > > EXTRA_LINE_SPACING; > > mFirst = firstChar; > > mCount = count; > > } > > > public int texture(char c) { > > return mFirstTexture + ( c - mFirst ); > > } > > > } > > > public abstract class Atlas { > > public int resourceID; > > public int[] coords; > > public int[] widthFP; > > public int[] heightFP; > > public int[] width; > > public int[] height; > > > public int textureID; > > > public Atlas(int resourceID, int[] > > ... > > read more » -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

