Mercurial > sdl-ios-xcode
comparison android/testproject/src/org/libsdl/android/SDLActivity.java @ 4723:74da47b2f5b7
More joystick stuff
author | Paul Hunkin <paul@bieh.net> |
---|---|
date | Tue, 27 Jul 2010 21:58:18 +0200 |
parents | faa228f7ce5b |
children |
comparison
equal
deleted
inserted
replaced
4722:faa228f7ce5b | 4723:74da47b2f5b7 |
---|---|
11 import android.util.Log; | 11 import android.util.Log; |
12 import android.graphics.*; | 12 import android.graphics.*; |
13 import android.text.method.*; | 13 import android.text.method.*; |
14 import android.text.*; | 14 import android.text.*; |
15 import android.media.*; | 15 import android.media.*; |
16 import android.hardware.*; | |
17 import android.content.*; | |
16 | 18 |
17 import java.lang.*; | 19 import java.lang.*; |
18 | 20 |
19 | 21 |
20 /** | 22 /** |
23 public class SDLActivity extends Activity { | 25 public class SDLActivity extends Activity { |
24 | 26 |
25 //Main components | 27 //Main components |
26 private static SDLActivity mSingleton; | 28 private static SDLActivity mSingleton; |
27 private static SDLSurface mSurface; | 29 private static SDLSurface mSurface; |
28 | 30 |
31 //Audio | |
29 private static AudioTrack mAudioTrack; | 32 private static AudioTrack mAudioTrack; |
33 private static boolean bAudioIsEnabled; | |
34 | |
35 //Sensors | |
36 private static boolean bAccelIsEnabled; | |
30 | 37 |
31 //feature IDs. Must match up on the C side as well. | 38 //feature IDs. Must match up on the C side as well. |
32 private static int FEATURE_SOUND = 1; | 39 private static int FEATURE_AUDIO = 1; |
33 private static int FEATURE_ACCEL = 2; | 40 private static int FEATURE_ACCEL = 2; |
34 | 41 |
35 //Load the .so | 42 //Load the .so |
36 static { | 43 static { |
37 System.loadLibrary("sdltest"); | 44 System.loadLibrary("sdltest"); |
50 SurfaceHolder holder = mSurface.getHolder(); | 57 SurfaceHolder holder = mSurface.getHolder(); |
51 holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); | 58 holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); |
52 | 59 |
53 } | 60 } |
54 | 61 |
62 //Audio | |
55 public static boolean initAudio(){ | 63 public static boolean initAudio(){ |
56 | 64 |
57 //blah. Hardcoded things are bad. FIXME when we have more sound stuff | 65 //blah. Hardcoded things are bad. FIXME when we have more sound stuff |
58 //working properly. | 66 //working properly. |
59 mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, | 67 mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, |
60 11025, | 68 11025, |
61 AudioFormat.CHANNEL_CONFIGURATION_MONO, | 69 AudioFormat.CHANNEL_CONFIGURATION_MONO, |
62 AudioFormat.ENCODING_PCM_8BIT, | 70 AudioFormat.ENCODING_PCM_8BIT, |
63 2048, | 71 2048, |
64 AudioTrack.MODE_STREAM); | 72 AudioTrack.MODE_STREAM); |
65 return true; | 73 bAudioIsEnabled = true; |
66 } | 74 return true; |
75 } | |
76 | |
77 //Accel | |
78 public static boolean initAccel(){ | |
79 mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, true); | |
80 bAccelIsEnabled = true; | |
81 return true; | |
82 } | |
83 | |
84 public static boolean closeAccel(){ | |
85 mSurface.enableSensor(Sensor.TYPE_ACCELEROMETER, false); | |
86 bAccelIsEnabled = false; | |
87 return true; | |
88 } | |
89 | |
67 | 90 |
68 //Events | 91 //Events |
69 protected void onPause() { | 92 protected void onPause() { |
70 super.onPause(); | 93 super.onPause(); |
71 } | 94 } |
85 public static native void onNativeKeyDown(int keycode); | 108 public static native void onNativeKeyDown(int keycode); |
86 public static native void onNativeKeyUp(int keycode); | 109 public static native void onNativeKeyUp(int keycode); |
87 public static native void onNativeTouch(int action, float x, | 110 public static native void onNativeTouch(int action, float x, |
88 float y, float p); | 111 float y, float p); |
89 public static native void onNativeResize(int x, int y, int format); | 112 public static native void onNativeResize(int x, int y, int format); |
90 | 113 public static native void onNativeAccel(float x, float y, float z); |
91 | 114 |
92 | 115 |
93 | 116 |
94 //Java functions called from C | 117 //Java functions called from C |
95 private static void createGLContext(){ | 118 private static void createGLContext(){ |
115 public static void enableFeature(int featureid, int enabled){ | 138 public static void enableFeature(int featureid, int enabled){ |
116 Log.v("SDL","Feature " + featureid + " = " + enabled); | 139 Log.v("SDL","Feature " + featureid + " = " + enabled); |
117 | 140 |
118 //Yuck. This is all horribly inelegent. If it gets to more than a few | 141 //Yuck. This is all horribly inelegent. If it gets to more than a few |
119 //'features' I'll rip this out and make something nicer, I promise :) | 142 //'features' I'll rip this out and make something nicer, I promise :) |
120 if(featureid == FEATURE_SOUND){ | 143 if(featureid == FEATURE_AUDIO){ |
121 if(enabled == 1){ | 144 if(enabled == 1){ |
122 initAudio(); | 145 initAudio(); |
123 }else{ | 146 }else{ |
124 //We don't have one of these yet... | 147 //We don't have one of these yet... |
125 //closeAudio(); | 148 //closeAudio(); |
126 } | 149 } |
127 } | 150 } |
151 | |
152 else if(featureid == FEATURE_ACCEL){ | |
153 if(enabled == 1){ | |
154 initAccel(); | |
155 }else{ | |
156 closeAccel(); | |
157 } | |
158 } | |
128 } | 159 } |
129 | 160 |
130 | 161 |
131 | 162 |
132 | 163 |
155 in order to do anything useful. | 186 in order to do anything useful. |
156 | 187 |
157 Because of this, that's where we set up the SDL thread | 188 Because of this, that's where we set up the SDL thread |
158 */ | 189 */ |
159 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, | 190 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, |
160 View.OnKeyListener, View.OnTouchListener { | 191 View.OnKeyListener, View.OnTouchListener, SensorEventListener { |
161 | 192 |
162 //This is what SDL runs in. It invokes SDL_main(), eventually | 193 //This is what SDL runs in. It invokes SDL_main(), eventually |
163 private Thread mSDLThread; | 194 private Thread mSDLThread; |
164 | 195 |
165 //EGL private objects | 196 //EGL private objects |
166 private EGLContext mEGLContext; | 197 private EGLContext mEGLContext; |
167 private EGLSurface mEGLSurface; | 198 private EGLSurface mEGLSurface; |
168 private EGLDisplay mEGLDisplay; | 199 private EGLDisplay mEGLDisplay; |
169 | 200 |
201 //Sensors | |
202 private static SensorManager mSensorManager; | |
203 | |
170 //Startup | 204 //Startup |
171 public SDLSurface(Context context) { | 205 public SDLSurface(Context context) { |
172 super(context); | 206 super(context); |
173 getHolder().addCallback(this); | 207 getHolder().addCallback(this); |
174 | 208 |
175 setFocusable(true); | 209 setFocusable(true); |
176 setFocusableInTouchMode(true); | 210 setFocusableInTouchMode(true); |
177 requestFocus(); | 211 requestFocus(); |
178 setOnKeyListener(this); | 212 setOnKeyListener(this); |
179 setOnTouchListener(this); | 213 setOnTouchListener(this); |
214 | |
215 mSensorManager = (SensorManager)context.getSystemService("sensor"); | |
180 } | 216 } |
181 | 217 |
182 //Called when we have a valid drawing surface | 218 //Called when we have a valid drawing surface |
183 public void surfaceCreated(SurfaceHolder holder) { | 219 public void surfaceCreated(SurfaceHolder holder) { |
184 Log.v("SDL","Surface created"); | 220 Log.v("SDL","Surface created"); |
319 //TODO: Anything else we need to pass? | 355 //TODO: Anything else we need to pass? |
320 SDLActivity.onNativeTouch(action, x, y, p); | 356 SDLActivity.onNativeTouch(action, x, y, p); |
321 return true; | 357 return true; |
322 } | 358 } |
323 | 359 |
360 //Sensor events | |
361 public void enableSensor(int sensortype, boolean enabled){ | |
362 //TODO: This uses getDefaultSensor - what if we have >1 accels? | |
363 if(enabled){ | |
364 mSensorManager.registerListener(this, | |
365 mSensorManager.getDefaultSensor(sensortype), | |
366 SensorManager.SENSOR_DELAY_GAME, null); | |
367 }else{ | |
368 mSensorManager.unregisterListener(this, | |
369 mSensorManager.getDefaultSensor(sensortype)); | |
370 } | |
371 } | |
372 | |
373 public void onAccuracyChanged(Sensor sensor, int accuracy){ | |
374 //TODO | |
375 } | |
376 | |
377 public void onSensorChanged(SensorEvent event){ | |
378 if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){ | |
379 SDLActivity.onNativeAccel( event.values[0], | |
380 event.values[1], | |
381 event.values[2] ); | |
382 } | |
383 } | |
384 | |
324 | 385 |
325 } | 386 } |
326 | 387 |
327 | 388 |