Quellcode durchsuchen

include android classes to src/os/android, TODO: tool->android_builder

mikymod vor 12 Jahren
Ursprung
Commit
60eaf90c1e

+ 21 - 0
src/os/android/AndroidManifest.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="crown.android"
+      android:versionCode="1"
+      android:versionName="1.0">
+    <uses-sdk
+        android:minSdkVersion="9"/>
+
+    <application android:label="Crown" >
+        <activity android:name="CrownActivity"
+                  android:label="Crown"
+				  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
+				  android:screenOrientation="landscape" 
+				  android:configChanges="orientation|keyboardHidden">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+</manifest> 

+ 141 - 0
src/os/android/CrownActivity.java

@@ -0,0 +1,141 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+package crown.android;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.WindowManager;
+import android.view.MotionEvent;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.content.Context;
+import android.widget.Toast;
+import android.content.res.AssetManager;
+import android.view.Surface;
+import android.view.SurfaceView;
+import android.view.SurfaceHolder;
+
+import crown.android.CrownEnum;
+
+/**
+*	BootStrap of Android Application
+*/
+public class CrownActivity extends Activity
+{
+
+	// Debug
+	public static String TAG = "CrownActivity";
+
+	// Resource attributes
+    static AssetManager 		mAssetManager;
+
+	// Input attributes
+	private CrownTouch 			mTouch;
+	private CrownSensor			mSensor;
+
+	// Graphic attributes
+	static CrownSurfaceView		mWindow;
+
+//-----------------------------------------------------------------------------
+    public void onCreate(Bundle savedInstanceState)
+    {
+        super.onCreate(savedInstanceState);
+
+		// init AssetManager
+		mAssetManager = getAssets();
+		CrownLib.initAssetManager(mAssetManager);
+
+		// init Native Window
+        mWindow = new CrownSurfaceView(this);
+        setContentView(mWindow);
+
+		// Init Input
+		mTouch = new CrownTouch(this);
+		mSensor = new CrownSensor(this);
+    }
+
+//-----------------------------------------------------------------------------
+	public void onStart()
+	{
+		super.onStart();
+
+	}
+
+//-----------------------------------------------------------------------------
+	public void onRestart()
+	{
+		super.onRestart();
+	}
+
+//-----------------------------------------------------------------------------
+	public void onResume()
+	{
+		super.onResume();
+		
+		// init accelerometer
+		if (!mSensor.startListening(this))
+		{
+			finish();
+		}
+	}
+
+//-----------------------------------------------------------------------------
+	public void onPause()
+	{
+		super.onPause();
+	}
+
+//-----------------------------------------------------------------------------
+	public void onStop()
+	{
+		super.onStop();
+
+		// stop accelerometer
+		mSensor.stopListening();
+	}
+
+//-----------------------------------------------------------------------------
+	public void onDestroy()
+	{
+		super.onDestroy();
+	}
+
+//-----------------------------------------------------------------------------
+	public boolean onTouchEvent(MotionEvent event)
+	{
+		mTouch.onTouch(event);
+        return super.onTouchEvent(event);
+	}
+
+//-----------------------------------------------------------------------------
+	public boolean hasMultiTouchSupport(Context context)
+	{
+		return context.getPackageManager().hasSystemFeature("android.hardware.touchscreen.multitouch");
+	}
+}

+ 16 - 0
src/os/android/CrownEnum.java

@@ -0,0 +1,16 @@
+package crown.android;
+
+public class CrownEnum
+{
+	// OSEventType enum in OS.h
+	public static int OSET_NONE				= 0;
+	public static int OSET_KEY_PRESS		= 1;
+	public static int OSET_KEY_RELEASE		= 2;
+	public static int OSET_BUTTON_PRESS		= 3;
+	public static int OSET_BUTTON_RELEASE	= 4;
+	public static int OSET_MOTION_NOTIFY	= 5;
+	public static int OSET_TOUCH_DOWN 		= 6;
+	public static int OSET_TOUCH_MOVE		= 7;
+	public static int OSET_TOUCH_UP			= 8;
+	public static int OSET_ACCELEROMETER	= 9; 
+}

+ 56 - 0
src/os/android/CrownLib.java

@@ -0,0 +1,56 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+package crown.android;
+
+import android.content.res.AssetManager;
+import android.view.Surface;
+
+public class CrownLib
+{
+	static 
+	{
+		System.loadLibrary("luajit-5.1");
+		System.loadLibrary("crown");
+	}
+	
+	// Device functions
+	public static native void 		init();
+	public static native void 		frame();
+	public static native void 		shutdown();
+	public static native boolean 	isInit();
+	public static native boolean	isRunning();
+
+	// AssetManager functions
+	public static native void 		initAssetManager(AssetManager assetManager);
+
+	// InputManager functions
+	public static native void 		pushIntEvent(int type, int a, int b, int c, int d);
+	public static native void 		pushFloatEvent(int type, float a, float b, float c, float d);
+
+	// Window functions
+	public static native void		setWindow(Surface window);
+	public static native void 		setDisplaySize(int width, int height);
+}

