April 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building Windows Phone Applications using Direct3D

  • March 11, 2013
  • By Vipul Patel
  • Send Email »
  • More Articles »

Introduction

With the launch of the Windows Phone 8.0 platform, Microsoft introduced native support for Direct3D, the technology that is part of the DirectX application programming interface. Support for Direct3D allows game developers, which have built games for the PC platform using Direct3D, to port their gaming application to target to Windows Phone. DirectX is a well-established engine to build games and there are numerous games already out today that use it. With this support, game developers can easily port their games to the Windows Phone platform.

A Direct3D application is written entirely in native code (C++) and only uses Direct3D technology for rendering the UI.

Direct3D Application Model for Windows Phone

The application model for Direct3D for the Windows Phone platform is a little different from how it works for other platforms.

When the Activated event is raised as part of application launch, IActivatedEventArgs.PreviousExecutionState only supports NotRunning and Suspended values. Values of Running, Terminated and ClosedByUser are not supported on Windows Phone 8.0 as the execution states of the application before this activation.

When the Activated event is raised as part of application launch, IActivatedEventArgs.Kind only supports Launch and PickerReturned. Values of Search, ShareTarget, File, Protocol, FIleOpenPicker, FileSavePicker, CachedFileUpdater, ContactPicker, Device, PrintTaskSettings, and CameraSettings are not supported as reasons for which a Windows Phone Direct3D application can be activated.

Windows Phone Direct3D applications cannot call CoreApplication.CreateNewView method. Calling it will result in an exception being thrown.

Background execution is not supported in Windows Phone Direct3D applications.

All Windows Phone tasks are executed asynchronously on a separate thread. However, CoreProcessEventsOption.ProcessOneIfPresent enumeration is not supported on Windows Phone 8.0 platform.

Lastly, Windows Phone Direct3D applications cannot call CoCreateInstanceEx  to instantiate a COM interface. Instead, they must use the CoCreateInstanceFromApp function to restrict it to within an app container.

Now, that we have seen the intricacies of the Windows Phone Direct3D application model, let's get hands-on and create a sample application.

Hands On

Start Visual Studio 2012 and create a new Project of type “Windows Phone Direct3D app (Native Only)”. The template can be found under Visual C++ -> Windows Phone category.

Call the project WindowsPhoneDirect3DDemo and click OK.

Create a new Project
Create a new Project

Visual Studio will show a default project with numerous files that contain the logic created.

If you compile and run the application, you can see it render a rotating cube.

Rotating Cube
Rotating Cube

Now, let's dig into what was created by Visual Studio. We see that Solution Explorer shows us the following files:

Solution Explorer
Solution Explorer

The CubeRender.* file contains the logic about the cube.

The Direct3Dbase.* file contains Direct3D code to work with device resources. This file contains methods that initialize and dispose device resources.

Finally,  WindowsPhoneDirect3DDemo.* files (which are named after the project name we gave earlier) contain code, which invokes the application.

If we explore the application  manifest file (WMAppManifest.xml), we can see that (in the code mode) the RuntimeType is defined as “Modern Native”.

// Listing WMAppManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2012/deployment" AppPlatformVersion="8.0">

  <DefaultLanguage xmlns="" code="en-US"/>

  <App xmlns="" ProductID="{00add631-b083-451a-9d5e-bbbbc7f07377}" Title="WindowsPhoneDirect3DDemo" RuntimeType="Modern Native" Version="1.0.0.0" Genre="apps.normal"  Author="WindowsPhoneDirect3DDemo author" Description="Simple Direct3D application" Publisher="WindowsPhoneDirect3DDemo" PublisherID="{07b472e8-7d4a-4e6e-9303-3c3d5506a97b}">

    <IconPath IsRelative="true" IsResource="false">Assets\ApplicationIcon.png</IconPath>

    <Capabilities>

      <Capability Name="ID_CAP_NETWORKING" />

      <Capability Name="ID_CAP_MEDIALIB_AUDIO" />

      <Capability Name="ID_CAP_MEDIALIB_PLAYBACK" />

    </Capabilities>

    <Tasks>

      <DefaultTask Name="_default" ImagePath="WindowsPhoneDirect3DDemo.exe" ImageParams="" />

    </Tasks>

    <Tokens>

      <PrimaryToken TokenID="WindowsPhoneDirect3DDemoToken" TaskName="_default">

        <TemplateFlip>

          <SmallImageURI IsRelative="true" IsResource="false">Assets\Tiles\FlipCycleTileSmall.png</SmallImageURI>

          <Count>0</Count>

          <BackgroundImageURI IsRelative="true" IsResource="false">Assets\Tiles\FlipCycleTileMedium.png</BackgroundImageURI>

          <Title>WindowsPhoneDirect3DDemo</Title>

          <BackContent></BackContent>

          <BackBackgroundImageURI></BackBackgroundImageURI>

          <BackTitle></BackTitle>

          <DeviceLockImageURI></DeviceLockImageURI>

          <HasLarge></HasLarge>

        </TemplateFlip>

      </PrimaryToken>

    </Tokens>

    <ScreenResolutions>

      <ScreenResolution Name="ID_RESOLUTION_WVGA" />

      <ScreenResolution Name="ID_RESOLUTION_WXGA" />

      <ScreenResolution Name="ID_RESOLUTION_HD720P" />

    </ScreenResolutions>

  </App>

</Deployment>

Readers are encouraged to look at Direct3D documentation available at http://msdn.microsoft.com/en-us/library/windows/desktop/hh309466(v=vs.85).aspx to discover suitable enhancements you can make to this demo.

A sample listing of this project is available here.

Summary

In this article, we learned about building Direct3D applications targeting the windows Phone 8.0 platform. I hope you have found this information useful.

About the Author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com


Tags: gaming, Windows Phone 8, Visual Studio 2012




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel