Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to Create Your First

Similar presentations


Presentation on theme: "How to Create Your First"— Presentation transcript:

1 How to Create Your First
Windows 8 Metro-style Game Chas. Boyd Principal Program Manager Microsoft Corporation

2 How to Create Your First Windows 8 Metro-style Game
Windows 8 metro-style apps are a huge new opportunity for all developers. Learn about the core components of the Windows8 platform specifically provided for metro-style games and how you can assemble them into your ideal game experience. Learn the latest tips and best practices for game graphics, input & animation, sound effects, and asset loading.

3 Three Presentations Today
Step by Step through Game Development How to set up the game How to code it <- You are here How to optimize it

4 Agenda Quick summary of the platform
Step through the process of development from a programmer’s perspective: App Initialization Opening a Window Rendering Graphics Loading and Saving Adding Input Controls Adding Sound Effects Adding an Art Pipeline This talk’s sample code is in C++

5 Platform Overview

6 Windows Kernel Services
Metro style apps Desktop apps C C++ Win32 C# VB .NET SL HTML JavaScript Internet Explorer View C C++ DX XAML C# VB JavaScript HTML / CSS Model Controller Communication & Data Devices & Printing WinRT APIs Graphics & Media System Services Application Model Kernel Windows Kernel Services

7 Windows 8 Game Platform Technologies
Your Killer Game Graphics Movies & Cut Scenes Game Input Audio Local Services Connected Services Tools Direct3D DirectX Video Pointer Point WASAPI PLM Windows Live Visual Studio Direct2D Media Foundation Sensor API XAudio2 AppData Windows Store Asset Viewers HTML XInput Contracts Xbox LIVE Asset Processors XAML

8 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

9 Visual Studio 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

10 C++/Cx Win32 WRL WinRT CoCreateInstance ComPtr<IObject>
Foo^ foo = ref new Foo() QueryInterface foo.As(&bar) N/A AddRef / Release std::vector Platform::Array<>^

11 Chapter 1/6 App Initialization

12 App Model – Activation ref class MyApp : public IFrameworkView {
        MyApp();     // IFrameworkView Methods     virtual void Initialize(CoreApplicationView^ applicationView);     virtual void SetWindow(CoreWindow^ window);     virtual void Load(String^ entryPoint);     virtual void Run();     virtual void Uninitialize();

13 Activation – Run void MyApp::Run() {
auto dispatcher = CoreWindow::GetForCurrentThread()->Dispatcher; while (!m_windowClosed) dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); m_renderer->Update(); m_renderer->Render(); m_renderer->Present(); }

14 App Activation Timeline
Splash Screen Loading Screen Menu Screen Gameplay OS Loads your App App Loads Loading Screen App is running Load() Run()

15 Handling Window Events
Chapter 2/6 Handling Window Events

16 ViewStates Widescreen (1366 x 768+) Portrait Snap view (required)
Minimum (1024 x 768) Portrait Snap view (required)

17 ViewStates 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); }

18 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(); }

19 Window Activation – Focus Changed
void MyApp::OnWindowActivationChanged( CoreWindow^ sender, WindowActivatedEventArgs^ args) { auto state = args->WindowActivationState; if(state == CoreWindowActivationState::Deactivated) OutputDebugString("Focus Lost"); if(state == CoreWindowActivationState::CodeActivated || state == CoreWindowActivationState::PointerActivated) OutputDebugString("Focus Regained"); }

20 Rotation Preferences {
DisplayOrientations::None; // Enable rotation by OS/Accelerometer DisplayOrientations::Landscape; // Lock rotation by OS/Accelerometer DisplayOrientations::LandscapeFlipped; // and enable this orientation DisplayOrientations::Portrait; DisplayOrientations::PortraitFlipped; } using namespace Windows::Graphics::Display; DisplayProperties::AutoRotationPreferences = DisplayOrientations::Landscape | DisplayOrientations::LandscapeFlipped;

21 Chapter 3/6 Rendering Graphics

22 3D Graphics via Direct3D 11 3D Graphics uses the same API as on Desktop/Classic WinRT API set includes only the latest DirectX 11 syntax DirectX 11 API supports multiple hardware generations via FeatureLevels: Feature_Level_9, Feature_Level_10, Feature_Level_11 Direct3D 11 updated for Windows 8

23 Creating a swap chain Windows::UI::Core::CoreWindow^ coreWindow; // app’s core window Microsoft::WRL::ComPtr<ID3D11Device1> d3dDevice; // renderer Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgiSwapChain; // front/back buffers of RT // Obtain the final swap chain for this window from the DXGI factory. dxgiFactory->CreateSwapChainForImmersiveWindow( d3dDevice.Get(), // the Direct3D device that will render to it DX::GetIUnknown(coreWindow), // IUnknown interface on our core window &swapChainDesc, // double or triple buffered, stereo, etc. nullptr, // allow on all displays &dxgiSwapChain // the resulting swap chain object );

24 void myApp::Render() { m_d3dContext->OMSetRenderTargets( // rebind every frame! 1, m_renderTargetView.GetAddressOf(), m_depthStencilView.Get() ); if (!m_loadingComplete) // only draw the cube once it's loaded return; // (this is async) m_d3dContext->IASetVertexBuffers( 0, 1, m_vertexBuffer.GetAddressOf(), &stride, &offset ); m_d3dContext->IASetIndexBuffer( m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0 );

25 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(), m_d3dContext->DrawIndexed( m_indexCount, 0, 0 ); }

26 Chapter 4/7 Loading and Saving

27 Process Lifetime Management
Suspend/Resume App gets 5 seconds to work after suspend message Apps are not notified before they are terminated Running App suspending Suspended App Terminated App User Launches App Low Memory resuming Apps are notified when they have been resumed

28 Concurrency Tasks using namespace Concurrency; float f = 1.0f;
thread float f = 1.0f; task<int>([=]() { return foo(f); }).then([](int x) bar(x); }).then(baz); // task defined with capturing lambda // continuation lambda argument is return value of previous // continuation using existing function baz()

29 State Persistence – Saving
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();     }); }

