1 package org.pileus.spades;
4 import java.util.HashMap;
6 import java.nio.ByteBuffer;
7 import java.nio.ByteOrder;
8 import java.nio.FloatBuffer;
10 import javax.microedition.khronos.egl.EGLConfig;
11 import javax.microedition.khronos.opengles.GL10;
13 import android.content.Context;
14 import android.content.res.Resources;
15 import android.graphics.Bitmap;
16 import android.graphics.BitmapFactory;
17 import android.opengl.GLES20;
18 import android.opengl.GLSurfaceView;
19 import android.opengl.GLUtils;
20 import android.opengl.Matrix;
21 import android.view.MotionEvent;
23 public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
26 private final String vertSource
27 = "uniform mat4 u_model;"
28 + "uniform mat4 u_view;"
29 + "uniform mat4 u_proj;"
30 + "attribute vec4 a_position;"
31 + "attribute vec2 a_mapping;"
32 + "varying vec2 v_mapping;"
34 + " gl_Position = u_proj"
38 + " v_mapping = a_mapping;"
41 private final String fragSource
42 = "precision mediump float;"
43 + "uniform sampler2D u_texture;"
44 + "uniform vec4 u_color;"
45 + "varying vec2 v_mapping;"
47 + " gl_FragColor = texture2D("
48 + " u_texture, v_mapping);"
52 private final float faceCoords[] = {
53 -0.063f, 0.088f, 0.05f, // Standard poker size:
54 -0.063f, -0.088f, 0.05f, // 2.5in x 3.5in
55 0.063f, -0.088f, 0.05f, // 63mm x 88mm
56 0.063f, 0.088f, 0.05f, //
59 private final float backCoords[] = {
60 0.063f, 0.088f, 0.05f, // Standard poker size:
61 0.063f, -0.088f, 0.05f, // 2.5in x 3.5in
62 -0.063f, -0.088f, 0.05f, // 63mm x 88mm
63 -0.063f, 0.088f, 0.05f, //
66 private final float tableCoords[] = {
73 private final float mapCoords[] = {
80 private final float color[] = {
85 private final String cards[] = {
86 "As", "Ks", "Qs", "Js", "10s", "9s", "8s", "7s", "6s", "5s", "4s", "3s", "2s",
87 "Ah", "Kh", "Qh", "Jh", "10h", "9h", "8h", "7h", "6h", "5h", "4h", "3h", "2h",
88 "Ac", "Kc", "Qc", "Jc", "10c", "9c", "8c", "7c", "6c", "5c", "4c", "3c", "2c",
89 "Ad", "Kd", "Qd", "Jd", "10d", "9d", "8d", "7d", "6d", "5d", "4d", "3d", "2d",
93 private Resources res; // app resources
94 private int program; // opengl program
96 private float[] model; // model matrix
97 private float[] view; // view matrix
98 private float[] proj; // projection matrix
100 private FloatBuffer faceBuf; // vertex positions for front of card
101 private FloatBuffer backBuf; // vertex positions for back of card
102 private FloatBuffer tableBuf; // vertex positions for table
103 private FloatBuffer mapBuf; // texture mapping coord buffer
105 private int modelHandle; // model matrix
106 private int viewHandle; // view matrix
107 private int projHandle; // projection matrix
108 private int vertHandle; // vertex positions
109 private int mapHandle; // texture mapping coords
110 private int texHandle; // texture data
111 private int colorHandle; // color data
113 private int[] face; // card face textures
114 private int red; // red card back
115 private int blue; // blue card back
116 private int table; // table top texture
118 private boolean drag; // currently in drag event
119 private int pick; // currently picked card
120 private float xpos; // x drag position (0=left - 1-right)
121 private float ypos; // y drag position (0=bottom - 1-top)
122 private float ylim; // y limit for a play
124 private Map<String,Integer> index; // card name to index map
127 public String[] hand; // cards to display
129 /* GLSurfaceView Methods */
130 public Cards(Context context)
133 Os.debug("Cards: create");
135 this.res = context.getResources();
137 this.model = new float[4*4];
138 this.view = new float[4*4];
139 this.proj = new float[4*4];
141 this.face = new int[52];
145 this.hand = new String[] {
146 "As", "7s", "6s", "6h", "2h", "Ac",
147 "Kc", "3c", "10d", "9d", "8d", "7d", "2d"
150 this.index = new HashMap<String,Integer>(52);
151 for (int i = 0; i < 52; i++)
152 this.index.put(this.cards[i], i);
154 this.setEGLContextClientVersion(2);
155 this.setRenderer(this);
156 this.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
159 /* Renderer methods */
161 public void onSurfaceCreated(GL10 unused, EGLConfig config)
163 Os.debug("Cards: onSurfaceCreate");
165 /* Initialize shaders */
166 int vertShader = this.loadShader(GLES20.GL_VERTEX_SHADER, vertSource);
167 int fragShader = this.loadShader(GLES20.GL_FRAGMENT_SHADER, fragSource);
169 /* Link shaders into an OpenGL program */
170 this.program = GLES20.glCreateProgram();
171 GLES20.glAttachShader(program, vertShader);
172 GLES20.glAttachShader(program, fragShader);
173 GLES20.glLinkProgram(program);
175 /* Get shaders attributes */
176 this.modelHandle = GLES20.glGetUniformLocation(program, "u_model");
177 this.viewHandle = GLES20.glGetUniformLocation(program, "u_view");
178 this.projHandle = GLES20.glGetUniformLocation(program, "u_proj");
179 this.vertHandle = GLES20.glGetAttribLocation(program, "a_position");
180 this.mapHandle = GLES20.glGetAttribLocation(program, "a_mapping");
181 this.texHandle = GLES20.glGetUniformLocation(program, "u_texture");
182 this.colorHandle = GLES20.glGetUniformLocation(program, "u_color");
184 /* Create vertex array */
185 this.faceBuf = this.loadBuffer(this.faceCoords);
186 this.backBuf = this.loadBuffer(this.backCoords);
187 this.tableBuf = this.loadBuffer(this.tableCoords);
188 this.mapBuf = this.loadBuffer(this.mapCoords);
191 for (int i = 0; i < 52; i++) {
192 String name = "card_" + this.cards[i].toLowerCase();
193 this.face[i] = this.loadTexture(name);
195 this.red = this.loadTexture("card_red");
196 this.blue = this.loadTexture("card_blue");
197 this.table = this.loadTexture("table");
200 Os.debug("Cards: onSurfaceCreate");
204 public void onDrawFrame(GL10 unused)
206 Os.debug("Cards: onDrawFrame");
208 /* Turn on the program */
209 GLES20.glUseProgram(program);
212 GLES20.glClearColor(0, 0, 0, 1);
213 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
215 /* Setup projection matricies */
216 GLES20.glUniformMatrix4fv(this.viewHandle, 1, false, this.view, 0);
217 GLES20.glUniformMatrix4fv(this.projHandle, 1, false, this.proj, 0);
219 /* Setup buffers objects */
220 GLES20.glEnableVertexAttribArray(this.vertHandle);
221 GLES20.glEnableVertexAttribArray(this.mapHandle);
223 /* Setup texturing */
224 GLES20.glEnable(GLES20.GL_CULL_FACE);
225 GLES20.glEnable(GLES20.GL_BLEND);
226 GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA);
227 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
228 GLES20.glUniform1i(this.texHandle, 0);
237 public void onSurfaceChanged(GL10 unused, int width, int height)
239 Os.debug("Cards: onSurfaceChanged");
241 GLES20.glViewport(0, 0, width, height);
243 Matrix.setIdentityM(this.model, 0);
244 Matrix.setIdentityM(this.view, 0);
245 Matrix.setIdentityM(this.proj, 0);
249 float yang = xang * ((float)height / (float)width);
251 Matrix.frustumM(this.proj, 0,
252 -1E-6f * xang, // left
253 1E-6f * xang, // right
254 -1E-6f * yang, // bottom
259 Matrix.rotateM(this.view, 0, 10f, 1, 0, 0);
260 Matrix.translateM(this.view, 0, 0, 0, -1.5f);
261 Matrix.rotateM(this.view, 0, -45f, 1, 0, 0);
265 public boolean onTouchEvent(MotionEvent event)
267 boolean up = event.getActionMasked() == MotionEvent.ACTION_UP;
269 float x = event.getX() / this.getWidth();
270 float y = 1-(event.getY() / this.getHeight());
274 int num = this.hand.length;
276 this.pick = (int)Math.floor((x*num));
277 if (this.pick < 0) this.pick = 0;
278 if (this.pick >= num) this.pick = num-1;
280 if (y < this.ylim && !this.drag) {
281 Os.debug("Cards: onTouchEvent - starting drag");
285 Os.debug("Cards: onTouchEvent - move "
287 this.requestRender();
289 if (y >= this.ylim && this.drag && up) {
290 Os.debug("Cards: onTouchEvent - playing card");
293 Os.debug("Cards: onTouchEvent - ending drag");
299 /* Private loading methods */
300 private int loadShader(int type, String code)
302 Os.debug("Cards: loadShader");
304 int shader = GLES20.glCreateShader(type);
305 GLES20.glShaderSource(shader, code);
306 GLES20.glCompileShader(shader);
310 private int loadTexture(String name)
312 Os.debug("Cards: loadTexture - " + name);
314 final int[] tex = new int[1];
316 /* Lookup the resource ID */
319 id = R.drawable.class.getField(name).getInt(null);
320 } catch(Exception e) {
321 Os.debug("Cards: lookup failed for '" + name + "'", e);
325 /* Load the bitmap */
326 Bitmap bitmap = BitmapFactory.decodeResource(this.res, id);
328 /* Copy into OpenGL */
329 GLES20.glGenTextures(1, tex, 0);
330 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex[0]);
331 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
332 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
333 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
338 private FloatBuffer loadBuffer(float[] data)
340 ByteBuffer bytes = ByteBuffer.allocateDirect(data.length * 4);
341 bytes.order(ByteOrder.nativeOrder());
343 FloatBuffer buf = bytes.asFloatBuffer();
350 /* Private drawing methods */
351 private void drawTable()
354 Matrix.setIdentityM(this.model, 0);
355 GLES20.glUniformMatrix4fv(this.modelHandle, 1, false, this.model, 0);
358 GLES20.glVertexAttribPointer(this.vertHandle, 3, GLES20.GL_FLOAT, false, 3*4, this.tableBuf);
359 GLES20.glVertexAttribPointer(this.mapHandle, 2, GLES20.GL_FLOAT, false, 2*4, this.mapBuf);
360 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.table);
361 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
365 private void drawHand()
368 int num = this.hand.length;
369 for (int i = 0; i < num; i++) {
370 if (this.drag && this.ypos >= this.ylim && i == this.pick)
373 Matrix.setIdentityM(this.model, 0);
375 Matrix.rotateM(this.model, 0, 45f, 1f, 0f, 0f);
376 Matrix.translateM(this.model, 0, 0f, -0.3f, 1.20f);
379 float pct = (float)(i+0.5) / num;
380 float err = this.xpos - pct;
381 float y = (float)this.ypos / this.ylim;
382 float lim = Math.min(Math.max(y,0),1);
384 * (float)Math.exp(-10*num*Math.pow(y*err,2))
385 * (1f-(float)Math.pow(1-lim, 2));
386 Matrix.translateM(this.model, 0, 0, fcn, 0);
389 float left = -20f + 20f*(1f/num);
390 float right = 54f - 54f*(1f/num);
391 float ang = left + i*(right-left)/num;
392 Matrix.rotateM(this.model, 0, ang, 0f, 0f, -1f);
393 Matrix.translateM(this.model, 0, 0f, 0.15f, 0f);
395 GLES20.glUniformMatrix4fv(this.modelHandle, 1, false, this.model, 0);
396 this.drawCard(this.hand[i]);
400 private void drawPick()
402 /* Draw selected card */
403 if (this.drag && this.ypos >= this.ylim) {
404 Matrix.setIdentityM(this.model, 0);
405 Matrix.rotateM(this.model, 0, 45f, 1f, 0f, 0f);
406 Matrix.translateM(this.model, 0, 0f, 0f, 1.20f);
407 GLES20.glUniformMatrix4fv(this.modelHandle, 1, false, this.model, 0);
408 this.drawCard(this.hand[this.pick]);
412 private void drawCard(String name)
414 int idx = this.index.get(name);
415 int front = this.face[idx];
419 GLES20.glVertexAttribPointer(this.vertHandle, 3, GLES20.GL_FLOAT, false, 3*4, this.faceBuf);
420 GLES20.glVertexAttribPointer(this.mapHandle, 2, GLES20.GL_FLOAT, false, 2*4, this.mapBuf);
421 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, front);
422 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
425 GLES20.glVertexAttribPointer(this.vertHandle, 3, GLES20.GL_FLOAT, false, 3*4, this.backBuf);
426 GLES20.glVertexAttribPointer(this.mapHandle, 2, GLES20.GL_FLOAT, false, 2*4, this.mapBuf);
427 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, back);
428 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);