Permalink
Browse files

Moved the digit drawing logic into a different class.

  • Loading branch information...
1 parent a776007 commit e9acbe2b609019d1a050da1f17e46aaba90495be @sofal committed Mar 23, 2011
Showing with 195 additions and 181 deletions.
  1. +6 −181 src/com/rmphelps/counter/CounterRenderer.java
  2. +189 −0 src/com/rmphelps/counter/Numbers.java
@@ -1,20 +1,12 @@
package com.rmphelps.counter;
import android.content.Context;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.graphics.Canvas;
-import android.graphics.Paint;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
-import android.opengl.GLUtils;
import android.view.View;
import android.view.View.OnClickListener;
-import java.io.InputStream;
-import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import java.nio.IntBuffer;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
@@ -26,17 +18,13 @@
private Context _context;
private FloatBuffer _verts;
- private FloatBuffer _tex;
- // texture pointer
- private int[] _textures = new int[10];
private int _digit;
- private static final int WIDTH = 320;
- private static final float WIDTH_F = WIDTH;
- private static final int HEIGHT = 400;
- private static final float HEIGHT_F = HEIGHT;
- private static final float TEX_SIZE = 512f;
+ protected static final int WIDTH = 320;
+ protected static final float WIDTH_F = WIDTH;
+ protected static final int HEIGHT = 400;
+ protected static final float HEIGHT_F = HEIGHT;
public CounterRenderer(Context context) {
@@ -49,146 +37,20 @@ public CounterRenderer(Context context) {
WIDTH_F, HEIGHT_F,
};
- float numWidth = 117.0f;
- float numHeight = 172.0f;
-
- // texture dimensions normalized to the texture size
- float normWidth = numWidth / TEX_SIZE;
- float normHeight = numHeight / TEX_SIZE;
-
- // Yes, I am hardcoding these. You got any better ideas?
- // These are the column splitter coordinates for numbers.png
- float[] cols = {
- 115, 234, 358, 485,
- };
-
- // The row splitter coordinates for numbers.png
- float[] rows = {
- 172, 344, 516,
- };
-
- // normalized columns
- float[] ncols = new float[cols.length];
- for (int i = 0; i < ncols.length; i++) {
- ncols[i] = cols[i] / TEX_SIZE;
- }
-
- // normalized rows
- float[] nrows = new float[rows.length];
- for (int i = 0; i < nrows.length; i++) {
- nrows[i] = rows[i] / TEX_SIZE;
- }
-
-
- float[][] tex = {
- // 0
- {
- ncols[0], nrows[1],
- ncols[0], nrows[2],
- ncols[1], nrows[1],
- ncols[1], nrows[2],
- },
- // 1
- {
- 0, 0,
- 0, nrows[0],
- ncols[0], 0,
- ncols[0], nrows[0],
- },
- // 2
- {
- ncols[0], 0,
- ncols[0], nrows[0],
- ncols[1], 0,
- ncols[1], nrows[0],
- },
- // 3
- {
- ncols[1], 0,
- ncols[1], nrows[0],
- ncols[2], 0,
- ncols[2], nrows[0],
- },
- // 4
- {
- ncols[2], 0,
- ncols[2], nrows[0],
- ncols[3], 0,
- ncols[3], nrows[0],
- },
- // 5
- {
- 0, nrows[0],
- 0, nrows[1],
- ncols[0], nrows[0],
- ncols[0], nrows[1],
- },
- // 6
- {
- ncols[0], nrows[0],
- ncols[0], nrows[1],
- ncols[1], nrows[0],
- ncols[1], nrows[1],
- },
- // 7
- {
- ncols[1], nrows[0],
- ncols[1], nrows[1],
- ncols[2], nrows[0],
- ncols[2], nrows[1],
- },
- // 8
- {
- ncols[2], nrows[0],
- ncols[2], nrows[1],
- ncols[3], nrows[0],
- ncols[3], nrows[1],
- },
- // 9
- {
- 0, nrows[1],
- 0, nrows[2],
- ncols[0], nrows[1],
- ncols[0], nrows[2],
- },
- };
ByteBuffer vbb = ByteBuffer.allocateDirect(verts.length * 4);
vbb.order(ByteOrder.nativeOrder());
_verts = vbb.asFloatBuffer();
_verts.put(verts);
_verts.position(0);
-
- vbb = ByteBuffer.allocateDirect(tex.length * 8 * 4);
- vbb.order(ByteOrder.nativeOrder());
- _tex = vbb.asFloatBuffer();
- for (float[] coords : tex) {
- _tex.put(coords);
- }
- _tex.position(0);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
-
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
- //gl.glBindTexture(GL10.GL_TEXTURE_2D, _textures[_digit]);
- gl.glBindTexture(GL10.GL_TEXTURE_2D, _textures[0]);
-
- gl.glMatrixMode(GL10.GL_MODELVIEW);
-
- gl.glLoadIdentity();
- gl.glVertexPointer(2, gl.GL_FLOAT, 0, _verts);
- _tex.position(_digit * 8);
- gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, _tex);
- gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, 4);
-
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
+ Numbers.drawDigit(gl, _digit, _verts);
}
@@ -209,7 +71,7 @@ public void onSurfaceChanged(GL10 gl, int width, int height) {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- generateTextures(gl);
+ Numbers.initTextures(gl, _context);
gl.glViewport(0, 0, WIDTH, HEIGHT);
/*
@@ -237,43 +99,6 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
- private void generateTextures(GL10 gl) {
- //Get the texture from the Android resource directory
- InputStream is = _context.getResources().openRawResource(R.drawable.numbers);
- Bitmap bitmap = null;
- try {
- bitmap = BitmapFactory.decodeStream(is);
- } finally {
- //Always clear and close
- try {
- is.close();
- is = null;
- } catch (IOException e) {
- }
- }
-
-
- // Generate one texture pointer
- gl.glGenTextures(1, _textures, 0);
- // and bind it to our array
- gl.glBindTexture(GL10.GL_TEXTURE_2D, _textures[0]);
-
- //Create Nearest Filtered Texture
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
-
- //Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
-
- //Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
- GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
-
- //Clean up
- bitmap.recycle();
- }
-
-
@Override
public void onClick(View v) {
_digit = (_digit + 1) % 10;
Oops, something went wrong.

0 comments on commit e9acbe2

Please sign in to comment.