Vibrations

Applications and games can interact with the user in many ways, including by vibrations. You can use them to indicate situations that require user attention, as well as to improve the game experience. The Windows Phone 8 platform provides mechanisms that allow developers to easily use vibrations in their projects.

Concept

The vibration controller can be used both in the managed and native development approaches. However, there is no feedback about using it in the emulator, thus you need to test the project on the phone. While using the vibration controller, you should specify a duration indicating a period of time while the phone is vibrating. It has to be in the range <0;5> seconds.

In the exemplary game, the vibrations will be used when the user selects any option in the Menu screen (100 ms), the rocket reaches the planet (500 ms), and the rocket crashes (1000 ms). You will learn how to implement these scenarios in the following part of this chapter.

Implementation

Handling vibrations in both the managed and native development approaches is performed in a similar way. In both cases, you need to add the same using directives, as well as use classes with the same names.

MenuViewModel.cs

At the beginning, you will implement the vibrations in the managed part. To do so, you should slightly modify the view model class for the Menu screen.

At the beginning, you add the private field (m_vibration) representing the vibration controller. You can get its data by calling the GetDefault static method:

private VibrationDevice m_vibration = VibrationDevice.GetDefault();

You need to call the Vibrate method (with a parameter representing a duration) to start the vibration controller. In this scenario, you will launch it for 100 milliseconds. Of course, vibrations should be started only if the user has the suitable settings enabled, as shown as follows:

public void NavigateToScreen(string key)
{ (...)
  if (Settings.Vibrations)
  {
    this.m_vibration.Vibrate(TimeSpan.FromMilliseconds(100));
  }
}

GameRenderer.h

You can also use the vibration controller in the native part of your project. At the beginning, you should add the following using directive:

using namespace Windows::Phone::Devices::Notification;

Then, you add two private fields and one method to the GameRenderer class:

VibrationDevice^ m_vibration;
void Vibrate(int ms);
bool m_vibrationsEnabled;

The first field (m_vibration) performs the same role as in case of the managed part. The Vibrate method is added to simplify the implementation code, because you will start vibration in more than one situation. The second private field (m_vibrationsEnabled) indicates whether vibrations are currently allowed.

You also add the public inline method, named EnableVibrations, which makes it possible to set a value of the m_vibrationsEnabled field:

void EnableVibrations(bool enabled) 
{
  m_vibrationsEnabled = enabled;
}

GameRenderer.cpp

The next modifications are necessary in the GameRenderer.cpp file. In the constructor, you set a default value for the m_vibration field by using the GetDefault static method:

GameRenderer::GameRenderer() : (...)
  m_vibration(VibrationDevice::GetDefault()) { }

Inside the Vibrate method, you declare the local variable of the TimeSpan type, which represents a duration. Its value needs to be calculated according to the formula ms * 10000, were ms denotes a number of milliseconds. Such a calculation is necessary, because the Duration property of the TimeSpan instance requires data in other units. Here, value one means 100 ns (0,1 μs or 0,0001 ms), thus you need to multiply given value by 10000.

Then, you call the Vibrate method (on the VibrationDevice instance), similarly as in case of the managed approach. It is worth mentioning that if vibrations are not allowed, no operations are performed, as presented as follows:

void GameRenderer::Vibrate(int ms)
{
  if (m_vibrationsEnabled)
  {
    TimeSpan vibrationPeriod;
    vibrationPeriod.Duration = ms * 10000;
    m_vibration->Vibrate(vibrationPeriod);
  }
}

At the end, you just call the Vibrate method in situations when vibration should start, that is, when the rocket reaches the planet and when it crashes:

void GameRenderer::ReachPlanet() 
{
  Vibrate(500); (...)
}
void GameRenderer::Crash() 
{
  Vibrate(1000); (...)
}

Direct3DInterop.h

An additional change is necessary to pass the value indicating whether vibrations are allowed, from the managed part to the native one. To achieve this goal, you add a new public inline method to the Direct3DInterop class, which calls the EnableVibrations method from the GameRenderer class, as shown as follows:

void EnableVibrations(bool enabled)
{
  m_renderer->EnableVibrations(enabled);
}

GamePage.xaml.cs

The newly added method (inside the Direct3DInterop class) will be called from the Game page code behind. Thus, you need to slightly modify the GamePage.xaml.cs file.

Here, you call the EnableVibrations method passing the suitable value from settings, inside the DrawingSurface_Loaded method:

private void DrawingSurface_Loaded(object sender,
  RoutedEventArgs e)
{
  if (m_d3dInterop == null)
  { (...)
    m_d3dInterop.EnableVibrations(Settings.Vibrations);
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.203.96