1 package org.pileus.spades;
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
5 import java.nio.FloatBuffer;
7 import javax.microedition.khronos.egl.EGLConfig;
8 import javax.microedition.khronos.opengles.GL10;
10 import android.content.Context;
11 import android.content.res.Resources;
12 import android.graphics.Bitmap;
13 import android.graphics.BitmapFactory;
14 import android.opengl.GLES20;
15 import android.opengl.GLSurfaceView;
16 import android.opengl.GLUtils;
18 public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
21 private final String vertSource =
22 "attribute vec4 a_position;" +
23 "attribute vec2 a_texCoord;" +
24 "varying vec2 v_texCoord;" +
26 " gl_Position = a_position;" +
27 " v_texCoord = a_texCoord;" +
30 private final String fragSource =
31 "precision mediump float;" +
32 "varying vec2 v_texCoord;" +
33 "uniform sampler2D s_texture;" +
34 "uniform vec4 a_color;" +
36 //" gl_FragColor = a_color;" +
37 " gl_FragColor = texture2D(" +
38 " s_texture, v_texCoord);" +
42 private final float vertCoords[] = {
49 private final float texCoords[] = {
56 private final float color[] = {
61 private final String cards[] = {
62 "As", "Ks", "Qs", "Js", "10s", "9s", "8s", "7s", "6s", "5s", "4s", "3s", "2s",
63 "Ah", "Kh", "Qh", "Jh", "10h", "9h", "8h", "7h", "6h", "5h", "4h", "3h", "2h",
64 "Ac", "Kc", "Qc", "Jc", "10c", "9c", "8c", "7c", "6c", "5c", "4c", "3c", "2c",
65 "Ad", "Kd", "Qd", "Jd", "10d", "9d", "8d", "7d", "6d", "5d", "4d", "3d", "2d",
69 private Resources res;
72 private FloatBuffer vertBuf;
73 private FloatBuffer coordBuf;
75 private int vertHandle;
76 private int coordHandle;
77 private int texHandle;
78 private int colorHandle;
80 private int faces[] = new int[52];
81 private int backs[] = new int[2];
82 private int test[] = new int[1];
85 private int loadShader(int type, String code)
87 Os.debug("Cards: loadShader");
89 int shader = GLES20.glCreateShader(type);
90 GLES20.glShaderSource(shader, code);
91 GLES20.glCompileShader(shader);
95 public int loadTexture(int id)
97 Os.debug("Cards: loadTexture");
99 final int[] tex = new int[1];
102 Bitmap bitmap = BitmapFactory.decodeResource(this.res, id);
103 Os.debug("Cards: loadTexture - bitmap=" + bitmap);
106 GLES20.glGenTextures(1, tex, 0);
107 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex[0]);
108 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
109 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
110 GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
112 // Free the bitmap ??
118 /* GLSurfaceView Methods */
119 public Cards(Context context)
122 Os.debug("Cards: create");
124 this.res = context.getResources();
126 this.setEGLContextClientVersion(2);
127 this.setRenderer(this);
128 this.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
131 /* Renderer methods */
133 public void onSurfaceCreated(GL10 unused, EGLConfig config)
135 Os.debug("Cards: onSurfaceCreate");
137 /* Initialize shaders */
138 int vertShader = this.loadShader(GLES20.GL_VERTEX_SHADER, vertSource);
139 int fragShader = this.loadShader(GLES20.GL_FRAGMENT_SHADER, fragSource);
141 /* Link shaders into an OpenGL program */
142 this.program = GLES20.glCreateProgram();
143 GLES20.glAttachShader(program, vertShader);
144 GLES20.glAttachShader(program, fragShader);
145 GLES20.glLinkProgram(program);
147 /* Get shaders attributes */
148 this.vertHandle = GLES20.glGetAttribLocation(program, "a_position");
149 this.coordHandle = GLES20.glGetAttribLocation(program, "a_texCoord");
150 this.texHandle = GLES20.glGetUniformLocation(program, "s_texture");
151 this.colorHandle = GLES20.glGetUniformLocation(program, "a_color");
154 //this.coordBuf = FloatBuffer.wrap(this.texCoords);
156 /* Create vertex array */
157 ByteBuffer vertBytes = ByteBuffer.allocateDirect(vertCoords.length * 4);
158 vertBytes.order(ByteOrder.nativeOrder());
160 this.vertBuf = vertBytes.asFloatBuffer();
161 this.vertBuf.put(vertCoords);
162 this.vertBuf.position(0);
164 /* Create texture coord array */
165 ByteBuffer coordBytes = ByteBuffer.allocateDirect(texCoords.length * 4);
166 coordBytes.order(ByteOrder.nativeOrder());
168 this.coordBuf = coordBytes.asFloatBuffer();
169 this.coordBuf.put(texCoords);
170 this.coordBuf.position(0);
173 this.test[0] = this.loadTexture(R.drawable.card_as);
174 //this.test[0] = this.staticTexture();
177 Os.debug("Cards: onSurfaceCreate");
178 Os.debug("Cards: tex=" + this.test[0]);
182 public void onDrawFrame(GL10 unused)
184 Os.debug("Cards: onDrawFrame");
186 /* Turn on the program */
187 GLES20.glUseProgram(program);
190 GLES20.glClearColor(0, 0, 0, 1);
191 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
193 GLES20.glEnableVertexAttribArray(this.vertHandle);
194 GLES20.glEnableVertexAttribArray(this.coordHandle);
195 GLES20.glVertexAttribPointer(this.vertHandle, 3, GLES20.GL_FLOAT, false, 3*4, this.vertBuf);
196 GLES20.glVertexAttribPointer(this.coordHandle, 2, GLES20.GL_FLOAT, false, 2*4, this.coordBuf);
198 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
199 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.test[0]);
200 GLES20.glUniform1i(this.texHandle, 0);
202 GLES20.glUniform4fv(this.colorHandle, 1, this.color, 0);
204 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
205 GLES20.glDisableVertexAttribArray(this.coordHandle);
209 public void onSurfaceChanged(GL10 unused, int width, int height)
211 Os.debug("Cards: onSurfaceChanged");
213 GLES20.glViewport(0, 0, width, height);