510 likes | 699 Views
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.
E N D
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 • HTML5, JavaScript and CSS3 • C# or Visual Basic .NET and XAML • Immersive 3D using DirectX 11.1 • Combination of DirectX 11.1 and XAML
DirectX fundamentals • Set up Direct3D device and context • Use the Direct3D device structure for low-frequency operations • Use the Context structure for high-frequency operations • Set up your shaders • Set up the swap chain
The Direct3D device • D3D11CreateDevice( • /* … */ • &device, // Returns the Direct3D device created. • &m_featureLevel, // Returns feature level of the device. • &context // Returns the device immediate context. • )
Shaders • structSimpleVertex { • 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, … );
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... • 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 ... • Without XAML • dxgiFactory->CreateSwapChainForCoreWindow( • m_d3dDevice.Get(), • reinterpret_cast<IUnknown*>(window), • &swapChainDesc, • nullptr, • &m_swapChain • )
In-box controls for Windows Store apps Button Grid Text box Clear button Spell checking Check box Radio button Progress ring Progress bar Hyperlink Combo box Context menu Flyout Password Reveal button ListView Semantic Zoom Rating Slider FlipView List box Scroll bar Toggle switch Tooltip Panning indicator App bar
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 To XAML or not to XAML?
Comparison: DirectX vs. XAML • Demo
XAML interop scenarios • SwapChainBackgroundPanel • Example: XAML for UI/HUD, Direct3D for graphics • See XAML DirectX 3D shooting game sample on MSDN
XAML interop scenarios • SurfaceImageSource • Example: Use DirectX inside a XAML element See XAML SurfaceImageSource DirectX Interop Sample on MSDN
XAML interop scenarios • VirtualSurfaceImageSource • Example: Bing maps for the Windows Store See May talk by Jesse Bishop, “Combining XAML and DirectX in Windows Store apps”
CoreWindow and IFrameworkView • CoreWindow • Replacement for Win32 hWnd • Manages screen layout • Provides app input • Using event handler model • IFrameworkView • Effectively the new App Object class • Enables OS to initialize app, deliver core OS resources • You will implement 5 methods
IFrameworkView methods Initialize Load Run Uninitialize SetWindow • Called on application launch • Register application events here • OS assigns CoreWindowto 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
Required IFrameworkView methods • refclassMyFrameworkView : publicIFrameworkView • { • public: • // IFrameworkView Methods • virtualvoid Initialize(CoreApplicationView^ applicationView); • virtualvoid SetWindow(CoreWindow^ window); • virtualvoid Load(String^ entryPoint); • virtualvoid Run(); • virtualvoid Uninitialize(); • // ...
Where to register events • Initialize method • Application-wide events • Example: Suspend and resume • SetWindow method • App window-specific events • Example: Window-size-changed and input events
There are many events • Suspend • Resume • PointerPressed • PointerMoved • KeyDown • KeyUp • SizeChanged • …
Registering the SizeChanged event • m_window->SizeChanged += refnew • TypedEventHandler<CoreWindow^,WindowSizeChangedEventArgs^> ( • this, • &Framework::OnWindowSizeChanged • );
The SizeChanged event • voidFramework::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args) • { usingWindows::UI::ViewManagement; if(ApplicationView::Value == ApplicationViewState::Snapped) { /* snapped specific code */ } /* respond to new window size … */ • } • // See Snap sample on MSDN
Process Lifetime Management (PLM) events • CoreApplication::Suspending += • refnewEventHandler<SuspendingEventArgs^>(this, &Framework::OnSuspending); • CoreApplication::Resuming += • refnewEventHandler<Platform::Object^>(this, &Framework::OnResuming); • // ... • voidFramework::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) { • // Save app state here • } • voidFramework::OnResuming(Platform::Object^ sender, Platform::Object^ args) { • // Restore app state here • }
Getting started • Use the Visual Studio 2012 templates for a quick start • Direct3D template for pure C++ and DirectX • Direct2D template for added XAML support • Reference the samples • Need a feature? Copy and paste from the corresponding sample! • Use the documentation at dev.windows.com
Templates • Demo
Live tiles • usingnamespace Windows::UI::Notifications; • usingnamespace Windows::Data::Xml::Dom; • autotileXml = refnewXmlDocument(); • tileXml->LoadXml("<tile>"…"</tile>"); • TileUpdateManager::CreateTileUpdaterForApplication()->Update( • refnewTileNotification(tileXml) • ); • See App tiles and badges sample on MSDN
Using templates for binding • tileXmlString = "<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
Toast • usingnamespace Windows::UI::Notifications; • usingnamespace Windows::Data::Xml::Dom; • auto toastXml = refnewXmlDocument(); • auto toastXmlString = "<toast>”…”</toast>"; • toastXml->LoadXml(toastXmlString); • auto toast = refnewToastNotification(toastXml); • ToastNotificationManager::CreateToastNotifier()->Show(toast);
Scheduled toast • toastXml->LoadXml(toastXmlString); • auto toast = refnewToastNotification(toastXml); • auto cal = refnew Windows::Globalization::Calendar(); • cal->SetToNow(); • cal->AddSeconds(7); • ToastNotificationManager::CreateToastNotifier()->AddToSchedule(refnewScheduledToastNotification(toastXml,cal->GetDateTime())); • // Remember to declare toast in your app manifest!
App bar and edge gestures • In C++, EdgeGesture->Completedevent fires in three cases • Swipe up from bottom (or down from top) of touch screen • Windows logo key+Z on keyboard • Right mouse button click • Draw app bar using Direct2D
The edge gesture event • usingnamespaceWindows::UI::Input; • EdgeGesture::GetForCurrentView()->Completed += refnew • TypedEventHandler<EdgeGesture^, EdgeGestureEventArgs^>( • this, &InputController::OnCompleted • ); • void InputController::OnCompleted(EdgeGesture^,EdgeGestureEventArgs^){ • // Check device orientation and draw app bar… • }
Charms (Share) • usingnamespace Windows::ApplicationModel::DataTransfer; • DataTransferManager::GetForCurrentView()->DataRequested += refnew • 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!“); /* … */ • });
Charms (Settings) • usingnamespace Windows::UI::ApplicationSettings; • SettingsPane::GetForCurrentView()->CommandsRequested += • refnewTypedEventHandler<SettingsPane^,SettingsPaneCommandsRequestedEventArgs^>( • &MyApp::OnCommandsRequested • );
More charms (Settings) • using namespace Windows::UI::ApplicationSettings; • void MyApp::OnCommandsRequested(SettingsPane^ settingsPane, • SettingsPaneCommandsRequestedEventArgs^ args) • { • UICommandInvokedHandler^ handler = • refnewUICommandInvokedHandler(this, &Scenario::onAudio); • SettingsCommand^ audioCommand = • refnewSettingsCommand("audioPage", "audio", handler); • // Make it visible • args->Request->ApplicationCommands->Append(audioCommand); • // Add additional settings ... • }
More charms (Settings) • voidScenario::OnAudio(IUICommand^ command) { • // Draw audio settings using Direct2D ... • } • Source code from App Settings Sample on MSDN
Saving data • Windows::Storage::ApplicationDatafor 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
Saving to local hard drive • auto settingsValues = • Windows::Storage::ApplicationData::Current->LocalSettings->Values; • float m_time = • safe_cast<IPropertyValue^>( • settingsValues->Lookup(“time") • )->GetSingle(); • settingsValues->Insert(“time", • PropertyValue::CreateSingle(m_time) • );
Saving to the cloud • auto 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
Handling trials • Select a time period for your trial • Let the Windows Store handle the rest
Using the Windows Store API • Store API is in Windows::ApplicationModel::Store • Use CurrentApp::LicenseInformationto 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
Marble Maze • Demo
Time to code • Get 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 templates • Live tiles • Toast • AppBar/EdgeGesture • Saving data locally and to the cloud • Features that can help you make money (for example, trials and in-app purchases)
Resources • Previous talks • “Combining XAML and DirectX in Windows Store apps” by Jesse Bishop • “Monetization Strategies for Windows 8 Games” by ShaiHinitz • Other resources at //Build • Check out the graphics talks (Build 3-112 and 3-113) • Build 3-101 for more info about live tiles and toast • Build 3-126 for more info about app state • Build 3-121 for more info about how to make money Please submit session evals on the Build Windows 8 App or at http://aka.ms/BuildSessions
Resources • Develop: http://msdn.microsoft.com/en-US/windows/apps/br229512 • Design: http://design.windows.com/ • Samples: http://code.msdn.microsoft.com/windowsapps/Windows-8-Modern-Style-App-Samples • Videos: http://channel9.msdn.com/Windows Please submit session evals by using the Build Windows 8 app or at http://aka.ms/BuildSessions