[android-developers] Re: write tag with opengl on android
If text is static then you need texture (picture) with label "Dog", apply it to a quad (two triangles that form up a rectangle). On Mar 21, 11:48 am, a a wrote: > Hi all, > > How can i write a tag like string "Dog" on the picture. Can anyone > paste his/her code on here? -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en
[android-developers] Re: write tag with opengl on android
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, fin
[android-developers] Re: write tag with opengl on android
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 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 : > > > 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, n
Re: [android-developers] Re: write tag with opengl on android
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 : > 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 :