0 leveraging c++11 for better, faster, easier windows phone 8 game development david cravey clear...
TRANSCRIPT
1
Leveraging C++11 for Better, Faster, Easier Windows Phone
8 Game Development
David CraveyClear Measure
James RobertsonNational Oilwell Varco
2
Welcome to Houston TechFest
• Please turn off all electronic devices or set them to vibrate.• If you must take a phone call, please do so in the lobby so as not
to disturb others.• Thanks to our Diamond Sponsors:
Thank you for being a part of the 7th Annual Houston TechFest!
3
Information• Speaker presentation slides will be available at
www.houstontechfest.org within a week
• Don’t forget to complete the Bingo card to be eligible for door prizes
4
Agenda• Speaker Introductions• Why develop for Windows Phone?• Windows Phone Development Overview• Demo• C++ 11 Language Features in VS2012/VS2013 you
should care about• Demo• What next?
5
Speaker Introductions• David Cravey
– Senior Software Architect at Clear Measure– David Cravey is a Senior Architect for Clear Measure in Austin, TX. David
has a love for software design, and a passion for sharing this love with the community. As a C++ enthusiast David has been awarded a Microsoft Visual C++ MVP for 3 years, and this year received the VC++ MVP of the year award. He is the leader of several C++ User Groups including: the Houston C++ User Group, DFW C++ User Group, the San Antonio C++ User Group, and the new C++ Brown Bag (weekly online lunchtime presentation).
• James Robertson– Programmer/QA at National Oilwell Varco– James Robertson lives and works in the Houston area. He is a C++
programmer who has been working for National Oilwell Varco for the last 2 years. Affiliated with the Houston C++ User Group he frequently talks on technology topics that interest him.
6
Why develop for Windows Phone?• The same DirectX programming model as the
Desktop– Currently there are a few libraries that are not
allowed: Direct2D, DirectWrite, XInput, Windows Imaging Components. Note, at least Direct2D has been tested to work but is not (at least not yet) allowed in the store http://kennykerr.ca/2012/12/01/windows-phone-and-direct2d/
• Share a code base with easily with Windows Store and with a little effort the Windows Desktop.
• Can you think of any other reason?
7
Windows Phone 8• (Native) Development Options– DirectX 11 (directly)– DirectX TK (wrapper)
• Currently there is no XAML support for C++ Apps, but it is easy to integrate C++ and .NET in a single application.
8
Windows Phone Emulator• Requires a Hyper Visor enabled CPU• Requires the Hyper Visor to not be in use– Works with Windows Hyper-V– VM Ware Workstation provides an unsupported
feature that many have made work– Virtual Box is the odd man out, uses the Hyper Visor,
but does not want to share• So if you (like me) enjoy Virtual Box then
develop/test against your phone!
9
Unlocking Your Windows Phone• Windows Phones can be unlocked for
development for free.• Use the “Windows Phone Developer Registration”
tool. TIP: Use your start menu search feature.• If you are still having problems then “Unregister
Your Device” and use the tool to register again.• http://mobileosgeek.com/how-to-unlock-your-wi
ndows-phone-8-for-development-and-deploying-apps/
10
Unlocking Your Windows Phone
This section of the text is dynamic, so watch for
changes!
There is only one button, but it not only changes its text, but
also it’s behavior!
11
Windows Phone Unlock Demo
DEMO TIME
12
DirectX Graphics Diagnostics• Uses the “REFERENCE” driver (all software) to enable
debugging at the graphics level.• Press “Print Screen” on your keyboard to capture a
frame.• Captured frames can be inspected to determine how
each pixel came into being.• With Visual Studio 2013 this is included in the Store
Express version. • TIP: If you do not have Pro, and you would like to debug
a Desktop DirectX application, you can launch your Desktop Express application in the Store Express IDE.
13
DirectTK Targets• Free wrapper for DirectX, makes life better • Develop using VS 2010/2012/2013• Can Target:
Win2008Sp2/Win2008R2/VistaSp2/Win7/Win8.xDestkop/Win8.xStore/Win2012/Win2012R2
• http://directxtk.codeplex.com/wikipage?title=Porting%20from%20D3DX&referringTitle=Documentation
14
DirectTK Headers• SpriteBatch.h - simple & efficient 2D sprite rendering• SpriteFont.h - bitmap based text rendering• Effects.h - set of built-in shaders for common rendering tasks• PrimitiveBatch.h - simple and efficient way to draw user primitives• GeometricPrimitive.h - draws basic shapes such as cubes and spheres• Model.h - draws simple meshes loaded from .CMO or .SDKMESH files• CommonStates.h - factory providing commonly used D3D state
objects• VertexTypes.h - structures for commonly used vertex data formats• DDSTextureLoader.h - light-weight DDS file texture loader• WICTextureLoader.h - WIC-based image file texture loader• ScreenGrab.h - light-weight screen shot saver• SimpleMath.h - simplified C++ wrapper for DirectXMath
15
Windows Phone Development Demo
DEMO TIME
16
Highlighted C++11 Features in VS2012/2013• Library Features
– Hash Tables• unordered_set, unordered_map
– RAII Wrappers• unique_ptr, shared_ptr, weak_ptr
– Atomic Variables– Threading (but, consider PPL and task oriented programming)
• Language Features– R-Value References
• && (move semantics & perfect forwarding)
– Local type deduction• auto, auto&, const auto&
– Range For– Lambdas
• Inline functors
– Variadic Templates, Initializer Lists, & Uniform Initialization• (new with VS2013)
17
R-Value References• In C++98/03 a reference could only bind to an l-
value* (there are some special rules for temporary values)– int& x = 1; // Error, 1 is an l-value (has no name)– int& x = y; //
• In C++13 an r-value reference can bind to an r-value or an l-value** (this topic can take atleast one entire talk)
• This powers 2 important technologies– Move Semantics (gut stealing)– Perfect Forwarding (simplifies wrappers/libraries/etc)
18
Local Type Deduction• Simply looks up the type of a variable• Many forms, all involve the keyword auto
– auto x = 1;• int x = 1;
– const auto• const int x = 1;
– auto &• int& x = y; // 1 would be illegal here as 1 is an r-value
– const auto&• const int& x = y;
– auto&&• Binds to either an l-value reference, or an r-value reference.• Scott Meyers calls this a universal reference, see his GN2013 talk.
• Uses template type deduction rules
19
How many ways to loop in C++98/03?• Given
– int myData[MaxMyData];• For loop using an index variable
– for (int x = 0; x < MaxMyData; ++x) { /*...*/ }• For loop using an iterator
– int*begin =&myData[MaxMyData];– int*end =&myData[MaxMyData];– for (int* it = begin; *it != end; ++it) { /*…*/ }
• Using for each– std::for_each(myData, &myData[MaxMyData], myFunction){}
• Plus– While Loops, Recursion, “May be considered harmful” goto, Template
Meta Programming, Function Pointers, and many more
20
C++11 Range For• Simplified access, that actually tell the compiler
more about what you are doing• Typical example when you don’t want to change
the element (i.e. only observe):int myData[MaxMyData];for (const auto& i : myData) { /*…*/ }
• If you want to change the element (i.e. mutate it):for (auto& i : myData) { /*…*/ }
• If you want a copy you can change:for (auto i : myData) { /*…*/ }
21
C++11 Lambdas• C++11 Lambdas provide a convenient syntax to
define anonymous functors inline.– Functors are function objects (i.e. overload the
function object)– Lambdas can “capture variables”
(note, this does not extend their lifetimes)– Lambdas that don’t capture variables can be used with
old fashioned standard C/C++ function pointers.– Lambdas love to inline, thus they can run very fast!– Lambdas make the STL algorithms much more
palatable
22
C++11 Lambda Expression Syntax
[captureList](parameterList)->returnType{lambdaBody}
• captureList– Optional (can be empty)– [=] captures by value (const unless lambda defined as mutable)– [&] captures by reference– [x,&y] captures x by value and y by reference– [x,y, &] captures x and y by value, and all others by reference
http://msdn.microsoft.com/en-us/library/dd293603(v=vs.120).aspx
23
Capture by reference, and returning a value Lambda Transformed
Lambda Expressionint x = 123;
auto lambda = [&x](long y){ return x + y };
// Same asauto lambda = [&x](long y)->long { return x + y };
Equivalent Functorclass unspeakableName{private: int& x;
public: unspeakableName(int& x_) :x(x_) { }
long operator()(long y){return x + y;}
} lambda;
24
Variadic Templates• Variadic Templates can take any number of
parameters, when combined with Perfect Forwarding they allow library designers to make better interfaces (faster, more consise, etc):
• Examples:– auto myPair = std::make_pair<int, double>(1,2.0);– auto myData = std::make_unique<int>(1.0);– std::vector myWidgets(Widget);– myVector.emplace_back(“Houston Techfest”, 2013);
25
Initializer Lists & Uniform Initialization• Initializer lists pack list of a single data type to be
passed as a parameter (typically a constructor/factory)– std::vector<int> myInts = {1,2,3,4,5,6};
• Uniform initialization is a combination of features to make variable initialization “uniform”.– int x { 1 };– Widget w { “Houston Techfest”, “Rocks”, 2013 };
• Note, Uniform initialization will default to using the constructor arguments, but if overrode they can make things better:– std::vector<int> myInts { 2, 4 };
26
Windows Phone Development Demo
DEMO TIME
27
Pluralsight Learning Resources(30 day trial in the bag you received this morning)
– C Programming Language Fundamentals– C++ Fundamentals (Parts 1 & 2)– C++ Language Changes in the VS 2013 Preview– Direct2D Fundamentals (Parts 1 & 2)– Modern C++ Libraries– The Essentials of COM (Parts 1 & 2)– C++ Advanced Topics
28
Recommended Books– Beginner• Programming 2D Games• Introduction to 3D Game Programming with DirectX11
– Intermediate/Advanced• Practical Rendering and Computation with Direct3D 11• ShaderX Books (9 of them)
– http://tog.acm.org/resources/shaderx/
• GPU Pro series (0 of them, was ShaderX)
29
Samples/Templates to Get You Going• Built Into Visual Studio– Windows Phone Direct3D App (Native Only)
• For download (within Visual Studio)– DirectXTK Simple Windows Phone Sample
• Windows SDK Samples• Older DirectX SDK Samples• NVidia SDK Samples• AMD Radeon SDK Samples
30
Windows Phone 8• Additional Learning Resources– http://www.DirectXTutorial.com• Excellent tutorial!!! ($50 one-time payment)
– http://rastertek.com/tutindex.html• I have heard good things, and its FREE!
– Houston C++ User Group• While not focused on Game Development, we cover a large
range of C++ topics (from Phone to Cloud )• Meets each month on the 4th Thursday of the month
(except November and December, and for Tech Fest )
31
Visual C++ Libraries Survey• Microsoft would like your feedback about what
libraries you use! (“to help guide their investments in Visual Studio tooling”)
https://illumeweb.smdisp.net/collector/Survey.ashx?Name=NuGetLibs
32
Thanks to all our Sponsors!
33
Please Leave Feedback During Q&AIf you leave session feedback and provide contact information, you will be qualified for a prize
Scan the QR code to the right or go to bit.ly/htf130206