PageRenderTime 75ms CodeModel.GetById 50ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

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