diff -r 88e25840f532 -r 2ad2a4261b56 project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java --- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java Thu Aug 04 17:34:21 2011 +0200 +++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java Thu Aug 04 17:34:57 2011 +0200 @@ -6,6 +6,9 @@ import javax.microedition.khronos.egl.EGLDisplay; import javax.microedition.khronos.egl.EGLSurface; +import org.hedgewars.mobile.EngineProtocol.EngineProtocolNetwork; +import org.hedgewars.mobile.EngineProtocol.GameConfig; + import android.app.Activity; import android.content.Context; import android.graphics.Canvas; @@ -27,9 +30,8 @@ import android.view.SurfaceView; import android.view.View; - /** - SDL Activity + * SDL Activity */ public class SDLActivity extends Activity { @@ -43,27 +45,28 @@ // Load the .so static { - System.loadLibrary("SDL"); System.loadLibrary("SDL_image"); System.loadLibrary("mikmod"); System.loadLibrary("SDL_net"); System.loadLibrary("SDL_mixer"); System.loadLibrary("SDL_ttf"); + System.loadLibrary("lua5.1"); System.loadLibrary("hwengine"); System.loadLibrary("main"); } // Setup protected void onCreate(Bundle savedInstanceState) { - //Log.v("SDL", "onCreate()"); + // Log.v("SDL", "onCreate()"); super.onCreate(savedInstanceState); // So we can call stuff from static callbacks mSingleton = this; // Set up the surface - mSurface = new SDLSurface(getApplication()); + GameConfig config = getIntent().getParcelableExtra("config"); + mSurface = new SDLSurface(getApplication(), config); setContentView(mSurface); SurfaceHolder holder = mSurface.getHolder(); holder.setType(SurfaceHolder.SURFACE_TYPE_GPU); @@ -71,12 +74,13 @@ // Events protected void onPause() { - //Log.v("SDL", "onPause()"); + // Log.v("SDL", "onPause()"); super.onPause(); + } protected void onResume() { - //Log.v("SDL", "onResume()"); + // Log.v("SDL", "onResume()"); super.onResume(); } @@ -87,7 +91,7 @@ Handler commandHandler = new Handler() { public void handleMessage(Message msg) { if (msg.arg1 == COMMAND_CHANGE_TITLE) { - setTitle((String)msg.obj); + setTitle((String) msg.obj); } } }; @@ -102,16 +106,22 @@ // C functions we call public static native void nativeInit(String[] argv); + public static native void nativeQuit(); + public static native void onNativeResize(int x, int y, int format); + public static native void onNativeKeyDown(int keycode); + public static native void onNativeKeyUp(int keycode); - public static native void onNativeTouch(int action, float x, - float y, float p); + + public static native void onNativeTouch(int action, float x, float y, + float p); + public static native void onNativeAccel(float x, float y, float z); + public static native void nativeRunAudioThread(); - // Java functions called from C public static boolean createGLContext(int majorVersion, int minorVersion) { @@ -130,29 +140,48 @@ // Audio private static Object buf; - public static Object audioInit(int sampleRate, boolean is16Bit, boolean isStereo, int desiredFrames) { - int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO : AudioFormat.CHANNEL_CONFIGURATION_MONO; - int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT : AudioFormat.ENCODING_PCM_8BIT; + public static Object audioInit(int sampleRate, boolean is16Bit, + boolean isStereo, int desiredFrames) { + int channelConfig = isStereo ? AudioFormat.CHANNEL_CONFIGURATION_STEREO + : AudioFormat.CHANNEL_CONFIGURATION_MONO; + int audioFormat = is16Bit ? AudioFormat.ENCODING_PCM_16BIT + : AudioFormat.ENCODING_PCM_8BIT; int frameSize = (isStereo ? 2 : 1) * (is16Bit ? 2 : 1); - Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + " " + (is16Bit ? "16-bit" : "8-bit") + " " + ((float)sampleRate / 1000f) + "kHz, " + desiredFrames + " frames buffer"); + Log.v("SDL", "SDL audio: wanted " + (isStereo ? "stereo" : "mono") + + " " + (is16Bit ? "16-bit" : "8-bit") + " " + + ((float) sampleRate / 1000f) + "kHz, " + desiredFrames + + " frames buffer"); // Let the user pick a larger buffer if they really want -- but ye // gods they probably shouldn't, the minimums are horrifyingly high // latency already - desiredFrames = Math.max(desiredFrames, (AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat) + frameSize - 1) / frameSize); + desiredFrames = Math.max( + desiredFrames, + (AudioTrack.getMinBufferSize(sampleRate, channelConfig, + audioFormat) + frameSize - 1) + / frameSize); mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, - channelConfig, audioFormat, desiredFrames * frameSize, AudioTrack.MODE_STREAM); + channelConfig, audioFormat, desiredFrames * frameSize, + AudioTrack.MODE_STREAM); audioStartThread(); - 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"); + 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"); if (is16Bit) { buf = new short[desiredFrames * (isStereo ? 2 : 1)]; } else { - buf = new byte[desiredFrames * (isStereo ? 2 : 1)]; + buf = new byte[desiredFrames * (isStereo ? 2 : 1)]; } return buf; } @@ -171,14 +200,14 @@ } public static void audioWriteShortBuffer(short[] buffer) { - for (int i = 0; i < buffer.length; ) { + for (int i = 0; i < buffer.length;) { int result = mAudioTrack.write(buffer, i, buffer.length - i); if (result > 0) { i += result; } else if (result == 0) { try { Thread.sleep(1); - } catch(InterruptedException e) { + } catch (InterruptedException e) { // Nom nom } } else { @@ -189,14 +218,14 @@ } public static void audioWriteByteBuffer(byte[] buffer) { - for (int i = 0; i < buffer.length; ) { + for (int i = 0; i < buffer.length;) { int result = mAudioTrack.write(buffer, i, buffer.length - i); if (result > 0) { i += result; } else if (result == 0) { try { Thread.sleep(1); - } catch(InterruptedException e) { + } catch (InterruptedException e) { // Nom nom } } else { @@ -210,12 +239,12 @@ if (mAudioThread != null) { try { mAudioThread.join(); - } catch(Exception e) { + } catch (Exception e) { Log.v("SDL", "Problem stopping audio thread: " + e); } mAudioThread = null; - //Log.v("SDL", "Finished waiting for audio thread"); + // Log.v("SDL", "Finished waiting for audio thread"); } if (mAudioTrack != null) { @@ -226,72 +255,81 @@ } /** - Simple nativeInit() runnable + * Simple nativeInit() runnable */ class SDLMain implements Runnable { private int surfaceWidth, surfaceHeight; - public SDLMain(int width, int height){ + private GameConfig config; + + public SDLMain(int width, int height, GameConfig _config) { + config = _config; surfaceWidth = width; surfaceHeight = height; } + public void run() { // Runs SDL_main() - EngineProtocolNetwork ipc = new EngineProtocolNetwork(EngineProtocolNetwork.MODE_GAME); - - String path = Utils.getDownloadPath(SDLActivity.mSingleton) + "Data"; - - SDLActivity.nativeInit(new String[]{ String.valueOf(ipc.port), String.valueOf(surfaceWidth), String.valueOf(surfaceHeight), "0", "null", "xeli", "1", "1", "1", "0", "", path}); + EngineProtocolNetwork ipc = new EngineProtocolNetwork(config); + + String path = Utils.getDownloadPath(SDLActivity.mSingleton); - //Log.v("SDL", "SDL thread terminated"); + SDLActivity.nativeInit(new String[] { String.valueOf(ipc.port), + String.valueOf(surfaceWidth), String.valueOf(surfaceHeight), + "0", "null", "xeli", "1", "1", "1", "0", "", path }); + + // Log.v("SDL", "SDL thread terminated"); } } - /** - SDLSurface. This is what we draw on, so we need to know when it's created - in order to do anything useful. - - Because of this, that's where we set up the SDL thread + * SDLSurface. This is what we draw on, so we need to know when it's created in + * order to do anything useful. + * + * Because of this, that's where we set up the SDL thread */ -class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, -View.OnKeyListener, View.OnTouchListener, SensorEventListener { +class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, + View.OnKeyListener, View.OnTouchListener, SensorEventListener { // This is what SDL runs in. It invokes SDL_main(), eventually - private Thread mSDLThread; + private Thread mSDLThread; // EGL private objects - private EGLContext mEGLContext; - private EGLSurface mEGLSurface; - private EGLDisplay mEGLDisplay; + private EGLContext mEGLContext; + private EGLSurface mEGLSurface; + private EGLDisplay mEGLDisplay; // Sensors private static SensorManager mSensorManager; - // Startup - public SDLSurface(Context context) { + private GameConfig config; + + // Startup + public SDLSurface(Context context, GameConfig _config) { super(context); - getHolder().addCallback(this); + getHolder().addCallback(this); setFocusable(true); setFocusableInTouchMode(true); requestFocus(); - setOnKeyListener(this); - setOnTouchListener(this); + setOnKeyListener(this); + setOnTouchListener(this); - mSensorManager = (SensorManager)context.getSystemService("sensor"); + mSensorManager = (SensorManager) context.getSystemService("sensor"); + + config = _config; } // Called when we have a valid drawing surface public void surfaceCreated(SurfaceHolder holder) { - //Log.v("SDL", "surfaceCreated()"); + // Log.v("SDL", "surfaceCreated()"); enableSensor(Sensor.TYPE_ACCELEROMETER, true); } // Called when we lose the surface public void surfaceDestroyed(SurfaceHolder holder) { - //Log.v("SDL", "surfaceDestroyed()"); + // Log.v("SDL", "surfaceDestroyed()"); // Send a quit message to the application SDLActivity.nativeQuit(); @@ -300,21 +338,21 @@ if (mSDLThread != null) { try { mSDLThread.join(); - } catch(Exception e) { + } catch (Exception e) { Log.v("SDL", "Problem stopping thread: " + e); } mSDLThread = null; - //Log.v("SDL", "Finished waiting for SDL thread"); + // Log.v("SDL", "Finished waiting for SDL thread"); } enableSensor(Sensor.TYPE_ACCELEROMETER, false); } // Called when the surface is resized - public void surfaceChanged(SurfaceHolder holder, - int format, int width, int height) { - Log.d("SDL", "surfaceChanged()"+ width + " + " + height); + public void surfaceChanged(SurfaceHolder holder, int format, int width, + int height) { + Log.d("SDL", "surfaceChanged()" + width + " + " + height); int sdlFormat = 0x85151002; // SDL_PIXELFORMAT_RGB565 by default switch (format) { @@ -364,21 +402,23 @@ // Now start up the C app thread if (mSDLThread == null) { - mSDLThread = new Thread(new SDLMain(width, height), "SDLThread"); - mSDLThread.start(); + mSDLThread = new Thread(new SDLMain(width, height, config), + "SDLThread"); + mSDLThread.start(); } } // unused - public void onDraw(Canvas canvas) {} - + public void onDraw(Canvas canvas) { + } // EGL functions public boolean initEGL(int majorVersion, int minorVersion) { - Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion); + Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + + minorVersion); try { - EGL10 egl = (EGL10)EGLContext.getEGL(); + EGL10 egl = (EGL10) EGLContext.getEGL(); EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY); @@ -394,25 +434,26 @@ renderableType = EGL_OPENGL_ES_BIT; } int[] configSpec = { - //EGL10.EGL_DEPTH_SIZE, 16, - EGL10.EGL_RENDERABLE_TYPE, renderableType, - EGL10.EGL_NONE - }; + // EGL10.EGL_DEPTH_SIZE, 16, + EGL10.EGL_RENDERABLE_TYPE, renderableType, EGL10.EGL_NONE }; EGLConfig[] configs = new EGLConfig[1]; int[] num_config = new int[1]; - if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) { + if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) + || num_config[0] == 0) { Log.e("SDL", "No EGL config available"); return false; } EGLConfig config = configs[0]; - EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null); + EGLContext ctx = egl.eglCreateContext(dpy, config, + EGL10.EGL_NO_CONTEXT, null); if (ctx == EGL10.EGL_NO_CONTEXT) { Log.e("SDL", "Couldn't create context"); return false; } - EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null); + EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, + null); if (surface == EGL10.EGL_NO_SURFACE) { Log.e("SDL", "Couldn't create surface"); return false; @@ -427,7 +468,7 @@ mEGLDisplay = dpy; mEGLSurface = surface; - } catch(Exception e) { + } catch (Exception e) { Log.v("SDL", e + ""); for (StackTraceElement s : e.getStackTrace()) { Log.v("SDL", s.toString()); @@ -440,7 +481,7 @@ // EGL buffer flip public void flipEGL() { try { - EGL10 egl = (EGL10)EGLContext.getEGL(); + EGL10 egl = (EGL10) EGLContext.getEGL(); egl.eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null); @@ -450,8 +491,7 @@ egl.eglSwapBuffers(mEGLDisplay, mEGLSurface); - - } catch(Exception e) { + } catch (Exception e) { Log.v("SDL", "flipEGL(): " + e); for (StackTraceElement s : e.getStackTrace()) { Log.v("SDL", s.toString()); @@ -460,15 +500,14 @@ } // Key events - public boolean onKey(View v, int keyCode, KeyEvent event) { + public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { - //Log.v("SDL", "key down: " + keyCode); + // Log.v("SDL", "key down: " + keyCode); SDLActivity.onNativeKeyDown(keyCode); return true; - } - else if (event.getAction() == KeyEvent.ACTION_UP) { - //Log.v("SDL", "key up: " + keyCode); + } else if (event.getAction() == KeyEvent.ACTION_UP) { + // Log.v("SDL", "key up: " + keyCode); SDLActivity.onNativeKeyUp(keyCode); return true; } @@ -483,7 +522,7 @@ float x = event.getX(); float y = event.getY(); float p = event.getPressure(); - // TODO: Anything else we need to pass? + // TODO: Anything else we need to pass? SDLActivity.onNativeTouch(action, x, y, p); return true; } @@ -492,11 +531,11 @@ public void enableSensor(int sensortype, boolean enabled) { // TODO: This uses getDefaultSensor - what if we have >1 accels? if (enabled) { - mSensorManager.registerListener(this, - mSensorManager.getDefaultSensor(sensortype), + mSensorManager.registerListener(this, + mSensorManager.getDefaultSensor(sensortype), SensorManager.SENSOR_DELAY_GAME, null); } else { - mSensorManager.unregisterListener(this, + mSensorManager.unregisterListener(this, mSensorManager.getDefaultSensor(sensortype)); } } @@ -507,11 +546,9 @@ public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { - SDLActivity.onNativeAccel(event.values[0], - event.values[1], + SDLActivity.onNativeAccel(event.values[0], event.values[1], event.values[2]); } } } -