]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Cards.java
756b00a15889102dc01bb33d9f0390ec87a851fa
[~andy/spades] / src / org / pileus / spades / Cards.java
1 package org.pileus.spades;
2
3 import java.nio.ByteBuffer;
4 import java.nio.ByteOrder;
5 import java.nio.FloatBuffer;
6
7 import javax.microedition.khronos.egl.EGLConfig;
8 import javax.microedition.khronos.opengles.GL10;
9
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;
17
18 public class Cards extends GLSurfaceView implements GLSurfaceView.Renderer
19 {
20         /* Shader data */
21         private final String vertSource =
22                 "attribute vec4 a_position;"  +
23                 "attribute vec2 a_texCoord;"  +
24                 "varying   vec2 v_texCoord;"  +
25                 "void main() {"               +
26                 "  gl_Position = a_position;" +
27                 "  v_texCoord  = a_texCoord;" +
28                 "}";
29
30         private final String fragSource =
31                 "precision mediump   float;"      +
32                 "varying   vec2      v_texCoord;" +
33                 "uniform   sampler2D s_texture;"  +
34                 "uniform   vec4      a_color;"    +
35                 "void main() {"                   +
36                 //"  gl_FragColor = a_color;"       +
37                 "  gl_FragColor = texture2D("     +
38                 "    s_texture, v_texCoord);"     +
39                 "}";
40
41         /* Drawing data */
42         private final float  vertCoords[] = {
43                 -0.5f,  0.5f, 0.0f,
44                 -0.5f, -0.5f, 0.0f,
45                  0.5f, -0.5f, 0.0f,
46                  0.5f,  0.5f, 0.0f,
47         };
48
49         private final float  texCoords[] = {
50                 0.0f, 0.0f,
51                 0.0f, 1.0f,
52                 1.0f, 1.0f,
53                 1.0f, 0.0f,
54         };
55
56         private final float  color[] = {
57                 1, 0, 0, 1
58         };
59
60         /* Cards data */
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",
66         };
67
68         /* Private data */
69         private Resources    res;
70         private int          program;
71
72         private FloatBuffer  vertBuf;
73         private FloatBuffer  coordBuf;
74
75         private int          vertHandle;
76         private int          coordHandle;
77         private int          texHandle;
78         private int          colorHandle;
79
80         private int          faces[] = new int[52];
81         private int          backs[] = new int[2];
82         private int          test[]  = new int[1];
83
84         /* Private methods */
85         private int loadShader(int type, String code)
86         {
87                 Os.debug("Cards: loadShader");
88
89                 int shader = GLES20.glCreateShader(type);
90                 GLES20.glShaderSource(shader, code);
91                 GLES20.glCompileShader(shader);
92                 return shader;
93         }
94
95         public int loadTexture(int id)
96         {
97                 Os.debug("Cards: loadTexture");
98
99                 final int[] tex = new int[1];
100
101                 // Load the bitmap
102                 Bitmap bitmap = BitmapFactory.decodeResource(this.res, id);
103                 Os.debug("Cards: loadTexture - bitmap=" + bitmap);
104
105                 // Copy into OpenGL
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);
111
112                 // Free the bitmap ??
113                 //bitmap.recycle();
114
115                 return tex[0];
116         }
117
118         /* GLSurfaceView Methods */
119         public Cards(Context context)
120         {
121                 super(context);
122                 Os.debug("Cards: create");
123
124                 this.res = context.getResources();
125
126                 this.setEGLContextClientVersion(2);
127                 this.setRenderer(this);
128                 this.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
129         }
130
131         /* Renderer methods */
132         @Override
133         public void onSurfaceCreated(GL10 unused, EGLConfig config)
134         {
135                 Os.debug("Cards: onSurfaceCreate");
136
137                 /* Initialize shaders */
138                 int vertShader = this.loadShader(GLES20.GL_VERTEX_SHADER,   vertSource);
139                 int fragShader = this.loadShader(GLES20.GL_FRAGMENT_SHADER, fragSource);
140
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);
146
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");
152
153                 // ???
154                 //this.coordBuf  = FloatBuffer.wrap(this.texCoords);
155
156                 /* Create vertex array  */
157                 ByteBuffer vertBytes = ByteBuffer.allocateDirect(vertCoords.length * 4);
158                 vertBytes.order(ByteOrder.nativeOrder());
159
160                 this.vertBuf = vertBytes.asFloatBuffer();
161                 this.vertBuf.put(vertCoords);
162                 this.vertBuf.position(0);
163
164                 /* Create texture coord array */
165                 ByteBuffer coordBytes = ByteBuffer.allocateDirect(texCoords.length * 4);
166                 coordBytes.order(ByteOrder.nativeOrder());
167
168                 this.coordBuf = coordBytes.asFloatBuffer();
169                 this.coordBuf.put(texCoords);
170                 this.coordBuf.position(0);
171
172                 /* Load textures */
173                 this.test[0] = this.loadTexture(R.drawable.card_as);
174                 //this.test[0] = this.staticTexture();
175
176                 /* Debug */
177                 Os.debug("Cards: onSurfaceCreate");
178                 Os.debug("Cards:     tex=" + this.test[0]);
179         }
180
181         @Override
182         public void onDrawFrame(GL10 unused)
183         {
184                 Os.debug("Cards: onDrawFrame");
185
186                 /* Turn on the program */
187                 GLES20.glUseProgram(program);
188
189                 /* Draw */
190                 GLES20.glClearColor(0, 0, 0, 1);
191                 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
192
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);
197
198                 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
199                 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, this.test[0]);
200                 GLES20.glUniform1i(this.texHandle, 0);
201
202                 GLES20.glUniform4fv(this.colorHandle, 1, this.color, 0);
203
204                 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
205                 GLES20.glDisableVertexAttribArray(this.coordHandle);
206         }
207
208         @Override
209         public void onSurfaceChanged(GL10 unused, int width, int height)
210         {
211                 Os.debug("Cards: onSurfaceChanged");
212
213                 GLES20.glViewport(0, 0, width, height);
214         }
215 }