project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java
branchhedgeroid
changeset 5469 2ad2a4261b56
parent 5416 26a36326d199
child 5508 dcf1b3645af6
equal deleted inserted replaced
5467:88e25840f532 5469:2ad2a4261b56
     3 import javax.microedition.khronos.egl.EGL10;
     3 import javax.microedition.khronos.egl.EGL10;
     4 import javax.microedition.khronos.egl.EGLConfig;
     4 import javax.microedition.khronos.egl.EGLConfig;
     5 import javax.microedition.khronos.egl.EGLContext;
     5 import javax.microedition.khronos.egl.EGLContext;
     6 import javax.microedition.khronos.egl.EGLDisplay;
     6 import javax.microedition.khronos.egl.EGLDisplay;
     7 import javax.microedition.khronos.egl.EGLSurface;
     7 import javax.microedition.khronos.egl.EGLSurface;
       
     8 
       
     9 import org.hedgewars.mobile.EngineProtocol.EngineProtocolNetwork;
       
    10 import org.hedgewars.mobile.EngineProtocol.GameConfig;
     8 
    11 
     9 import android.app.Activity;
    12 import android.app.Activity;
    10 import android.content.Context;
    13 import android.content.Context;
    11 import android.graphics.Canvas;
    14 import android.graphics.Canvas;
    12 import android.graphics.PixelFormat;
    15 import android.graphics.PixelFormat;
    25 import android.view.MotionEvent;
    28 import android.view.MotionEvent;
    26 import android.view.SurfaceHolder;
    29 import android.view.SurfaceHolder;
    27 import android.view.SurfaceView;
    30 import android.view.SurfaceView;
    28 import android.view.View;
    31 import android.view.View;
    29 
    32 
    30 
       
    31 /**
    33 /**
    32     SDL Activity
    34  * SDL Activity
    33  */
    35  */
    34 public class SDLActivity extends Activity {
    36 public class SDLActivity extends Activity {
    35 
    37 
    36 	// Main components
    38 	// Main components
    37 	public static SDLActivity mSingleton;
    39 	public static SDLActivity mSingleton;
    41 	private static Thread mAudioThread;
    43 	private static Thread mAudioThread;
    42 	private static AudioTrack mAudioTrack;
    44 	private static AudioTrack mAudioTrack;
    43 
    45 
    44 	// Load the .so
    46 	// Load the .so
    45 	static {
    47 	static {
    46 
       
    47 		System.loadLibrary("SDL");
    48 		System.loadLibrary("SDL");
    48 		System.loadLibrary("SDL_image");
    49 		System.loadLibrary("SDL_image");
    49 		System.loadLibrary("mikmod");
    50 		System.loadLibrary("mikmod");
    50 		System.loadLibrary("SDL_net");
    51 		System.loadLibrary("SDL_net");
    51 		System.loadLibrary("SDL_mixer");
    52 		System.loadLibrary("SDL_mixer");
    52 		System.loadLibrary("SDL_ttf");
    53 		System.loadLibrary("SDL_ttf");
       
    54 		System.loadLibrary("lua5.1");
    53 		System.loadLibrary("hwengine");
    55 		System.loadLibrary("hwengine");
    54 		System.loadLibrary("main");
    56 		System.loadLibrary("main");
    55 	}
    57 	}
    56 
    58 
    57 	// Setup
    59 	// Setup
    58 	protected void onCreate(Bundle savedInstanceState) {
    60 	protected void onCreate(Bundle savedInstanceState) {
    59 		//Log.v("SDL", "onCreate()");
    61 		// Log.v("SDL", "onCreate()");
    60 		super.onCreate(savedInstanceState);
    62 		super.onCreate(savedInstanceState);
    61 
    63 
    62 		// So we can call stuff from static callbacks
    64 		// So we can call stuff from static callbacks
    63 		mSingleton = this;
    65 		mSingleton = this;
    64 
    66 
    65 		// Set up the surface
    67 		// Set up the surface
    66 		mSurface = new SDLSurface(getApplication());
    68 		GameConfig config = getIntent().getParcelableExtra("config");
       
    69 		mSurface = new SDLSurface(getApplication(), config);
    67 		setContentView(mSurface);
    70 		setContentView(mSurface);
    68 		SurfaceHolder holder = mSurface.getHolder();
    71 		SurfaceHolder holder = mSurface.getHolder();
    69 		holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    72 		holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
    70 	}
    73 	}
    71 
    74 
    72 	// Events
    75 	// Events
    73 	protected void onPause() {
    76 	protected void onPause() {
    74 		//Log.v("SDL", "onPause()");
    77 		// Log.v("SDL", "onPause()");
    75 		super.onPause();
    78 		super.onPause();
       
    79 
    76 	}
    80 	}
    77 
    81 
    78 	protected void onResume() {
    82 	protected void onResume() {
    79 		//Log.v("SDL", "onResume()");
    83 		// Log.v("SDL", "onResume()");
    80 		super.onResume();
    84 		super.onResume();
    81 	}
    85 	}
    82 
    86 
    83 	// Messages from the SDLMain thread
    87 	// Messages from the SDLMain thread
    84 	static int COMMAND_CHANGE_TITLE = 1;
    88 	static int COMMAND_CHANGE_TITLE = 1;
    85 
    89 
    86 	// Handler for the messages
    90 	// Handler for the messages
    87 	Handler commandHandler = new Handler() {
    91 	Handler commandHandler = new Handler() {
    88 		public void handleMessage(Message msg) {
    92 		public void handleMessage(Message msg) {
    89 			if (msg.arg1 == COMMAND_CHANGE_TITLE) {
    93 			if (msg.arg1 == COMMAND_CHANGE_TITLE) {
    90 				setTitle((String)msg.obj);
    94 				setTitle((String) msg.obj);
    91 			}
    95 			}
    92 		}
    96 		}
    93 	};
    97 	};
    94 
    98 
    95 	// Send a message from the SDLMain thread
    99 	// Send a message from the SDLMain thread
   100 		commandHandler.sendMessage(msg);
   104 		commandHandler.sendMessage(msg);
   101 	}
   105 	}
   102 
   106 
   103 	// C functions we call
   107 	// C functions we call
   104 	public static native void nativeInit(String[] argv);
   108 	public static native void nativeInit(String[] argv);
       
   109 
   105 	public static native void nativeQuit();
   110 	public static native void nativeQuit();
       
   111 
   106 	public static native void onNativeResize(int x, int y, int format);
   112 	public static native void onNativeResize(int x, int y, int format);
       
   113 
   107 	public static native void onNativeKeyDown(int keycode);
   114 	public static native void onNativeKeyDown(int keycode);
       
   115 
   108 	public static native void onNativeKeyUp(int keycode);
   116 	public static native void onNativeKeyUp(int keycode);
   109 	public static native void onNativeTouch(int action, float x, 
   117 
   110 			float y, float p);
   118 	public static native void onNativeTouch(int action, float x, float y,
       
   119 			float p);
       
   120 
   111 	public static native void onNativeAccel(float x, float y, float z);
   121 	public static native void onNativeAccel(float x, float y, float z);
       
   122 
   112 	public static native void nativeRunAudioThread();
   123 	public static native void nativeRunAudioThread();
   113 
       
   114 
   124 
   115 	// Java functions called from C
   125 	// Java functions called from C
   116 
   126 
   117 	public static boolean createGLContext(int majorVersion, int minorVersion) {
   127 	public static boolean createGLContext(int majorVersion, int minorVersion) {
   118 		return mSurface.initEGL(majorVersion, minorVersion);
   128 		return mSurface.initEGL(majorVersion, minorVersion);
   128 	}
   138 	}
   129 
   139 
   130 	// Audio
   140 	// Audio
   131 	private static Object buf;
   141 	private static Object buf;
   132 
   142 
   133 	public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) {
   143 	public static Object audioInit(int sampleRate, boolean is16Bit,
   134 		int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO;
   144 			boolean isStereo, int desiredFrames) {
   135 		int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT;
   145 		int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO
       
   146 				: AudioFormat.CHANNEL_CONFIGURATION_MONO;
       
   147 		int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT
       
   148 				: AudioFormat.ENCODING_PCM_8BIT;
   136 		int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
   149 		int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1);
   137 
   150 
   138 		Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer");
   151 		Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono")
       
   152 				+ " " + (is16Bit ? "16-bit" : "8-bit") + " "
       
   153 				+ ((float) sampleRate / 1000f) + "kHz, " + desiredFrames
       
   154 				+ " frames buffer");
   139 
   155 
   140 		// Let the user pick a larger buffer if they really want -- but ye
   156 		// Let the user pick a larger buffer if they really want -- but ye
   141 		// gods they probably shouldn't, the minimums are horrifyingly high
   157 		// gods they probably shouldn't, the minimums are horrifyingly high
   142 		// latency already
   158 		// latency already
   143 		desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize);
   159 		desiredFrames = Math.max(
       
   160 				desiredFrames,
       
   161 				(AudioTrack.getMinBufferSize(sampleRate, channelConfig,
       
   162 						audioFormat) + frameSize - 1)
       
   163 						/ frameSize);
   144 
   164 
   145 		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
   165 		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
   146 				channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM);
   166 				channelConfig, audioFormat, desiredFrames * frameSize,
       
   167 				AudioTrack.MODE_STREAM);
   147 
   168 
   148 		audioStartThread();
   169 		audioStartThread();
   149 
   170 
   150 		Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + ((float)mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
   171 		Log.v("SDL",
       
   172 				"SDL audio: got "
       
   173 						+ ((mAudioTrack.getChannelCount() >= 2) ? "stereo"
       
   174 								: "mono")
       
   175 						+ " "
       
   176 						+ ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit"
       
   177 								: "8-bit") + " "
       
   178 						+ ((float) mAudioTrack.getSampleRate() / 1000f)
       
   179 						+ "kHz, " + desiredFrames + " frames buffer");
   151 
   180 
   152 		if (is16Bit) {
   181 		if (is16Bit) {
   153 			buf = new short[desiredFrames * (isStereo ? 2 : 1)];
   182 			buf = new short[desiredFrames * (isStereo ? 2 : 1)];
   154 		} else {
   183 		} else {
   155 			buf = new byte[desiredFrames * (isStereo ? 2 : 1)]; 
   184 			buf = new byte[desiredFrames * (isStereo ? 2 : 1)];
   156 		}
   185 		}
   157 		return buf;
   186 		return buf;
   158 	}
   187 	}
   159 
   188 
   160 	public static void audioStartThread() {
   189 	public static void audioStartThread() {
   169 		mAudioThread.setPriority(Thread.MAX_PRIORITY);
   198 		mAudioThread.setPriority(Thread.MAX_PRIORITY);
   170 		mAudioThread.start();
   199 		mAudioThread.start();
   171 	}
   200 	}
   172 
   201 
   173 	public static void audioWriteShortBuffer(short[] buffer) {
   202 	public static void audioWriteShortBuffer(short[] buffer) {
   174 		for (int i = 0; i < buffer.length; ) {
   203 		for (int i = 0; i < buffer.length;) {
   175 			int result = mAudioTrack.write(buffer, i, buffer.length - i);
   204 			int result = mAudioTrack.write(buffer, i, buffer.length - i);
   176 			if (result > 0) {
   205 			if (result > 0) {
   177 				i += result;
   206 				i += result;
   178 			} else if (result == 0) {
   207 			} else if (result == 0) {
   179 				try {
   208 				try {
   180 					Thread.sleep(1);
   209 					Thread.sleep(1);
   181 				} catch(InterruptedException e) {
   210 				} catch (InterruptedException e) {
   182 					// Nom nom
   211 					// Nom nom
   183 				}
   212 				}
   184 			} else {
   213 			} else {
   185 				Log.w("SDL", "SDL audio: error return from write(short)");
   214 				Log.w("SDL", "SDL audio: error return from write(short)");
   186 				return;
   215 				return;
   187 			}
   216 			}
   188 		}
   217 		}
   189 	}
   218 	}
   190 
   219 
   191 	public static void audioWriteByteBuffer(byte[] buffer) {
   220 	public static void audioWriteByteBuffer(byte[] buffer) {
   192 		for (int i = 0; i < buffer.length; ) {
   221 		for (int i = 0; i < buffer.length;) {
   193 			int result = mAudioTrack.write(buffer, i, buffer.length - i);
   222 			int result = mAudioTrack.write(buffer, i, buffer.length - i);
   194 			if (result > 0) {
   223 			if (result > 0) {
   195 				i += result;
   224 				i += result;
   196 			} else if (result == 0) {
   225 			} else if (result == 0) {
   197 				try {
   226 				try {
   198 					Thread.sleep(1);
   227 					Thread.sleep(1);
   199 				} catch(InterruptedException e) {
   228 				} catch (InterruptedException e) {
   200 					// Nom nom
   229 					// Nom nom
   201 				}
   230 				}
   202 			} else {
   231 			} else {
   203 				Log.w("SDL", "SDL audio: error return from write(short)");
   232 				Log.w("SDL", "SDL audio: error return from write(short)");
   204 				return;
   233 				return;
   208 
   237 
   209 	public static void audioQuit() {
   238 	public static void audioQuit() {
   210 		if (mAudioThread != null) {
   239 		if (mAudioThread != null) {
   211 			try {
   240 			try {
   212 				mAudioThread.join();
   241 				mAudioThread.join();
   213 			} catch(Exception e) {
   242 			} catch (Exception e) {
   214 				Log.v("SDL", "Problem stopping audio thread: " + e);
   243 				Log.v("SDL", "Problem stopping audio thread: " + e);
   215 			}
   244 			}
   216 			mAudioThread = null;
   245 			mAudioThread = null;
   217 
   246 
   218 			//Log.v("SDL", "Finished waiting for audio thread");
   247 			// Log.v("SDL", "Finished waiting for audio thread");
   219 		}
   248 		}
   220 
   249 
   221 		if (mAudioTrack != null) {
   250 		if (mAudioTrack != null) {
   222 			mAudioTrack.stop();
   251 			mAudioTrack.stop();
   223 			mAudioTrack = null;
   252 			mAudioTrack = null;
   224 		}
   253 		}
   225 	}
   254 	}
   226 }
   255 }
   227 
   256 
   228 /**
   257 /**
   229     Simple nativeInit() runnable
   258  * Simple nativeInit() runnable
   230  */
   259  */
   231 class SDLMain implements Runnable {
   260 class SDLMain implements Runnable {
   232 	private int surfaceWidth, surfaceHeight;
   261 	private int surfaceWidth, surfaceHeight;
   233 	public SDLMain(int width, int height){
   262 	private GameConfig config;
       
   263 
       
   264 	public SDLMain(int width, int height, GameConfig _config) {
       
   265 		config = _config;
   234 		surfaceWidth = width;
   266 		surfaceWidth = width;
   235 		surfaceHeight = height;
   267 		surfaceHeight = height;
   236 	}
   268 	}
       
   269 
   237 	public void run() {
   270 	public void run() {
   238 		// Runs SDL_main()
   271 		// Runs SDL_main()
   239 
   272 
   240 		EngineProtocolNetwork ipc = new EngineProtocolNetwork(EngineProtocolNetwork.MODE_GAME);
   273 		EngineProtocolNetwork ipc = new EngineProtocolNetwork(config);
   241 		
   274 
   242 		String path = Utils.getDownloadPath(SDLActivity.mSingleton) + "Data";
   275 		String path = Utils.getDownloadPath(SDLActivity.mSingleton);
   243 		
   276 
   244 		SDLActivity.nativeInit(new String[]{ String.valueOf(ipc.port), String.valueOf(surfaceWidth), String.valueOf(surfaceHeight), "0", "null", "xeli", "1", "1", "1", "0", "", path});
   277 		SDLActivity.nativeInit(new String[] { String.valueOf(ipc.port),
   245 
   278 				String.valueOf(surfaceWidth), String.valueOf(surfaceHeight),
   246 		//Log.v("SDL", "SDL thread terminated");
   279 				"0", "null", "xeli", "1", "1", "1", "0", "", path });
       
   280 
       
   281 		// Log.v("SDL", "SDL thread terminated");
   247 	}
   282 	}
   248 }
   283 }
   249 
   284 
   250 
       
   251 /**
   285 /**
   252     SDLSurface. This is what we draw on, so we need to know when it's created
   286  * SDLSurface. This is what we draw on, so we need to know when it's created in
   253     in order to do anything useful. 
   287  * order to do anything useful.
   254 
   288  * 
   255     Because of this, that's where we set up the SDL thread
   289  * Because of this, that's where we set up the SDL thread
   256  */
   290  */
   257 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, 
   291 class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
   258 View.OnKeyListener, View.OnTouchListener, SensorEventListener  {
   292 		View.OnKeyListener, View.OnTouchListener, SensorEventListener {
   259 
   293 
   260 	// This is what SDL runs in. It invokes SDL_main(), eventually
   294 	// This is what SDL runs in. It invokes SDL_main(), eventually
   261 	private Thread mSDLThread;    
   295 	private Thread mSDLThread;
   262 
   296 
   263 	// EGL private objects
   297 	// EGL private objects
   264 	private EGLContext  mEGLContext;
   298 	private EGLContext mEGLContext;
   265 	private EGLSurface  mEGLSurface;
   299 	private EGLSurface mEGLSurface;
   266 	private EGLDisplay  mEGLDisplay;
   300 	private EGLDisplay mEGLDisplay;
   267 
   301 
   268 	// Sensors
   302 	// Sensors
   269 	private static SensorManager mSensorManager;
   303 	private static SensorManager mSensorManager;
   270 
   304 
   271 	// Startup    
   305 	private GameConfig config;
   272 	public SDLSurface(Context context) {
   306 
       
   307 	// Startup
       
   308 	public SDLSurface(Context context, GameConfig _config) {
   273 		super(context);
   309 		super(context);
   274 		getHolder().addCallback(this); 
   310 		getHolder().addCallback(this);
   275 
   311 
   276 		setFocusable(true);
   312 		setFocusable(true);
   277 		setFocusableInTouchMode(true);
   313 		setFocusableInTouchMode(true);
   278 		requestFocus();
   314 		requestFocus();
   279 		setOnKeyListener(this); 
   315 		setOnKeyListener(this);
   280 		setOnTouchListener(this);   
   316 		setOnTouchListener(this);
   281 
   317 
   282 		mSensorManager = (SensorManager)context.getSystemService("sensor");  
   318 		mSensorManager = (SensorManager) context.getSystemService("sensor");
       
   319 
       
   320 		config = _config;
   283 	}
   321 	}
   284 
   322 
   285 	// Called when we have a valid drawing surface
   323 	// Called when we have a valid drawing surface
   286 	public void surfaceCreated(SurfaceHolder holder) {
   324 	public void surfaceCreated(SurfaceHolder holder) {
   287 		//Log.v("SDL", "surfaceCreated()");
   325 		// Log.v("SDL", "surfaceCreated()");
   288 
   326 
   289 		enableSensor(Sensor.TYPE_ACCELEROMETER, true);
   327 		enableSensor(Sensor.TYPE_ACCELEROMETER, true);
   290 	}
   328 	}
   291 
   329 
   292 	// Called when we lose the surface
   330 	// Called when we lose the surface
   293 	public void surfaceDestroyed(SurfaceHolder holder) {
   331 	public void surfaceDestroyed(SurfaceHolder holder) {
   294 		//Log.v("SDL", "surfaceDestroyed()");
   332 		// Log.v("SDL", "surfaceDestroyed()");
   295 
   333 
   296 		// Send a quit message to the application
   334 		// Send a quit message to the application
   297 		SDLActivity.nativeQuit();
   335 		SDLActivity.nativeQuit();
   298 
   336 
   299 		// Now wait for the SDL thread to quit
   337 		// Now wait for the SDL thread to quit
   300 		if (mSDLThread != null) {
   338 		if (mSDLThread != null) {
   301 			try {
   339 			try {
   302 				mSDLThread.join();
   340 				mSDLThread.join();
   303 			} catch(Exception e) {
   341 			} catch (Exception e) {
   304 				Log.v("SDL", "Problem stopping thread: " + e);
   342 				Log.v("SDL", "Problem stopping thread: " + e);
   305 			}
   343 			}
   306 			mSDLThread = null;
   344 			mSDLThread = null;
   307 
   345 
   308 			//Log.v("SDL", "Finished waiting for SDL thread");
   346 			// Log.v("SDL", "Finished waiting for SDL thread");
   309 		}
   347 		}
   310 
   348 
   311 		enableSensor(Sensor.TYPE_ACCELEROMETER, false);
   349 		enableSensor(Sensor.TYPE_ACCELEROMETER, false);
   312 	}
   350 	}
   313 
   351 
   314 	// Called when the surface is resized
   352 	// Called when the surface is resized
   315 	public void surfaceChanged(SurfaceHolder holder,
   353 	public void surfaceChanged(SurfaceHolder holder, int format, int width,
   316 			int format, int width, int height) {
   354 			int height) {
   317 		Log.d("SDL", "surfaceChanged()"+ width + " + " + height);
   355 		Log.d("SDL", "surfaceChanged()" + width + " + " + height);
   318 
   356 
   319 		int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
   357 		int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default
   320 		switch (format) {
   358 		switch (format) {
   321 		case PixelFormat.A_8:
   359 		case PixelFormat.A_8:
   322 			Log.v("SDL", "pixel format A_8");
   360 			Log.v("SDL", "pixel format A_8");
   362 		}
   400 		}
   363 		SDLActivity.onNativeResize(width, height, sdlFormat);
   401 		SDLActivity.onNativeResize(width, height, sdlFormat);
   364 
   402 
   365 		// Now start up the C app thread
   403 		// Now start up the C app thread
   366 		if (mSDLThread == null) {
   404 		if (mSDLThread == null) {
   367 			mSDLThread = new Thread(new SDLMain(width, height), "SDLThread"); 
   405 			mSDLThread = new Thread(new SDLMain(width, height, config),
   368 			mSDLThread.start();       
   406 					"SDLThread");
       
   407 			mSDLThread.start();
   369 		}
   408 		}
   370 	}
   409 	}
   371 
   410 
   372 	// unused
   411 	// unused
   373 	public void onDraw(Canvas canvas) {}
   412 	public void onDraw(Canvas canvas) {
   374 
   413 	}
   375 
   414 
   376 	// EGL functions
   415 	// EGL functions
   377 	public boolean initEGL(int majorVersion, int minorVersion) {
   416 	public boolean initEGL(int majorVersion, int minorVersion) {
   378 		Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
   417 		Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "."
       
   418 				+ minorVersion);
   379 
   419 
   380 		try {
   420 		try {
   381 			EGL10 egl = (EGL10)EGLContext.getEGL();
   421 			EGL10 egl = (EGL10) EGLContext.getEGL();
   382 
   422 
   383 			EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
   423 			EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
   384 
   424 
   385 			int[] version = new int[2];
   425 			int[] version = new int[2];
   386 			egl.eglInitialize(dpy, version);
   426 			egl.eglInitialize(dpy, version);
   392 				renderableType = EGL_OPENGL_ES2_BIT;
   432 				renderableType = EGL_OPENGL_ES2_BIT;
   393 			} else if (majorVersion == 1) {
   433 			} else if (majorVersion == 1) {
   394 				renderableType = EGL_OPENGL_ES_BIT;
   434 				renderableType = EGL_OPENGL_ES_BIT;
   395 			}
   435 			}
   396 			int[] configSpec = {
   436 			int[] configSpec = {
   397 					//EGL10.EGL_DEPTH_SIZE,   16,
   437 					// EGL10.EGL_DEPTH_SIZE, 16,
   398 					EGL10.EGL_RENDERABLE_TYPE, renderableType,
   438 					EGL10.EGL_RENDERABLE_TYPE, renderableType, EGL10.EGL_NONE };
   399 					EGL10.EGL_NONE
       
   400 			};
       
   401 			EGLConfig[] configs = new EGLConfig[1];
   439 			EGLConfig[] configs = new EGLConfig[1];
   402 			int[] num_config = new int[1];
   440 			int[] num_config = new int[1];
   403 			if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
   441 			if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config)
       
   442 					|| num_config[0] == 0) {
   404 				Log.e("SDL", "No EGL config available");
   443 				Log.e("SDL", "No EGL config available");
   405 				return false;
   444 				return false;
   406 			}
   445 			}
   407 			EGLConfig config = configs[0];
   446 			EGLConfig config = configs[0];
   408 
   447 
   409 			EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
   448 			EGLContext ctx = egl.eglCreateContext(dpy, config,
       
   449 					EGL10.EGL_NO_CONTEXT, null);
   410 			if (ctx == EGL10.EGL_NO_CONTEXT) {
   450 			if (ctx == EGL10.EGL_NO_CONTEXT) {
   411 				Log.e("SDL", "Couldn't create context");
   451 				Log.e("SDL", "Couldn't create context");
   412 				return false;
   452 				return false;
   413 			}
   453 			}
   414 
   454 
   415 			EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
   455 			EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this,
       
   456 					null);
   416 			if (surface == EGL10.EGL_NO_SURFACE) {
   457 			if (surface == EGL10.EGL_NO_SURFACE) {
   417 				Log.e("SDL", "Couldn't create surface");
   458 				Log.e("SDL", "Couldn't create surface");
   418 				return false;
   459 				return false;
   419 			}
   460 			}
   420 
   461 
   425 
   466 
   426 			mEGLContext = ctx;
   467 			mEGLContext = ctx;
   427 			mEGLDisplay = dpy;
   468 			mEGLDisplay = dpy;
   428 			mEGLSurface = surface;
   469 			mEGLSurface = surface;
   429 
   470 
   430 		} catch(Exception e) {
   471 		} catch (Exception e) {
   431 			Log.v("SDL", e + "");
   472 			Log.v("SDL", e + "");
   432 			for (StackTraceElement s : e.getStackTrace()) {
   473 			for (StackTraceElement s : e.getStackTrace()) {
   433 				Log.v("SDL", s.toString());
   474 				Log.v("SDL", s.toString());
   434 			}
   475 			}
   435 		}
   476 		}
   438 	}
   479 	}
   439 
   480 
   440 	// EGL buffer flip
   481 	// EGL buffer flip
   441 	public void flipEGL() {
   482 	public void flipEGL() {
   442 		try {
   483 		try {
   443 			EGL10 egl = (EGL10)EGLContext.getEGL();
   484 			EGL10 egl = (EGL10) EGLContext.getEGL();
   444 
   485 
   445 			egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
   486 			egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
   446 
   487 
   447 			// drawing here
   488 			// drawing here
   448 
   489 
   449 			egl.eglWaitGL();
   490 			egl.eglWaitGL();
   450 
   491 
   451 			egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
   492 			egl.eglSwapBuffers(mEGLDisplay, mEGLSurface);
   452 
   493 
   453 
   494 		} catch (Exception e) {
   454 		} catch(Exception e) {
       
   455 			Log.v("SDL", "flipEGL(): " + e);
   495 			Log.v("SDL", "flipEGL(): " + e);
   456 			for (StackTraceElement s : e.getStackTrace()) {
   496 			for (StackTraceElement s : e.getStackTrace()) {
   457 				Log.v("SDL", s.toString());
   497 				Log.v("SDL", s.toString());
   458 			}
   498 			}
   459 		}
   499 		}
   460 	}
   500 	}
   461 
   501 
   462 	// Key events
   502 	// Key events
   463 	public boolean onKey(View  v, int keyCode, KeyEvent event) {
   503 	public boolean onKey(View v, int keyCode, KeyEvent event) {
   464 
   504 
   465 		if (event.getAction() == KeyEvent.ACTION_DOWN) {
   505 		if (event.getAction() == KeyEvent.ACTION_DOWN) {
   466 			//Log.v("SDL", "key down: " + keyCode);
   506 			// Log.v("SDL", "key down: " + keyCode);
   467 			SDLActivity.onNativeKeyDown(keyCode);
   507 			SDLActivity.onNativeKeyDown(keyCode);
   468 			return true;
   508 			return true;
   469 		}
   509 		} else if (event.getAction() == KeyEvent.ACTION_UP) {
   470 		else if (event.getAction() == KeyEvent.ACTION_UP) {
   510 			// Log.v("SDL", "key up: " + keyCode);
   471 			//Log.v("SDL", "key up: " + keyCode);
       
   472 			SDLActivity.onNativeKeyUp(keyCode);
   511 			SDLActivity.onNativeKeyUp(keyCode);
   473 			return true;
   512 			return true;
   474 		}
   513 		}
   475 
   514 
   476 		return false;
   515 		return false;
   481 
   520 
   482 		int action = event.getAction();
   521 		int action = event.getAction();
   483 		float x = event.getX();
   522 		float x = event.getX();
   484 		float y = event.getY();
   523 		float y = event.getY();
   485 		float p = event.getPressure();
   524 		float p = event.getPressure();
   486 		// TODO: Anything else we need to pass?        
   525 		// TODO: Anything else we need to pass?
   487 		SDLActivity.onNativeTouch(action, x, y, p);
   526 		SDLActivity.onNativeTouch(action, x, y, p);
   488 		return true;
   527 		return true;
   489 	}
   528 	}
   490 
   529 
   491 	// Sensor events
   530 	// Sensor events
   492 	public void enableSensor(int sensortype, boolean enabled) {
   531 	public void enableSensor(int sensortype, boolean enabled) {
   493 		// TODO: This uses getDefaultSensor - what if we have >1 accels?
   532 		// TODO: This uses getDefaultSensor - what if we have >1 accels?
   494 		if (enabled) {
   533 		if (enabled) {
   495 			mSensorManager.registerListener(this, 
   534 			mSensorManager.registerListener(this,
   496 					mSensorManager.getDefaultSensor(sensortype), 
   535 					mSensorManager.getDefaultSensor(sensortype),
   497 					SensorManager.SENSOR_DELAY_GAME, null);
   536 					SensorManager.SENSOR_DELAY_GAME, null);
   498 		} else {
   537 		} else {
   499 			mSensorManager.unregisterListener(this, 
   538 			mSensorManager.unregisterListener(this,
   500 					mSensorManager.getDefaultSensor(sensortype));
   539 					mSensorManager.getDefaultSensor(sensortype));
   501 		}
   540 		}
   502 	}
   541 	}
   503 
   542 
   504 	public void onAccuracyChanged(Sensor sensor, int accuracy) {
   543 	public void onAccuracyChanged(Sensor sensor, int accuracy) {
   505 		// TODO
   544 		// TODO
   506 	}
   545 	}
   507 
   546 
   508 	public void onSensorChanged(SensorEvent event) {
   547 	public void onSensorChanged(SensorEvent event) {
   509 		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
   548 		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
   510 			SDLActivity.onNativeAccel(event.values[0],
   549 			SDLActivity.onNativeAccel(event.values[0], event.values[1],
   511 					event.values[1],
       
   512 					event.values[2]);
   550 					event.values[2]);
   513 		}
   551 		}
   514 	}
   552 	}
   515 
   553 
   516 }
   554 }
   517