Back to project page RageTextSample-Android-Java.
The source code is released under:
MIT License
If you think the Android project RageTextSample-Android-Java listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package de.ertlu.rob.ragesamplebase; //from w w w . ja v a 2s . co m import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLES20; import android.opengl.GLSurfaceView; import android.opengl.GLUtils; import android.opengl.Matrix; import java.io.IOException; import java.io.InputStream; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import de.ertlu.rob.Rage.Common.Rage; import de.ertlu.rob.Rage.Shader.Shader; import de.ertlu.rob.Rage.Text.Font; import de.ertlu.rob.Rage.Text.FontShader; import de.ertlu.rob.Rage.Text.Text; /** * @author Robert Lude : rob@ertlu.de */ public class RAGESampleRenderer implements GLSurfaceView.Renderer { private static final String TAG = RAGESampleRenderer.class.getName(); private static final float[] screenMatrix = new float[16]; private Font font; private Text textA; private Text textB; private Text textC; private Text textD; public void onSurfaceCreated(GL10 gl, EGLConfig config) { GLES20.glDisable(GLES20.GL_CULL_FACE); Rage.checkGLError(TAG, "onSurfaceCreated", "glDisable"); GLES20.glDisable(GLES20.GL_DEPTH_TEST); Rage.checkGLError(TAG, "onSurfaceCreated", "glDisable"); GLES20.glEnable(GLES20.GL_BLEND); Rage.checkGLError(TAG, "onSurfaceCreated", "glEnable"); GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA); Rage.checkGLError(TAG, "onSurfaceCreated", "glBlendFunc"); GLES20.glBlendEquation(GLES20.GL_FUNC_ADD); Rage.checkGLError(TAG, "onSurfaceCreated", "glBlendEquation"); GLES20.glClearColor(0, 0, 0, 1); Rage.checkGLError(TAG, "onSurfaceCreated", "glClearColor"); // This call lets the font shader know that the OpenGL context has been reset FontShader.resetInstance(); font = loadFont("Roboto-Medium"); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { calculateScreenMatrix(width, height); final float textSize = height * 0.1f; final String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut fringilla sit amet risus ut tempor. Sed lacinia enim vitae hendrerit sodales. Duis interdum vel nunc."; textA = new Text(font, loremIpsum, 0, 0, textSize, new float[]{1,0,0,1}); textB = textA.setPosition(textSize, textSize).setColor(new float[]{1,1,0,1}); textC = textA.setSize(textSize * 2).setPosition(0, textSize * 2).setColor(new float[]{0,1,0,1}); textD = textA.setText("Goodbye, tutorial!").setPosition(0, height - textSize).setColor(new float[]{0,0,1,1}); } @Override public void onDrawFrame(GL10 gl) { final String method = "onDrawFrame"; GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); Rage.checkGLError(TAG, method, "glClear"); if ( textA == null ) return; Shader.setViewMatrix(screenMatrix); textA.draw(); textB.draw(); textC.draw(); textD.draw(); } private void calculateScreenMatrix(final float width, final float height) { float[] a = new float[16]; float[] b = new float[16]; Matrix.orthoM(a, 0, 0, width, height, 0, -1, 50); Matrix.setLookAtM(b, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0); Matrix.multiplyMM(screenMatrix,0, a,0, b,0); } private static final Font loadFont(final String name) { // This is so we don't have to type the method name for every call to checkGLError // This also prevents method name mistakes on our part from copying and pasting code to a different method final String method = "loadFont"; // First, create an array to store the ids of the new textures // We only need one, but we still need an array. final int[] textureId = new int[1]; // This is the actual call that creates the new texture ids GLES20.glGenTextures(1, textureId, 0); Rage.checkGLError(TAG, method, "glGenTextures"); // We are going to assume everything went ok for the purposes of this tutorial // The BitmapFactory helper takes an object that is full of options for constructing the // bitmap. We are only concerned with 'inScaled' which determines whether or not the image // will be resized upon reading. We don't want that. BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); bitmapFactoryOptions.inScaled = false; // Try opening the bitmap asset. Remember, we're not really handling errors here properly InputStream inputStream; try { inputStream = RAGESampleActivity.instance.getAssets().open(name + ".png"); } catch (IOException e) { return null; } // Decode the stream into a bitmap in system memory Bitmap bitmap = BitmapFactory.decodeStream(inputStream); // Close the input stream now that we are done with it try { inputStream.close(); } catch (IOException e) { return null; } // Bind the texture we are loading and set some basic filtering parameters GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId[0]); Rage.checkGLError(TAG, method, "glBindTexture"); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); Rage.checkGLError(TAG, method, "glTexParameteri"); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); Rage.checkGLError(TAG, method, "glTexParameteri"); // Use GLUtils to copy the bitmap from system memory to GPU memory GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0); // Get rid of the system copy bitmap.recycle(); // Now we need to get a stream to the .glyphData file try { inputStream = RAGESampleActivity.instance.getAssets().open(name + ".glyphData"); } catch (IOException e) { return null; } // At last we can create the new Font object! Font result = new Font(textureId[0], inputStream); // Close up that .glyphData stream before we go... try { inputStream.close(); } catch (IOException e) { return null; } // Return the resulting font object return result; } }