Game Development Reference
In-Depth Information
On iOS, we use an Objective-C class called CMMotionManager to gain access to
gyroscope data, so we first need to let our code know about this class by changing
the list of included files as follows:
#include <CoreMotion/CoreMotion.h>
#include "Gyroscope_internal.h"
We'll also declare a global pointer to a CMMotionManager instance that we will use
throughout the rest of our code. Add the following line after the include files:
CMMotionManager* gpMotionManager = nil;
We'll need to allocate an instance of this class before we can access the gyroscope.
Luckily, the EDK build script has generated a function called GyroscopeInit_
platform that is automatically called for us when we use the extension in our
project, so this will make a good place to allocate a new CMMotionManager instance,
as shown in the following code:
s3eResult GyroscopeInit_platform()
{
gpMotionManager = [[CMMotionManager alloc] init];
return S3E_RESULT_SUCCESS;
}
We also need to free the instance when our application is terminated and
once again the EDK build script has come to our rescue with the function
GyroscopeTerminate_platform . We need to modify this function so that it stops
the gyroscope, if it is still active, and then releases the CMMotionManager instance.
Here's the completed function:
void GyroscopeTerminate_platform()
{
GyroscopeStop_platform();
[gpMotionManager release];
}
The rest of the implementation is actually surprisingly easy, as the
CMMotionManager class works in a very similar manner to the API we have
chosen for the extension. We'll start with checking to see if gyroscope hardware
is available. The GyroscopeSupported_platform function looks like this:
bool GyroscopeSupported_platform()
{
return gpMotionManager.gyroAvailable;
}
 
Search WWH ::




Custom Search