Game Development Reference
In-Depth Information
We'll also add some member variables for caching the gyroscope values and a flag
that we'll use to handle the fact that some Android devices return gyroscope values
in degrees per second rather than radians per second. Add the following code to the
bottom of the class definition:
// Cached gyroscope values
private float x;
private float y;
private float z;
// Are the results in degrees/s or radians/s
private boolean mUsesDegrees;
Before we start implementing the EDK itself, we'll add a couple of private helper
functions to allow us to access the Android
SensorManager
and gyroscope
Sensor
instances that will allow us to retrieve the current gyroscope data. Add the following
two methods at the beginning of the class definition:
// Helper function for accessing the Android SensorManager
private SensorManager GetSensorManager()
{
Context lContext = (Context) LoaderActivity.m_Activity;
SensorManager lSensorManager = (SensorManager)
lContext.getSystemService(Context.SENSOR_SERVICE);
return lSensorManager;
}
// Helper function for accessing the Android Gyroscope Sensor
private Sensor GetGyroscopeSensor()
{
SensorManager lSensorManager = GetSensorManager();
if (lSensorManager == null)
return null;
Sensor lGyroscope =
lSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
return lGyroscope;
}
The
GetSensorManager
method accesses the global
SensorManager
instance by
using the main
Context
class of the Marmalade application. We do this using
Marmalade's
LoaderActivity
class that contains a member variable that is a
reference to the main Android SDK
Activity
class instance. This reference can then
be cast into a reference to a
Context
instance, since
Activity
derives from
Context
.
Search WWH ::
Custom Search