Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Agenda
Quick summary of the platform Step through the process of development from a programmers perspective: App Initialization Opening a Window Rendering Graphics Loading and Saving Adding Input Controls Adding Sound Effects Adding an Art Pipeline This talks sample code is in C++
Platform Overview
Desktop apps
DX C C++
Communication & Data
Model Controller
JavaScript
C C++
C# VB
System Services
Win32
.NET SL
Kernel
Graphics
Audio
Local Services
Direct3D
WASAPI XAudio2
PLM
AppData Contracts
Direct2D
HTML
XAML
Windows Runtime
All WinRT APIs are native WinRT APIs feel natural in C++, C#/VB, and JavaScript Reuse of portable C++ code just works WinRT APIs provide rich access to devices, OS, and services HTML5/CSS/JavaScript and XAML are great for Metro style apps Providing a rich set of controls Native DirectX is great for Metro style apps
Updated C++ language support File->New Project templates for native DirectX C++ apps DirectX HLSL shader compilation and syntax highlighting Packaging compiled HLSL shaders into the .appx package Support for other asset types in MSBuild and previewer Visualization, processing and packaging of Textures, meshes, shaders, and audio Debugging DirectX API calls
Chapter 1/6
App Initialization
ref class MyApp : public IFrameworkView { public: MyApp(); // IFrameworkView Methods virtual void Initialize(CoreApplicationView^ applicationView); virtual void SetWindow(CoreWindow^ window); virtual void Load(String^ entryPoint); virtual void Run(); virtual void Uninitialize();
Chapter 2/6
void MyApp::SetWindow(CoreWindow^ window) { window->SizeChanged += ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>( this, &MyApp::OnWindowSizeChanged); ApplicationView::GetForCurrentView()->ViewStateChanged += ref new TypedEventHandler <ApplicationView^, ApplicationViewStateChangedEventArgs^>( this, &MyApp::OnViewStateChanged); }
Resize Handler
void DirectXApp::OnWindowSizeChanged( _In_ CoreWindow^ sender, _In_ WindowSizeChangedEventArgs^ args ) { if (m_window->Bounds.Width != m_windowBounds.Width || m_window->Bounds.Height != m_windowBounds.Height) { m_d2dContext->SetTarget(nullptr); m_d2dTargetBitmap = nullptr; m_renderTargetView = nullptr; m_depthStencilView = nullptr; CreateWindowSizeDependentResources(); } }
{ DisplayOrientations::None; DisplayOrientations::Landscape; DisplayOrientations::LandscapeFlipped; DisplayOrientations::Portrait; DisplayOrientations::PortraitFlipped; } using namespace Windows::Graphics::Display; DisplayProperties::AutoRotationPreferences = DisplayOrientations::Landscape | DisplayOrientations::LandscapeFlipped; // Enable rotation by OS/Accelerometer // Lock rotation by OS/Accelerometer // and enable this orientation
Chapter 3/6
Rendering Graphics
// Obtain the final swap chain for this window from the DXGI factory.
// // // // // the Direct3D device that will render to it IUnknown interface on our core window double or triple buffered, stereo, etc. allow on all displays the resulting swap chain object
nullptr
void myApp::Render() { m_d3dContext->OMSetRenderTargets( // rebind every frame! 1, m_renderTargetView.GetAddressOf(), m_depthStencilView.Get() ); if (!m_loadingComplete) return; // only draw the cube once it's loaded // (this is async)
m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_d3dContext->IASetInputLayout(m_inputLayout.Get());
m_d3dContext->VSSetConstantBuffers( 0, 1, m_constantBuffer.GetAddressOf() ); m_d3dContext->VSSetShader( m_vertexShader.Get(), nullptr, 0 ); m_d3dContext->PSSetShader( m_pixelShader.Get(), nullptr, 0 ); m_d3dContext->DrawIndexed( m_indexCount, 0, 0 ); }
Chapter 4/7
Running App
suspending resuming
Suspended App
Low Memory
Terminated App
float f = 1.0f; // task defined with capturing lambda task<int>([=]() { return foo(f); }).then([](int x) // continuation lambda argument is return value of previous { bar(x); // continuation using existing function baz() }).then(baz);
void MyApp::OnSuspending(Object^ sender, SuspendingEventArgs^ args) { SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral(); task<void>([=]() { auto localState = ApplicationData::Current->LocalSettings->Values; auto roamingState = ApplicationData::Current->RoamingSettings->Values; localState->Insert( "GameTime", PropertyValue::CreateSingle(m_gameTime)); roamingState->Insert( "MaxLevel", PropertyValue::CreateUInt32(m_maxLevelUnlocked)); }).then([=]() { deferral->Complete(); }); }
void MyApp::Load(String^ entryPoint) { LoadMyGameStateAsync().then([=]() { auto localState = ApplicationData::Current->LocalSettings->Values; auto roamingState = ApplicationData::Current->RoamingSettings->Values; m_gameTime = safe_cast<IPropertyValue^> (localState->Lookup("GameTime"))->GetSingle(); m_maxLevelUnlocked = safe_cast<IPropertyValue^> (roamingState->Lookup("MaxLevel"))->GetUInt32(); }).then([=]() { m_loadingComplete = true; }); }
task<byte*> LoadSkyAsync() { auto folder = Package::Current->InstalledLocation; return task<StorageFile^>( folder->GetFileAsync("sky.dds")).then([](StorageFile^ file){ return FileIO::ReadBufferAsync(file); }).then([](IBuffer^ buffer){ auto fileData = ref new Array<byte>(buffer->Length); DataReader::FromBuffer(buffer)->ReadBytes(fileData); return fileData->Data; }); } ... LoadSkyAsync().then([=](byte* skyTextureData) { CreateTexture(skyTextureData); m_loadingComplete = true; });
Chapter 5/7
Adding Input
// position of contact
// Arrow keys or WASD example auto upKeyState = window->GetKeyAsyncState(VirtualKey::Up); auto wKeyState = window->GetAsyncKeyState(VirtualKey::W);
using Windows::Devices::Sensors; // Get current reading from sensor OrientationSensorReading^ orientationReading = m_orientationsensor->GetCurrentReading(); SensorQuaternion^ quat = orientationReading->Quaternion; // Transform quaternion from device orientation space to world space // Orientation space is Z-up, right-handed coordinate system // World space is Y-up, left-handed coordinate system XMVECTOR orientationQuat = XMVectorSet(-quat->X, quat->Z, quat->Y, -quat->W); // Create a rotation matrix from the quaternion // This matrix can be used to rotate an object inside the scene to match // the rotation of the device XMMATRIX rotXMMatrix = XMMatrixRotationQuaternion(orientationQuat);
if ( m_xinputState.Gamepad.wButtons & XINPUT_GAMEPAD_A ) { m_aButtonWasPressed = true; } else if ( m_aButtonWasPressed ) { m_aButtonWasPressed = false; // Trigger once, only on button release TriggerSoundEffect(); }
Chapter 6/7
// Create the XAudio2 engine and mastering voice on the default audio device
// Load all audio data for the sound effect into a single in-memory buffer new // Create a single source voice for a sound effect
// Trigger sound effect: queue in-memory buffer for playback and start the voice
Chapter 7/7
Author-time Asset
shader.hlsl
Build Step
Run-time Asset
shader.cso
Packager
Package
texture.bmp texture.dds myApp .appx
mesh.obj
mesh.vbo
music.wav
music.wma
Installer
Run-time Asset
shader.cso
Loader Component
Package
texture.dds myApp .appx Texture
mesh.vbo
VertexBuffer
music.wma
Stream
Set up a multi-monitor dev PC with a touch monitor as the primary To work with the samples, set a breakpoint inside the DX:: macro in DirectSample.h line 18
Time to Act
Biggest opportunity. Ever.
Windows 8 Consumer Preview is now available.
Q&A