Developing a Windows Store app using C++ and DirectX

51
Developing a Windows Store app using C++ and DirectX Phil Napieralski Program Manager 3-109

description

Developing a Windows Store app using C++ and DirectX. Phil Napieralski Program Manager 3-109. Agenda. Fundamentals (DirectX) What is XAML? Why interop ? More fundamentals (C++) Registering events Windows Store app features Making money. Graphics language of choice. - PowerPoint PPT Presentation

Transcript of Developing a Windows Store app using C++ and DirectX

Page 1: Developing a Windows Store app using C++ and DirectX

Developing a Windows Store app using C++ and DirectXPhil NapieralskiProgram Manager3-109

Page 2: Developing a Windows Store app using C++ and DirectX

Fundamentals (DirectX)What is XAML? Why interop?More fundamentals (C++)Registering eventsWindows Store app featuresMaking money

Agenda

Page 3: Developing a Windows Store app using C++ and DirectX

Graphics language of choice

HTML5, JavaScript and CSS3

C# or Visual Basic .NET and XAML

Immersive 3D using DirectX 11.1

Combination of DirectX 11.1 and XAML

Page 4: Developing a Windows Store app using C++ and DirectX

Fundamentals (DirectX)

Page 5: Developing a Windows Store app using C++ and DirectX

DirectX fundamentalsSet up Direct3D device and contextUse the Direct3D device structure for low-frequency operationsUse the Context structure for high-frequency operations

Set up your shaders

Set up the swap chain

Page 6: Developing a Windows Store app using C++ and DirectX

