Sridhar Poduri's Blog, page 3
June 25, 2013
A getting started template for creating XAML/DX apps using C++ and the SwapChainBackgroundPanel
This post comes courtesy of Wayne Ransier.
Start a XAML/C++ project in Visual Studio 2012 with the default Blank Template and replace the generated element with a
<SwapChainBackgroundPanel x:Name="DXSwapChainPanel">
</SwapChainBackgroundPanel>
If you build the solution now, the code compiles but the app crashes at runtime. The fix for the crash is simple.
Replace the OnLaunched method of your App class with the below code
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ args)
{
m_mainPage = ref new MainPage();
Window::Current->Content = m_mainPage;
Window::Current->Activate();
}
The code to replace the OnLaunched method of the App class is missing from the step by step application tutorial of the DrawIt application.
You can get the complete details about why the crash happens here.
A simple project template with this fix applied can be found here.
Copy this project template (the zip file) to “C:\Users\\Documents\Visual Studio 2012\Templates\ProjectTemplates” and re-start Visual Studio 2012.
A new template with the name SCBPCppProject will be listed when you select Visual C++ project type in Visual Studio 2012.
Another nifty tip to keep in mind and this is obtained from the DrawIt sample from “Modern C++ and Windows Store apps” book.
There are a few styles that I have created for the ApplicationBar and placed them in Styles.xaml. In the sample I do not use the default generated styles from StandardStyles.xaml.
You can see the change in app.xaml for the DrawIt project.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
Merging only the resources your application requires, into the Resource Dictionary is a nifty optimization.
A version of the sample that supports multi-touch can be downloaded here.
-Sridhar
June 19, 2013
Tip of the day: Using Constants in WinRT
This tip comes courtesy of a question asked on an internal discussion list. Deon from the C++ team provided the answer.
WinRT does not support constants. This means you cannot have code such as the following in C++ /CX:
public ref class FooClass sealed
{
public:
const int FooValue = 42;
}
How does one mimic constants with C++ /CX? Turns out using a static property is the solution.
namespace Foo
{
public
ref
class
FooClass
sealed
{
public:
static
property
int Value
{
int get()
{
return x;
}
void set(int
_value)
{
x = _value;
}
}
private:
static
int x;
};
}
Having static properties allows you to write code as follows:
Foo::FooClass::Value = 42;
int i = Foo::FooClass::Value;
Better, this code now works if you want to access such properties from other WinRT supported languages such as JS, C# etc.
Enjoy!
-Sridhar
June 18, 2013
Power savings and performance benefits of writing code using C++
I get a few questions my way on an almost daily basis about how writing code using C++ results in savings in power consumption and other performance benefits. In this regard, I defer to the experts (Bjarne, Sutter etc) who explain these concepts with much more clarity and authority. So here you go:
http://www.noodle.org/learn/details/115493/bjarne-stroustrup-how-c-combats-global-warming
http://channel9.msdn.com/posts/C-and-Beyond-2011-Herb-Sutter-Why-C
Enjoy!
-Sridhar
June 17, 2013
Flipkart in India begins taking order for Modern C++ and Windows Store apps
June 16, 2013
Multi-touch drawing in Windows Store apps using C++ and DirectX
Windows 8 introduces input via multiple touch points. In the DrawIt sample of my book “Modern C++ and Windows Store apps”, I show how to add free form drawing on a XAML SwapChainBackgroundPanel using either touch, mouse or pen inputs. The sample restricted input to a single pointer i.e. you could draw using a finger or with left mouse button down or with a stylus if your device supported one. In today’s post, I will show how to move from a single pointer to supporting multiple pointers (restricted only by the number of touch points supported by the underlying hardware).
The DrawIt sample has two main classes: MainPage and ShapeRenderer. All of the changes I am about to make below are in the ShapeRenderer class.
The only change to MainPage is to add support for the PointerEntered event which in turn calls the corresponding method from ShapeRenderer.
I will also add an overlay to the code to show Pointer touch point properties like PointerId, the point where the touch occurs etc. These changes are also in ShapeRenderer
In the sample I enclose below, I have removed support for erasing. You are free to add it back as an exercise.
Handling Multiple Pointers
Open the ShapeRenderer header file. Add a private member variable of type Platform::Collections::Map.
Platform::Collections::Map<UINT, Windows::UI::Xaml::Input::Pointer^>^ m_PointerContact;
Add another private variable of type UINT.
UINT m_activeContacts;
These two variables will be used to keep track of the multiple pointers that can be used to draw on screen.
Add a function declaration for OnPointerEntered in the ShapeRenderer class. The ShapeRenderer class already has methods for OnPointerPressed, OnPointerReleased etc.
void OnPointerEntered(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ args);
In the ShapeRenderer constructor, initialize the map as follows:
m_PointerContact = ref new Map<UINT, Pointer^>();
In the implementation for OnPointerEntered method, we will use the following logic. If the pointer that fires the “Entered” event exists in our map, we do nothing. Else we store the new pointer details in the map, increment the number of active pointer count and begin drawing.
Next we call the Render method of the SwapChain.
Similarly in the OnPointerPressed event handler, we check if the pointer exists in our saved collection. If yes, do nothing. Else proceed to drawing.
In the OnPointerReleased event handler, call EndStroke of the InkManager and then Render the strokes.
After the Render call, remove the released pointer details from the collection and decrement the active pointer count.
That is all there is to handling multiple pointers.
Creating an overlay while drawing
Add three new methods to the ShapeRenderer header file.
void UpdateInfoPop(Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e);
void DestroyInfoPop(Windows::UI::Xaml::Input::Pointer^ ptr);
void CreateInfoPop(Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e);
The logic to create the popup is simple. In the CreateInfoPop method, we create a TextBlock. Get the current touch point details and add the pointer details to the TextBlock. Apply a RenderTransform on the TextBlock by creating a TranslateTransform and offset the TextBlock position by 20 pixels from the point where the pointer is on screen. This allows the TextBlock to be visible and display contents.
The UpdateInfoPop is used to move the TextBlock as the pointer moves on the screen. Check for all children of the SwapChain that matches the TextBlock control type and then use the name property to zero on the right pointer. Once the right pointer is identified, update the TextBlock position to the new position as determined by the pointer point.
That is all there is to adding information overlay to a pointer move event. The sample code is attached. Download the code and let me know your thoughts!
-Sridhar
June 15, 2013
The Windows Store opportunity for C++ developers
One of my colleagues sent this statement my way and I quote him “The advent of Windows Store combines a great app-development infrastructure with a reach of 250 million people and counting. This is a great opportunity for seasoned and new app developers alike. While Microsoft has published great examples, tutorials, and help, a more detailed and focused resource like the book, “Modern C++ and Windows Store apps” is a fantastic contribution to help developers profit from this opportunity.”
Many thanks Mike Wehinger
-Sridhar
Two copies remaining
With less than two weeks to go for //BUILD 2013, I still have two copies of my book “Modern C++ and Windows Store apps” waiting to be given away. Follow the rules listed here and send me a mail.
-Sridhar
June 12, 2013
It’s time to move away from Turbo C++
You might be surprised to hear that a good number of colleges (at least in India) still rely on using the ages old Turbo C++ package to teach aspiring students C++. With a new language standard and great library support, it is now time to bid goodbye to Turbo C++. I answered a few questions about Turbo C++ and Modern C++ in this month’s edition of PCQuest (Thank you Hiren Mehta and other good folks from PCQuest). Attaching the Q&A below.
While Visual Studio 2012 is a great tool for developing Windows apps using C++, there are also similar utilities for other platforms that support the C++11 standard. These include, but are not limited to, GCC, Clang etc. We owe it to aspiring students and should teach and train them on the latest language standard. In case you have not checked out Modern C++, I urge you all to check out the new language and you will be pleasantly surprised at the changes!!
-Sridhar
May 29, 2013
Getting started with C++
How does one get started with C++? Where should an aspiring developer start? What books should they read? These are some questions that do the rounds of many developers who want to dip their toes in the magical world of C++. This post is an attempt to address some of the questions while also trying to provide my own perspective. If you find the links and my suggestions useful, please do let me know. If you do not find them useful, kindly ignore.
C++11 a.k.a Modern C++ is a new language. It feels different from the old C++ (C++98) and places great emphasis on developer productivity enhancing facilities, focus on runtime performance, writing safe, efficient and portable code. Hence it is very, very important to learn the nuances of Modern C++ v/s trying to learn the old C++ and attempt to fit the old in the new.
Here are my recommended books.
C++ Primer by Stanley Lippman et.al. The latest edition has been completely re-written for the C++11 standard.
The C++ Programming Language 4th Ed by Bjarne Stroustrup. The 4th edition has been revised and updated for the C++11 standard.
The C++ Standard Library, 2nd Ed by Nicolai Josuttis. The 2nd edition has been revised and updated for the C++11 standard. This book focuses on the Standard Library that is part and parcel of the C++ programming language.
These books are by no means exhaustive but will allow you to start learning C++. One can begin with C++ Primer and then move to the other books to understand and learn about advanced topics.
Having said that, reading in no way substitutes for actual coding practice. Spend as much time as possible writing code. You can start with simple programs that are discussed in the books above and slowly work your way to adding functionality and features to those programs. Or you can also start writing your own programs. Either way, know your tools and write as much code as possible while applying the concepts you learn.
A lot of coding problems have probably been already solved. The technology world is a rich source of learning from one another, provided one learns to ask the right questions in the proper format. Hang out on sites such as stackoverflow.com, codeproject.com, MSDN forums etc and learn from fellow developers. If you know the answer to a problem, help your fellow colleagues from the industry. Participating in community activities helps you build confidence in your tools, your craftsmanship and ultimately your own abilities. Think of this as an investment for your own good and the benefit of all.
In addition here are a few links you should regularly visit to keep yourself updated with all major events and happenings related to C++.
http://www.isocpp.org
The Home Page of Standard C++.
http://www.stroustrup.com/ The Home Page of Bjarne Stroustrup, the designer of C++.
http://herbsutter.com Herb Sutter on software, hardware, and concurrency. He has also begun an updated series of Guru of the Week Questions based on C++11 and will be updating his famous Exceptional C++ series of books with C++11.
http://scottmeyers.blogspot.com/ Scott Meyers is one of the world’s foremost experts on C++ software development. He offers training and consulting services to clients worldwide.
Again, no means exhaustive but good starting points from where you can learn C++. Once you learn the basics of standard C++, you can then move to learning platform specific extensions like C++ /CX and develop cool Windows Store apps!
-Sridhar
May 28, 2013
A short interview on Why is C++ still the answer?
Preemptive snarky comment: What is the question? J
A few weeks back the awesome folks from the Microsoft Careers group and I had a short conversation about my book, how I ended up in the technology world and why I strongly believe that C++ is still the answer to some of the big challenges we face in the technology industry. They have published the interview in a Q&A format via multiple social networks. Pick the one you are associated with and enjoy!!
In the latest JobsBlog: Microsoft program manager, and recently published author, Sridhar Poduri describes his path to Microsoft and explains why C++ is the answer: http://bit.ly/15fGtwZ
- Feel free to tag the Microsoft Careers facebook page when sharing.
New on the @MicrosoftJobs Blog: Microsoft PM & author @sridharpoduri explains why C++ is the answer – http://bit.ly/15fGtwZ
In the latest JobsBlog: Microsoft program manager, and recently published author, Sridhar Poduri describes his path to Microsoft and explains why C++ is the answer: http://bit.ly/15fGtwZ
- Feel free to tag the Microsoft page on LinkedIn when sharing.