/Research/Example projects/android2.1_TestOGL/src/com/example/atogl/BitmapFont.java

https://bitbucket.org/DeveloperUX/behaviortree · Java · 72 lines · 53 code · 19 blank · 0 comment · 3 complexity · 7da56d76eafc269ab81ee839e16387c2 MD5 · raw file

  1. package com.example.atogl;
  2. import java.nio.FloatBuffer;
  3. import javax.microedition.khronos.opengles.GL10;
  4. import javax.microedition.khronos.opengles.GL11Ext;
  5. public class BitmapFont {
  6. int font_tex;
  7. FloatBuffer fontTex;
  8. FloatBuffer face;
  9. public BitmapFont(int texture) {
  10. this.font_tex = texture;
  11. float width = 1f/16f;
  12. float height = width;
  13. float[] fontTexCoords = new float[16*16*8];
  14. int ix = 0;
  15. for (int row = 1; row <= 16; ++row) {
  16. for(int col = 1; col <= 16; ++col) {
  17. fontTexCoords[ix++] = col*width;
  18. fontTexCoords[ix++] = 1-row*height + 0.01f;
  19. fontTexCoords[ix++] = col*width;
  20. fontTexCoords[ix++] = 1-(row-1)*height - 0.01f;
  21. fontTexCoords[ix++] = (col-1)*width;
  22. fontTexCoords[ix++] = 1-row*height + 0.01f;
  23. fontTexCoords[ix++] = (col-1)*width;
  24. fontTexCoords[ix++] = 1-(row-1)*height - 0.01f;
  25. }
  26. }
  27. fontTex = GLTutorialBase.makeFloatBuffer(fontTexCoords);
  28. float faceVerts[] = new float[] {
  29. 0.5f, -0.5f, 0.5f,
  30. 0.5f, 0.5f, 0.5f,
  31. -0.5f, -0.5f, 0.5f,
  32. -0.5f, 0.5f, 0.5f,
  33. };
  34. face = GLTutorialBase.makeFloatBuffer(faceVerts);
  35. }
  36. public void draw(GL10 gl, String str) {
  37. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, face);
  38. gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  39. gl.glEnable(GL10.GL_TEXTURE_2D);
  40. gl.glBindTexture(GL10.GL_TEXTURE_2D, font_tex);
  41. gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  42. char[] chars = str.toCharArray();
  43. gl.glTranslatef(-chars.length*0.5f, 0, 0);
  44. for (int i = 0; i < chars.length; ++i) {
  45. fontTex.position(chars[i]*8);
  46. gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, fontTex);
  47. gl.glColor4f(1.0f, 1, 1, 1.0f);
  48. gl.glNormal3f(0,0,1);
  49. gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  50. gl.glTranslatef(1f, 0, 0);
  51. }
  52. }
  53. }