30 State Persistence – Loading
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;     }); }

31 File Input / Output 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;

32 Chapter 5/7 Adding Input

33 Mouse and Touch Input win->PointerPressed += ref new TypedEventHandler<CoreWindow^,PointerEventArgs^> (this, &LonLatController::OnPointerPressed); void LonLatController::OnPointerPressed( _In_ CoreWindow^ sender, _In_ PointerEventArgs^ args ) { float2 position = float2( // position of contact args->CurrentPoint->Position.X, args->CurrentPoint->Position.Y ); m_lonLatLastPoint = position; // save for use in controller m_lonLatPointerID = args->CurrentPoint->PointerId; }

34 Polling for Keyboard Input
// Arrow keys or WASD example auto upKeyState = window->GetKeyAsyncState(VirtualKey::Up); auto wKeyState = window->GetAsyncKeyState(VirtualKey::W); if (upKeyState & CoreVirtualKeyStates::Down || wKeyState & CoreVirtualKeyStates::Down) { m_playerPosition.y += 1.0f; }

35 Sensor fusion inputs and outputs
3D Accelerometer Pass-Through Accelerometer 3D Gyro Gyro 3D Magnetometer Sensor Fusion Compass Inclinometer Device Orientation

36 Reading orientation sensor data
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);

37 Game Controller Input Windows 8 supports Xbox360 -compatible controllers Check out new ControllerSketch sample Demonstrates game controller usage from JavaScript app

38 Game Controller Button Input
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();

39 Game Controller D-pad Input
SHORT thumbLeftX = inputState.Gamepad.sThumbLX; if (abs(thumbLeftX) < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) thumbLeftX = 0; SHORT thumbLeftY = inputState.Gamepad.sThumbLY; if (abs(thumbLeftY) < XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) thumbLeftY = 0; combinedTiltX += (float)thumbLeftX / f; combinedTiltY += (float)thumbLeftY / f;

40 Chapter 6/7 Adding Sound Effects

41 Sound Effects // Create the XAudio2 engine and mastering voice on the default audio device XAudio2Create(&m_audioEngine); m_audioEngine->CreateMasteringVoice(&m_masteringVoice); // Load all audio data for the sound effect into a single in-memory buffer MediaLoader soundFile(ref new Platform::String(SOUND_FILE)); m_soundEffectBuffer = soundFile.ReadAll(); // Create a single source voice for a sound effect m_audioEngine->CreateSourceVoice(&m_sourceVoice, &(soundFile.GetOutputWaveFormatEx())); // Trigger sound effect: queue in-memory buffer for playback and start the voice XAUDIO2_BUFFER buf = {0}; buf.AudioBytes = m_soundEffectBuffer.size(); buf.pAudioData = &m_soundEffectBuffer[0]; buf.Flags = XAUDIO2_END_OF_STREAM; m_sourceVoice->SubmitSourceBuffer(&buf); m_sourceVoice->Start();

42 Creating an Asset Pipeline
Chapter 7/7 Creating an Asset Pipeline

43 Package Author-time Asset Build Step Packager Run-time Asset
shader.hlsl fxc shader.cso Package texture.bmp dxtex texture.dds myApp .appx mesh.obj obj2vbo mesh.vbo music.wav myTool music.wma

44 Package Bindable Run-time Loader Component API object Asset Installer
App Bindable API object Run-time Asset Loader Component Installer shader.cso D3D Shader Package texture.dds myApp .appx Texture Loader Texture mesh.vbo Mesh Loader VertexBuffer music.wma myTool Stream

45 Pro Dev Hints Find commands in the IDE via the search textport
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::ThrowIfFailed() macro in DirectSample.h line 18

46 Time to Act Biggest opportunity. Ever. Windows 8 Consumer Preview is now available. Check out the store. Go build great games.

47 Q & A

48 4/20/ :57 AM © 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. © 2010 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.


Download ppt "How to Create Your First"

Similar presentations


Ads by Google