+ /* Shader data */
+ private final String vertSource
+ = "uniform mat4 u_model;"
+ + "uniform mat4 u_view;"
+ + "uniform mat4 u_proj;"
+ + "attribute vec4 a_position;"
+ + "attribute vec2 a_mapping;"
+ + "varying vec2 v_mapping;"
+ + "void main() {"
+ + " gl_Position = u_proj"
+ + " * u_model"
+ + " * u_view"
+ + " * a_position;"
+ + " v_mapping = a_mapping;"
+ + "}";
+
+ private final String fragSource
+ = "precision mediump float;"
+ + "uniform sampler2D u_texture;"
+ + "uniform vec4 u_color;"
+ + "varying vec2 v_mapping;"
+ + "void main() {"
+ + " gl_FragColor = texture2D("
+ + " u_texture, v_mapping);"
+ + "}";
+
+ /* Drawing data */
+ private final float vertCoords[] = {
+ -0.063f, 0.088f, 0f, // Standard poker size:
+ -0.063f, -0.088f, 0f, // 2.5in x 3.5in
+ 0.063f, -0.088f, 0f, // 63mm x 88mm
+ 0.063f, 0.088f, 0f, //
+ };
+
+ private final float mapCoords[] = {
+ 0.0f, 0.0f,
+ 0.0f, 1.0f,
+ 1.0f, 1.0f,
+ 1.0f, 0.0f,
+ };
+
+ private final float color[] = {
+ 1, 0, 0, 1
+ };
+
+ /* Cards data */
+ private final String cards[] = {
+ "As", "Ks", "Qs", "Js", "10s", "9s", "8s", "7s", "6s", "5s", "4s", "3s", "2s",
+ "Ah", "Kh", "Qh", "Jh", "10h", "9h", "8h", "7h", "6h", "5h", "4h", "3h", "2h",
+ "Ac", "Kc", "Qc", "Jc", "10c", "9c", "8c", "7c", "6c", "5c", "4c", "3c", "2c",
+ "Ad", "Kd", "Qd", "Jd", "10d", "9d", "8d", "7d", "6d", "5d", "4d", "3d", "2d",
+ };
+
+ /* Private data */
+ private Resources res; // app resources
+ private int program; // opengl program
+
+ private float model[]; // model matrix
+ private float view[]; // view matrix
+ private float proj[]; // projection matrix
+
+ private FloatBuffer vertBuf; // vertex position buffer
+ private FloatBuffer mapBuf; // texture mapping coord buffer
+
+ private int modelHandle; // model matrix
+ private int viewHandle; // view matrix
+ private int projHandle; // projection matrix
+ private int vertHandle; // vertex positions
+ private int mapHandle; // texture mapping coords
+ private int texHandle; // texture data
+ private int colorHandle; // color data
+
+ private int face[]; // card face textures
+ private int red; // red card back
+ private int blue; // blue card back
+
+ /* GLSurfaceView Methods */
+ public Cards(Context context)
+ {
+ super(context);
+ Os.debug("Cards: create");
+
+ this.res = context.getResources();
+
+ this.model = new float[4*4];
+ this.view = new float[4*4];
+ this.proj = new float[4*4];
+
+ this.face = new int[52];
+
+ this.setEGLContextClientVersion(2);
+ this.setRenderer(this);
+ this.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
+ }
+
+ /* Renderer methods */
+ @Override
+ public void onSurfaceCreated(GL10 unused, EGLConfig config)
+ {
+ Os.debug("Cards: onSurfaceCreate");
+
+ /* Initialize shaders */
+ int vertShader = this.loadShader(GLES20.GL_VERTEX_SHADER, vertSource);
+ int fragShader = this.loadShader(GLES20.GL_FRAGMENT_SHADER, fragSource);
+
+ /* Link shaders into an OpenGL program */
+ this.program = GLES20.glCreateProgram();
+ GLES20.glAttachShader(program, vertShader);
+ GLES20.glAttachShader(program, fragShader);
+ GLES20.glLinkProgram(program);
+
+ /* Get shaders attributes */
+ this.modelHandle = GLES20.glGetUniformLocation(program, "u_model");
+ this.viewHandle = GLES20.glGetUniformLocation(program, "u_view");
+ this.projHandle = GLES20.glGetUniformLocation(program, "u_proj");
+ this.vertHandle = GLES20.glGetAttribLocation(program, "a_position");
+ this.mapHandle = GLES20.glGetAttribLocation(program, "a_mapping");
+ this.texHandle = GLES20.glGetUniformLocation(program, "u_texture");
+ this.colorHandle = GLES20.glGetUniformLocation(program, "u_color");
+
+ /* Create vertex array */
+ this.vertBuf = this.loadBuffer(this.vertCoords);
+ this.mapBuf = this.loadBuffer(this.mapCoords);
+
+ /* Load textures */
+ for (int i = 0; i < 52; i++) {
+ String name = "card_" + this.cards[i].toLowerCase();
+ this.face[i] = this.loadTexture(name);
+ }
+ this.red = this.loadTexture("card_red");
+ this.blue = this.loadTexture("card_blue");
+
+ /* Debug */
+ Os.debug("Cards: onSurfaceCreate");
+ }
+
+ @Override
+ public void onDrawFrame(GL10 unused)