My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.badlogic.gamedev.tools;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.EGLConfigChooser;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
* The game activity implements the Activity Life Cycle of the
* game. A {@link GameListener} can be attached to the activity and will
* be called whenever a new frame has to be rendered or the
* application needs to be setup.
*
* @author mzechner
*
*/
public class GameActivity extends Activity implements GLSurfaceView.Renderer, OnTouchListener, SensorEventListener
{
/** GLSurfaceView **/
private GLSurfaceView glSurface;

/** with and height of the viewport **/
private int width, height;

/** GameListener **/
private GameListener listener;

/** start time of last frame in nano seconds **/
long lastFrameStart;

/** delta time in seconds **/
float deltaTime;

/** touch coordinates and touched state **/
int touchX, touchY;
boolean isTouched;

/** acceleration on the 3 axis **/
float[] acceleration = new float[3];

/**
* Called on creation of the Activity
*/
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

glSurface = new GLSurfaceView( this );
// glSurface.setEGLConfigChooser(new EGLConfigChooser() {
//
// @Override
// public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display)
// {
// final int R = 4;
// final int G = 4;
// final int B = 4;
// final int A = 0;
//
// /* The best configuration that was found. */
// EGLConfig best = null;
//
// /* Temporary configurations that are found during the process. */
// EGLConfig[] configs = new EGLConfig[100];
//
// /* Number of configurations found during a choose configuration. */
// int[] result = new int[1];
//
// /* Desired configuration attributes. */
// int[] attr;
//
// /* Starting depth. */
// int depth = 24;
//
// /*
// * Search for configurations that support 24-bit depth first. This
// * will handle the Droid case where a higher depth buffer size
// * renders faster.
// */
// while ((depth > 4) && (best == null))
// {
// Log.i("OGL", "Searching with depth of '"
// + Integer.toString(depth) + "' bits.");
//
// attr = new int[] {
// EGL10.EGL_RED_SIZE, R, EGL10.EGL_GREEN_SIZE, G,
// EGL10.EGL_BLUE_SIZE, B, EGL10.EGL_ALPHA_SIZE, A,
// EGL10.EGL_DEPTH_SIZE, depth, EGL10.EGL_NONE };
//
// egl.eglChooseConfig(display, attr, configs, configs.length, result);
//
// for (int i = 0; i < result[0]; i++)
// {
// Log.i("OGL", " Candidate " + Integer.toString(i + 1) + ":");
// logConfig(egl, display, configs[i]);
// Log.i("OGL", " ---------------------------------------");
// }
//
// if (result[0] > 0)
// {
// best = configs[result[0]-1];
//
// Log.i("OGL", "Best Config: ");
// logConfig(egl, display, best);
//
// break;
// }
//
// depth -= 4;
//
//
// }
//
// return best;
//
// }
//
// private void logConfig( EGL10 egl, EGLDisplay display, EGLConfig config )
// {
// int r = 0;
// int g = 0;
// int b = 0;
// int a = 0;
// int d = 0;
// int s = 0;
// int mValue[] = new int[1];
// if( egl.eglGetConfigAttrib(display, config, EGL10.EGL_RED_SIZE, mValue) )
// r = mValue[0];
// if( egl.eglGetConfigAttrib(display, config, EGL10.EGL_GREEN_SIZE, mValue) )
// g = mValue[0];
// if( egl.eglGetConfigAttrib(display, config, EGL10.EGL_BLUE_SIZE, mValue) )
// b = mValue[0];
// if( egl.eglGetConfigAttrib(display, config, EGL10.EGL_ALPHA_SIZE, mValue) )
// a = mValue[0];
// if( egl.eglGetConfigAttrib(display, config, EGL10.EGL_DEPTH_SIZE, mValue) )
// d = mValue[0];
// if( egl.eglGetConfigAttrib(display, config, EGL10.EGL_STENCIL_SIZE, mValue) )
// s = mValue[0];
//
// Log.d( "OGL", r + "" + g + "" + b + "" + a + ":" + d + ":" + s );
// }
// });
glSurface.setRenderer( this );
this.setContentView( glSurface );

glSurface.setOnTouchListener( this );

SensorManager manager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
if( manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() > 0 )
{
Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME );
}
}

/**
* Called each Frame
*/
@Override
public void onDrawFrame(GL10 gl)
{
long currentFrameStart = System.nanoTime();
deltaTime = (currentFrameStart-lastFrameStart) / 1000000000.0f;
lastFrameStart = currentFrameStart;

if( listener != null )
listener.mainLoopIteration( this, gl );
}

/**
* Called when the surface size changed, e.g. due to tilting
*/
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
this.width = width;
this.height = height;
}

/**
* Called when the GLSurfaceView has finished initialization
*/
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
lastFrameStart = System.nanoTime();
String renderer = gl.glGetString( GL10.GL_RENDERER );
if( renderer.toLowerCase().contains("pixelflinger" ) )
Mesh.globalVBO = false;

if( listener != null )
listener.setup( this, gl );
}

/**
* Called when the application is paused. We need to
* also pause the GLSurfaceView.
*/
@Override
protected void onPause() {
super.onPause();
glSurface.onPause();
}

/**
* Called when the application is resumed. We need to
* also resume the GLSurfaceView.
*/
@Override
protected void onResume() {
super.onResume();
glSurface.onResume();
}

/**
* Called when a touch event occurs.
*/
@Override
public boolean onTouch(View v, MotionEvent event)
{
if( event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE )
{
touchX = (int)event.getX();
touchY = (int)event.getY();
isTouched = true;
}

if( event.getAction() == MotionEvent.ACTION_UP )
isTouched = false;

try
{
Thread.sleep( 30 );
}
catch( Exception ex )
{

}

return true;
}

/**
* Called when the accuracy of the Sensor has changed.
* @param sensor
* @param accuracy
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// we ignore this
}

/**
* Called when the sensor has new input
* @param event The SensorEvent
*/
@Override
public void onSensorChanged(SensorEvent event) {
System.arraycopy( event.values, 0, acceleration, 0, 3 );
}

/**
* Sets the {@link GameListener}
* @param listener the GameListener
*/
public void setGameListener(GameListener listener)
{
this.listener = listener;
}

/**
* @return the viewport width in pixels
*/
public int getViewportWidth( )
{
return width;
}

/**
* @return the viewport height in pixels
*/
public int getViewportHeight( )
{
return height;
}

/**
* @return the delta time in seconds
*/
public float getDeltaTime( )
{
return deltaTime;
}

/**
* @return the last known touch coordinate on the x axis
*/
public int getTouchX( )
{
return touchX;
}

/**
* @return the last known touch coordinate on the x axis
*/
public int getTouchY( )
{
return touchY;
}

/**
* @return wheter the touch screen is touched or not
*/
public boolean isTouched( )
{
return isTouched;
}

/**
* @return the acceleration on the x-Axis of the device
*/
public float getAccelerationOnXAxis( )
{
return acceleration[0];
}

/**
* @return the acceleration on the x-Axis of the device
*/
public float getAccelerationOnYAxis( )
{
return acceleration[1];
}

/**
* @return the acceleration on the x-Axis of the device
*/
public float getAccelerationOnZAxis( )
{
return acceleration[2];
}
}

Change log

r61 by quantumgame on Jan 16, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r60 by quantumgame on Jan 16, 2010   Diff
[No log message]
r58 by quantumgame on Jan 16, 2010   Diff
[No log message]
r56 by quantumgame on Jan 16, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 8533 bytes, 356 lines
Powered by Google Project Hosting