+ 185 - 0
src/os/android/CrownSensor.java

@@ -0,0 +1,185 @@
+package crown.android;
+
+import java.util.List;
+import java.lang.Math;
+
+import android.content.Context;
+import android.util.Log;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+
+import crown.android.CrownEnum;
+
+
+/**
+*	CrownSensor manage sensors for Android Devices.
+*/
+public class CrownSensor
+{
+    private final float MIN_VALUE = -1.0f;
+    private final float MAX_VALUE = 1.0f;
+    private final static float RAD2DEG = (float) (180.0f / Math.PI);
+
+    private SensorManager sensorManager;
+    private Sensor mAccelerometerSensor;
+    private Sensor mCompassSensor;
+    private SensorEventListener mAccelerometerEventListener;
+    private SensorEventListener mCompassEventListener;
+    private boolean isAccelerometerAvailable;
+    private boolean isCompassAvailable;
+
+    private float[] mRotationMatrix;
+    private float[] mGravity;
+    private float[] mLastGravity;
+    private float[] mBufferedAccelGData;
+    private float[] mGeoMagn;
+    private float[] mBufferedMagnetData;
+    private float[] mOrientation;
+
+    private float[] mRotAngle;
+ 
+
+//-----------------------------------------------------------------------------------
+    public CrownSensor(Context context)
+    {
+        mRotationMatrix = new float[16];
+        mGravity = new float[3];
+        mLastGravity = new float[3];
+        mGeoMagn = new float[3];
+        mOrientation = new float[3];
+        mRotAngle = new float[3];
+
+        isAccelerometerAvailable = context.getPackageManager().hasSystemFeature("android.hardware.sensor.accelerometer");
+        isCompassAvailable = context.getPackageManager().hasSystemFeature("android.hardware.sensor.compass");
+
+        if (hasAccelerometerSupport())
+        {
+            mAccelerometerEventListener = new SensorEventListener()
+            {
+                public void onAccuracyChanged(Sensor sensor, int accuracy) 
+                {
+                }
+
+                public void onSensorChanged(SensorEvent event)
+                {
+                    mGravity[0] = (mGravity[0] * 2 + event.values[0]) * 0.33334f * RAD2DEG;
+                    mGravity[1] = (mGravity[1] * 2 + event.values[1]) * 0.33334f * RAD2DEG;
+                    mGravity[2] = (mGravity[2] * 2 + event.values[2]) * 0.33334f * RAD2DEG;
+
+                    norm();
+
+                    CrownLib.pushFloatEvent(CrownEnum.OSET_ACCELEROMETER, mGravity[0], mGravity[1], mGravity[2], 0.0f);
+                }
+            };           
+        }
+
+        if (hasCompassSupport())
+        {
+            mCompassEventListener = new SensorEventListener()
+            {
+                public void onAccuracyChanged(Sensor sensor, int accuracy) 
+                {
+                }
+
+                public void onSensorChanged(SensorEvent event)
+                {
+                    mGeoMagn[0] = (mGeoMagn[0] + event.values[0]) * 0.5f;
+                    mGeoMagn[1] = (mGeoMagn[1] + event.values[1]) * 0.5f;
+                    mGeoMagn[2] = (mGeoMagn[2] + event.values[2]) * 0.5f; 
+
+                    // CrownLib.pushFloatEvent(CrownEnum.OSET_ACCELEROMETER, mGeoMagn[0], mGeoMagn[1], mGeoMagn[2], 0.0f);
+                }                
+            };
+        }
+    }
+
+//-----------------------------------------------------------------------------------
+    public boolean startListening(Context context)
+    {
+        sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
+
+        mAccelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
+        mCompassSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
+
+        sensorManager.registerListener(mAccelerometerEventListener, mAccelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
+        sensorManager.registerListener(mCompassEventListener, mCompassSensor, SensorManager.SENSOR_DELAY_GAME);
+
+        return true;
+    }
+
+//-----------------------------------------------------------------------------------
+    public void stopListening()
+    {
+    	sensorManager.unregisterListener(mAccelerometerEventListener);
+        sensorManager.unregisterListener(mCompassEventListener);
+    }
+
+//-----------------------------------------------------------------------------------
+    public boolean hasAccelerometerSupport()
+    {
+        return isAccelerometerAvailable;       
+    }
+
+//-----------------------------------------------------------------------------------
+    public boolean hasCompassSupport()
+    {   
+        return isCompassAvailable;
+    }
+
+ //-----------------------------------------------------------------------------------
+//     private void lowPassFiltering(float x, float y, float z)
+//     {
+//         float updateFreq = 30; // match this to your update speed
+//         float cutOffFreq = 0.9f;
+//         float timeRC = 1.0f / cutOffFreq;
+//         float dt = 1.0f / updateFreq;
+//         float filterConstant = timeRC / (dt + timeRC);
+//         float alpha = filterConstant;                 
+//         float kAccelerometerMinStep = 0.033f;
+//         float kAccelerometerNoiseAttenuation = 3.0f;
+
+//         float d = clamp((Math.abs(norm(mGravity[0], mGravity[1], mGravity[2]) - norm(x, y, z)) / kAccelerometerMinStep - 1.0f), MIN_VALUE, MAX_VALUE);
+//         alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0f - d) * filterConstant;
+
+//         mGravity[0] = (float) (alpha * (mGravity[0] + x - mLastGravity[0]));
+//         mGravity[1] = (float) (alpha * (mGravity[1] + y - mLastGravity[1]));
+//         mGravity[2] = (float) (alpha * (mGravity[2] + z - mLastGravity[2]));
+
+//         mLastGravity[0] = mGravity[0];
+//         mLastGravity[1] = mGravity[1];
+//         mLastGravity[2] = mGravity[2];
+//     }
+
+//-----------------------------------------------------------------------------------
+    private float clamp(float v, float min, float max)
+    {
+        if (v < min)
+        {
+            v = min;
+        }
+        else if (v > max)
+        {
+            v = max;
+        }
+
+        return v;
+    }
+
+//-----------------------------------------------------------------------------------
+    private void norm()
+    {
+        float v = mGravity[0] * mGravity[0] + mGravity[1] * mGravity[1] + mGravity[2] * mGravity[2];
+        float magnitude = (float)Math.sqrt(v);
+        float invMagnitude = 1 / magnitude;
+
+        mGravity[0] *= invMagnitude;
+        mGravity[1] *= invMagnitude;
+        mGravity[2] *= invMagnitude;
+
+        mGravity[0] = clamp(mGravity[0], MIN_VALUE, MAX_VALUE);
+        mGravity[1] = clamp(mGravity[1], MIN_VALUE, MAX_VALUE);
+        mGravity[2] = clamp(mGravity[2], MIN_VALUE, MAX_VALUE);
+    }
+}

+ 44 - 0
src/os/android/CrownSurfaceView.java

@@ -0,0 +1,44 @@
+package crown.android;
+
+import android.content.Context;
+import android.view.Surface;
+import android.view.SurfaceView;
+import android.view.SurfaceHolder;
+
+public class CrownSurfaceView extends SurfaceView implements SurfaceHolder.Callback
+{
+//-----------------------------------------------------------------------------
+	public CrownSurfaceView(Context context)
+	{
+		super(context);
+		this.getHolder().addCallback(this);
+	}
+
+//-----------------------------------------------------------------------------
+    @Override
+    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
+    {    
+    	if (CrownLib.isRunning())
+    	{
+    		CrownLib.frame();
+    	}
+    }
+
+//-----------------------------------------------------------------------------
+    @Override
+    public void surfaceCreated(SurfaceHolder holder) 
+    {
+        CrownLib.setWindow(holder.getSurface());
+    	CrownLib.init();
+    }
+
+//-----------------------------------------------------------------------------
+    @Override
+    public void surfaceDestroyed(SurfaceHolder holder) 
+    {  
+    	if (CrownLib.isInit())
+    	{
+    		CrownLib.shutdown();
+    	}
+    }
+}

+ 64 - 0
src/os/android/CrownTouch.java

@@ -0,0 +1,64 @@
+package crown.android;
+
+import android.content.Context;
+import android.util.Log;
+import android.view.MotionEvent;
+
+
+/**
+* CrownTouch manages touch and gesture events passing them to Crown Engine
+*/
+
+//TODO: gestures doesn't work...fix them
+public class CrownTouch
+{
+	private boolean 				isListening;
+
+//-----------------------------------------------------------------------------------
+	public CrownTouch(Context context)
+	{
+		isListening = false;
+	}
+
+//-----------------------------------------------------------------------------------
+	public void onTouch(MotionEvent event)
+	{
+		final int pointerIndex = event.getActionIndex();
+		final int pointerCount = event.getPointerCount();
+
+		final int pointerId = event.getPointerId(pointerIndex);
+		final float x = event.getX(pointerIndex);
+		final float y = event.getY(pointerIndex);
+
+		final int actionMasked = event.getActionMasked();
+
+		switch (actionMasked) 
+		{	
+			case MotionEvent.ACTION_DOWN:
+			case MotionEvent.ACTION_POINTER_DOWN:
+			{
+				CrownLib.pushIntEvent(CrownEnum.OSET_TOUCH_DOWN, pointerId, (int)x, (int)y, 0);
+				break;			
+			}
+
+			case MotionEvent.ACTION_UP:
+			case MotionEvent.ACTION_POINTER_UP:
+			case MotionEvent.ACTION_OUTSIDE:
+			case MotionEvent.ACTION_CANCEL:
+			{
+				CrownLib.pushIntEvent(CrownEnum.OSET_TOUCH_UP, pointerId, (int)x, (int)y, 0);
+				break;			
+			}
+			
+			case MotionEvent.ACTION_MOVE:
+			{
+				for (int index = 0; index < pointerCount; index++)
+				{
+					CrownLib.pushIntEvent(CrownEnum.OSET_TOUCH_MOVE, event.getPointerId(index), (int)event.getX(index), (int)event.getY(index), 0);
+				}
+
+				break;
+			}
+		}
+	}
+}