D3D11CreateDevice( /* … */ &device, // Returns the Direct3D device created. &m_featureLevel, // Returns feature level of the device. &context // Returns the device immediate context.)

The Direct3D device

Page 7: Developing a Windows Store app using C++ and DirectX

struct SimpleVertex {

XMFLOAT3 pos;

XMFLOAT3 color;

};

// Create the shaders

m_d3dDevice->CreateVertexShader( … );

m_d3dDevice->CreatePixelShader( … );

// Define the input to the vertex shader

m_d3dDevice->CreateInputLayout( … &vertexShaderByteCode, &layoutdesc, … );

Shaders

Page 8: Developing a Windows Store app using C++ and DirectX

Without XAML

dxgiFactory->CreateSwapChainForCoreWindow(

m_d3dDevice.Get(),

reinterpret_cast<IUnknown*>(window),

&swapChainDesc,

nullptr,

&m_swapChain

)  

The swap chain

With XAML (SwapChainBackgroundPanel)

dxgiFactory->CreateSwapChainForComposition(

m_d3dDevice.Get(),                  

&swapChainDesc,                  

nullptr,         

&m_swapChain                  

// Get XAML element into ComPtr<ISwapChainBackgroundPanelNative>

// Set its swap chain...

Page 9: Developing a Windows Store app using C++ and DirectX

Without XAML

dxgiFactory->CreateSwapChainForCoreWindow(

m_d3dDevice.Get(),

reinterpret_cast<IUnknown*>(window),

&swapChainDesc,

nullptr,

&m_swapChain

)  

The swap chain

With XAML (SwapChainBackgroundPanel)

dxgiFactory->CreateSwapChainForComposition(

m_d3dDevice.Get(),                  

&swapChainDesc,                  

nullptr,         

&m_swapChain                  

)

// Get XAML element into ComPtr<ISwapChainBackgroundPanelNative> ...

// Set its swap chain ...

Page 10: Developing a Windows Store app using C++ and DirectX

What is XAML? Why interop?

Page 11: Developing a Windows Store app using C++ and DirectX

In-box controls for Windows Store apps

App bar

List box

Hyperlink

Check boxProgress bar

Text box

Password

Progress ring

Tooltip

GridButton

FlipView

Combo box

Scroll bar

Context menu

Slider

Toggle switch

Semantic Zoom

Panning indicator

Rating

ListView

Flyout

Radio button

Clear button

Reveal button

Spell checking

Page 12: Developing a Windows Store app using C++ and DirectX

To XAML or not to XAML?

XAML interoperates nicely with DirectX in various scenarios

Combine ease of XAML for 2D, power of DirectX for 3D

Make the XAML interop decision up front

Page 13: Developing a Windows Store app using C++ and DirectX

Demo

Comparison: DirectX vs. XAML

Page 14: Developing a Windows Store app using C++ and DirectX

SwapChainBackgroundPanelExample: XAML for UI/HUD, Direct3D for graphics

XAML interop scenarios

See XAML DirectX 3D shooting game sample on MSDN

Page 15: Developing a Windows Store app using C++ and DirectX

SurfaceImageSourceExample: Use DirectX inside a XAML element

XAML interop scenarios

See XAML SurfaceImageSource DirectX Interop Sample on MSDN

Page 17: Developing a Windows Store app using C++ and DirectX

More fundamentals (C++)

Page 18: Developing a Windows Store app using C++ and DirectX

CoreWindow and IFrameworkViewCoreWindowReplacement for Win32 hWndManages screen layoutProvides app inputUsing event handler model

IFrameworkViewEffectively the new App Object classEnables OS to initialize app, deliver core OS resourcesYou will implement 5 methods

Page 19: Developing a Windows Store app using C++ and DirectX

IFrameworkView methods

Called on application launch

Register application events here

OS assigns CoreWindow to your app

Register window events here

Parameter tells us from where the app was launched

Put your rendering loop in here

3 seconds to get here to start handling events

Rarely executed

Generally leave empty

Initialize Load Run UninitializeSetWindow

Page 20: Developing a Windows Store app using C++ and DirectX

ref class MyFrameworkView : public IFrameworkView{public:     // IFrameworkView Methods    virtual void Initialize(CoreApplicationView^ applicationView);    virtual void SetWindow(CoreWindow^ window);    virtual void Load(String^ entryPoint);    virtual void Run();    virtual void Uninitialize();

// ... 

Required IFrameworkView methods

Page 21: Developing a Windows Store app using C++ and DirectX

Registering events

Page 22: Developing a Windows Store app using C++ and DirectX

Where to register eventsInitialize methodApplication-wide eventsExample: Suspend and resume

SetWindow methodApp window-specific eventsExample: Window-size-changed and input events

Page 23: Developing a Windows Store app using C++ and DirectX

There are many eventsSuspendResumePointerPressedPointerMovedKeyDownKeyUpSizeChanged…

Page 24: Developing a Windows Store app using C++ and DirectX

m_window->SizeChanged += ref new

TypedEventHandler<CoreWindow^,WindowSizeChangedEventArgs^> (

this,

&Framework::OnWindowSizeChanged

);

Registering the SizeChanged event

Page 25: Developing a Windows Store app using C++ and DirectX

void Framework::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)

{

using Windows::UI::ViewManagement;if(ApplicationView::Value == ApplicationViewState::Snapped) {

/* snapped specific code */}/* respond to new window size … */

}

// See Snap sample on MSDN

The SizeChanged event

Page 26: Developing a Windows Store app using C++ and DirectX

CoreApplication::Suspending += ref new EventHandler<SuspendingEventArgs^>(this, &Framework::OnSuspending);

CoreApplication::Resuming += ref new EventHandler<Platform::Object^>(this, &Framework::OnResuming);

// ...

void Framework::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) { // Save app state here}

void Framework::OnResuming(Platform::Object^ sender, Platform::Object^ args) { // Restore app state here}

Process Lifetime Management (PLM) events

Page 27: Developing a Windows Store app using C++ and DirectX

Get going quickly

Page 28: Developing a Windows Store app using C++ and DirectX

Getting startedUse the Visual Studio 2012 templates for a quick startDirect3D template for pure C++ and DirectXDirect2D template for added XAML support

Reference the samplesNeed a feature? Copy and paste from the corresponding sample!

Use the documentation at dev.windows.com

Page 29: Developing a Windows Store app using C++ and DirectX

Demo

Templates

Page 30: Developing a Windows Store app using C++ and DirectX

Windows Store app features

Page 31: Developing a Windows Store app using C++ and DirectX

Live tilesusing namespace Windows::UI::Notifications;using namespace Windows::Data::Xml::Dom;

auto tileXml = ref new XmlDocument();tileXml->LoadXml("<tile>"…"</tile>");

TileUpdateManager::CreateTileUpdaterForApplication()->Update(ref new TileNotification(tileXml)

);

See App tiles and badges sample on MSDN

Page 32: Developing a Windows Store app using C++ and DirectX

Using templates for bindingtileXmlString = "<tile>"+ "<visual>”+ "<binding template=‘TileWideText03'>"+ "<text id='1'>My first tile notification!</text>"+ "</binding>"+ "</visual>"+ "</tile>";

// See Build 3-101, “Alive with activity” by Kraig Brockschmidt

See App tiles and badges sample on MSDN

Page 33: Developing a Windows Store app using C++ and DirectX

Toastusing namespace Windows::UI::Notifications;using namespace Windows::Data::Xml::Dom;

auto toastXml = ref new XmlDocument();auto toastXmlString = "<toast>”…”</toast>";toastXml->LoadXml(toastXmlString);

auto toast = ref new ToastNotification(toastXml);

ToastNotificationManager::CreateToastNotifier()->Show(toast);

Page 34: Developing a Windows Store app using C++ and DirectX

Scheduled toasttoastXml->LoadXml(toastXmlString);

auto toast = ref new ToastNotification(toastXml);

auto cal = ref new Windows::Globalization::Calendar();cal->SetToNow();cal->AddSeconds(7);ToastNotificationManager::CreateToastNotifier()->AddToSchedule(ref new ScheduledToastNotification(toastXml,cal->GetDateTime()));

// Remember to declare toast in your app manifest!

Page 35: Developing a Windows Store app using C++ and DirectX

App bar and edge gesturesIn C++, EdgeGesture->Completed event fires in three casesSwipe up from bottom (or down from top) of touch screenWindows logo key+Z on keyboardRight mouse button click

Draw app bar using Direct2D

Page 36: Developing a Windows Store app using C++ and DirectX

using namespace Windows::UI::Input;

EdgeGesture::GetForCurrentView()->Completed += ref new

TypedEventHandler<EdgeGesture^, EdgeGestureEventArgs^>(

this, &InputController::OnCompleted

);

void InputController::OnCompleted(EdgeGesture^,EdgeGestureEventArgs^){

// Check device orientation and draw app bar…

}

The edge gesture event

Page 37: Developing a Windows Store app using C++ and DirectX

Charms (Share)using namespace Windows::ApplicationModel::DataTransfer;DataTransferManager::GetForCurrentView()->DataRequested += ref new TypedEventHandler<DataTransferManager^,DataRequestedEventArgs^>( &App::onShare );

void App::onShare(DataTransferManager^ sender, DataRequestedEventArgs^ args){auto requestData = args->Request->Data;requestData->Properties->Title = "High Score";requestData->Properties->Description = “My High Score";requestData->SetText(“I just got a new high score!“);

/* … */});

Page 38: Developing a Windows Store app using C++ and DirectX

Charms (Settings)using namespace Windows::UI::ApplicationSettings;SettingsPane::GetForCurrentView()->CommandsRequested += ref new TypedEventHandler<SettingsPane^,SettingsPaneCommandsRequestedEventArgs^>(

&MyApp::OnCommandsRequested );

Page 39: Developing a Windows Store app using C++ and DirectX

More charms (Settings)using namespace Windows::UI::ApplicationSettings;void MyApp::OnCommandsRequested(SettingsPane^ settingsPane, SettingsPaneCommandsRequestedEventArgs^ args){ UICommandInvokedHandler^ handler = ref new UICommandInvokedHandler(this, &Scenario::onAudio);

SettingsCommand^ audioCommand = ref new SettingsCommand("audioPage", "audio", handler);

// Make it visible args->Request->ApplicationCommands->Append(audioCommand);

// Add additional settings ...}

Page 40: Developing a Windows Store app using C++ and DirectX

More charms (Settings)void Scenario::OnAudio(IUICommand^ command) { // Draw audio settings using Direct2D ...}

Source code from App Settings Sample on MSDN

Page 41: Developing a Windows Store app using C++ and DirectX

Saving dataWindows::Storage::ApplicationData for app data

Use File I/O or cloud services for big data

If the app crashes, don’t load this data!

Data physically at %localappdata%\packages

Page 42: Developing a Windows Store app using C++ and DirectX

Saving to local hard driveauto settingsValues = Windows::Storage::ApplicationData::Current->LocalSettings->Values;float m_time = safe_cast<IPropertyValue^>(

settingsValues->Lookup(“time") )->GetSingle();settingsValues->Insert(“time", PropertyValue::CreateSingle(m_time));

Page 43: Developing a Windows Store app using C++ and DirectX

Saving to the cloudauto settingsValues = Windows::Storage::ApplicationData::Current->RoamingSettings->Values;float m_time = safe_cast<IPropertyValue^>(

settingsValues->Lookup(“time") )->GetSingle();settingsValues->Insert(“time", PropertyValue::CreateSingle(m_time));

// See Build 3-126, “The Story of State” by Kraig Brockschmidt

Page 44: Developing a Windows Store app using C++ and DirectX

Making money

Page 45: Developing a Windows Store app using C++ and DirectX

Select a time period for your trial

Let the Windows Store handle the rest

Handling trials

Page 46: Developing a Windows Store app using C++ and DirectX

Using the Windows Store APIStore API is in Windows::ApplicationModel::Store

Use CurrentApp::LicenseInformation to check trial information

When testing, use CurrentAppSimulator

Check out the following resources for more information:Build 3-121, “Making money with your app on the Windows Store” by Drew Robbins“Monetization Strategies for Windows 8 Games” by Shai Hinitz from GDC 2012

Page 47: Developing a Windows Store app using C++ and DirectX

Demo

Marble Maze

Page 48: Developing a Windows Store app using C++ and DirectX

Time to codeGet Windows RTM trial and Visual Studio 2012 from dev.windows.com

Get going quickly with the templates

Understand where the fundamentals fit in

Try adding Windows Store features to the templatesLive tilesToastAppBar/EdgeGestureSaving data locally and to the cloudFeatures that can help you make money (for example, trials and in-app purchases)

Page 49: Developing a Windows Store app using C++ and DirectX

Previous talks“Combining XAML and DirectX in Windows Store apps” by Jesse Bishop“Monetization Strategies for Windows 8 Games” by Shai Hinitz

Other resources at //BuildCheck out the graphics talks (Build 3-112 and 3-113)Build 3-101 for more info about live tiles and toastBuild 3-126 for more info about app stateBuild 3-121 for more info about how to make money

Resources

Please submit session evals on the Build Windows 8 App or at http://aka.ms/BuildSessions

Page 51: Developing a Windows Store app using C++ and DirectX

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.