Game Development Reference
In-Depth Information
The source directory will now contain a new directory called android that contains
two files Gyroscope.java and Gyroscope_platform.cpp . The former is where we
can add Java code that uses the Android SDK code to implement our extension API.
The latter is the C++ code that our Marmalade project will call, which in turn calls
the Java implementation code.
It is possible to implement the entire extension in the Gyroscope_platform.cpp ile
by using the Java Native Interface ( JNI ) to access and call into the compiled Java
code; but this adds an extra layer of complexity and implementing the extension in
Java is normally a far easier proposition!
Implementing an Android extension
To implement the gyroscope code for Android, we will need to edit the file source\
android\Gyroscope.java . First we need to make a reference to the Java classes
we'll be using; so change the list of import declarations at the top of the file to look
like this:
import com.ideaworks3d.marmalade.LoaderAPI;
import com.ideaworks3d.marmalade.LoaderActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
The first two imports allow us access to some helper functions that provide
access to things such as the application's main Activity class (all applications in
Android need to be derived from this base class). We'll need this to access some
system resources.
The remaining imports are for the parts of the Android SDK that we will need to use
to access the gyroscope data.
The EDK system has generated a Java class called Gyroscope that contains stubs
for all the methods we need to implement. We will need to alter the class definition
slightly, though, as we need to implement some methods that will receive gyroscope
updates. Change the class definition as follows:
class Gyroscope implements SensorEventListener
SensorEventListener is a Java interface that our class must implement in order to
receive sensor events (in our case, gyroscope data).
 
Search WWH ::




Custom Search