Skip to content

Commit

Permalink
更新libcocos2dx的src代码为cocos2d-x-3.17版本之下的
Browse files Browse the repository at this point in the history
  • Loading branch information
1019238091 committed Aug 3, 2018
1 parent cdab8ce commit 5f4460d
Show file tree
Hide file tree
Showing 27 changed files with 1,451 additions and 717 deletions.

This file was deleted.

@@ -1,5 +1,6 @@
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Expand Down Expand Up @@ -47,7 +48,11 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
private final Context mContext;
private final SensorManager mSensorManager;
private final Sensor mAccelerometer;
private final Sensor mCompass;
private final int mNaturalOrientation;
final float[] accelerometerValues = new float[3];
final float[] compassFieldValues = new float[3];
static final float ALPHA = 0.25f; // if ALPHA = 1 OR 0, no filter applies.

// ===========================================================
// Constructors
Expand All @@ -58,6 +63,7 @@ public Cocos2dxAccelerometer(final Context context) {

this.mSensorManager = (SensorManager) this.mContext.getSystemService(Context.SENSOR_SERVICE);
this.mAccelerometer = this.mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
this.mCompass = this.mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

final Display display = ((WindowManager) this.mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
this.mNaturalOrientation = display.getOrientation();
Expand All @@ -67,62 +73,82 @@ public Cocos2dxAccelerometer(final Context context) {
// Getter & Setter
// ===========================================================

public void enable() {
public void enableCompass() {
this.mSensorManager.registerListener(this, this.mCompass, SensorManager.SENSOR_DELAY_GAME);
}

public void enableAccel() {
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
}

public void setInterval(float interval) {
// Honeycomb version is 11
if(android.os.Build.VERSION.SDK_INT < 11) {
public void setInterval(float interval) {
// Honeycomb version is 11
if(android.os.Build.VERSION.SDK_INT < 11) {
this.mSensorManager.registerListener(this, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
} else {
//convert seconds to microseconds
this.mSensorManager.registerListener(this, this.mAccelerometer, (int)(interval*100000));
this.mSensorManager.registerListener(this, this.mAccelerometer, (int)(interval*1000000));
}
}

public void disable() {
this.mSensorManager.unregisterListener(this);
}

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public void onSensorChanged(final SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return;
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
final float z = sensorEvent.values[2];

// needed by VR code
this.accelerometerValues[0] = x;
this.accelerometerValues[1] = y;
this.accelerometerValues[2] = z;

/*
* Because the axes are not swapped when the device's screen orientation
* changes. So we should swap it here. In tablets such as Motorola Xoom,
* the default orientation is landscape, so should consider this.
*/
final int orientation = this.mContext.getResources().getConfiguration().orientation;

if ((orientation == Configuration.ORIENTATION_LANDSCAPE) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
final float tmp = x;
x = -y;
y = tmp;
} else if ((orientation == Configuration.ORIENTATION_PORTRAIT) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
final float tmp = x;
x = y;
y = -tmp;
}

// Invert axes for reverse landscape and reverse portrait
int rotation = Cocos2dxHelper.getActivity().getWindowManager().getDefaultDisplay().getRotation();
if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
x = -x;
y = -y;
}

Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,sensorEvent.timestamp);

/*
if(BuildConfig.DEBUG) {
Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
}
*/
}

float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
final float z = sensorEvent.values[2];

/*
* Because the axes are not swapped when the device's screen orientation
* changes. So we should swap it here. In tablets such as Motorola Xoom,
* the default orientation is landscape, so should consider this.
*/
final int orientation = this.mContext.getResources().getConfiguration().orientation;

if ((orientation == Configuration.ORIENTATION_LANDSCAPE) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
final float tmp = x;
x = -y;
y = tmp;
} else if ((orientation == Configuration.ORIENTATION_PORTRAIT) && (this.mNaturalOrientation != Surface.ROTATION_0)) {
final float tmp = x;
x = y;
y = -tmp;
}

Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,sensorEvent.timestamp);

/*
if(BuildConfig.DEBUG) {
Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
// needed by VR code
this.compassFieldValues[0] = sensorEvent.values[0];
this.compassFieldValues[1] = sensorEvent.values[1];
this.compassFieldValues[2] = sensorEvent.values[2];
}
*/
}

@Override
Expand All @@ -133,7 +159,7 @@ public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
// Methods
// Native method called from Cocos2dxGLSurfaceView (To be in the same thread)
// ===========================================================

public static native void onSensorChanged(final float x, final float y, final float z, final long timestamp);

// ===========================================================
Expand Down

0 comments on commit 5f4460d

Please sign in to comment.