Windows with C++: Introducing Direct2D

I’ve been away from my computer for a few days working on a house renovation project and didn’t notice that my latest Windows with C++ column came out until the mail man delivered a copy of the June 2009 issue of MSDN Magazine.

In this issue I introduce a new technology that I’m very excited about. I’ve been waiting for Direct2D for some time. When Microsoft started pushing WPF as the “one true path” (TM) I just couldn’t persuade myself to buy the sales pitch. If you felt the same way then read on. This just might make your day.

Windows with C++: Introducing Direct2D

With the introduction of Windows Vista a few years ago, it was clear that the era of the Windows Graphics Device Interface (GDI) was coming to an end. GDI, even with the help of GDI+, had been showing its age especially when compared to the superior graphics capabilities of Windows Presentation Foundation (WPF). As if that weren't enough, GDI lost its hardware acceleration while WPF took advantage of Direct3D's abundance of hardware acceleration.

However, if you want to develop high-performance and high-quality commercial applications, you'll still look to C++ and native code to deliver that power. That's why few things excite me more about Windows 7 than the introduction of Direct2D and DirectWrite. Direct2D is a brand new 2D graphics API designed to support the most demanding and visually rich desktop applications with the best possible performance. DirectWrite is also a brand-new API that complements Direct2D and provides hardware-accelerated text, when used with Direct2D, and high-quality text layout and rendering with advanced support for OpenType typography and ClearType text rendering.

In this article, I will explore these new technologies and give you an idea of why it matters and how you can start using them today.

Big thanks to Mark Lawrence and Ben Constable, from the Direct2D team, for their feedback on the article as well as for providing a side bar discussing rendering performance.

Stay tuned for more Direct2D articles in my upcoming Windows with C++ columns.

If you’re looking for one of my previous articles here is a complete list of them for you to browse through.

Produce the highest quality screenshots with the least amount of effort! Use Window Clippings.

Published Tuesday, June 02, 2009 11:44 AM by KennyKerr
Filed under: ,

Comments

# re: Windows with C++: Introducing Direct2D

Tuesday, June 02, 2009 9:33 AM by Leo Davidson

Looks like a handy introduction. Thanks for sharing it.

I'm looking forward to the day when Direct2D is ubiquitous. It definitely seems like what we've been crying out for for a long time. Now we just have to wait (as always with new OS features) for it to become the baseline.

I'd like to add some D2D rendering paths in one or two things where it'll make a big difference to quality/performance but will still have to write/maintain GDI code for everyone stuck on earlier OS versions. The thing I'm really looking forward to is when we can just write Direct2D and forget about GDI. The new API looks so much better than ye olde GDI (wouldn't be hard :)) and not requiring a redistributable* is a big win.

(*I don't get why things like GDI+ and the MS CRT DLLs are not pushed via Windows Update. It'd mean we could use them without having to worry about shipping them and, more importantly keeping up to date. Unless I'm mistake and GDI+ is on WU.)

For anyone else new to Direct2D, I recommend this video from the recent PDC. It's fairly long but it does an excellent job of showing what Direct2D and DirectWrite can do, and it shows you some of the C++ code behind it as well. Should be a perfect companion to Kenny's article:

channel9.msdn.com/.../PC18

I guess I'll never get around to reading those two books on WPF that I bought a couple of years ago. I was never entirely sold on it either (and I'm just waiting for it to be made obsolete in a year or so -- like every single other Microsoft UI framework! -- so why bother learning it now? lol) but it seemed like a decent way to gain access to simple vector rendering. With Direct2D perhaps I've got all I need, without having to pull .Net into the mix. (I dig .Net and have used it a lot, but when the codebase and libraries I'm using are all C++ at the moment, using .Net to get at a nice 3D API never seemed like a brilliant move.)

Oops, I'm rambling again. :) Have a nice day!

Leo

# re: Windows with C++: Introducing Direct2D

Tuesday, June 02, 2009 10:18 AM by Ryan

Wow, thank you. I'm a Win32/C++ hobbiest and this is very exciting info!

# re: Windows with C++: Introducing Direct2D

Wednesday, June 03, 2009 12:48 PM by pingpong

Backport it to XP, pretty please.

# re: Windows with C++: Introducing Direct2D

Wednesday, July 08, 2009 2:34 PM by Anon

Nice article. You make no metion of the other DirectX 2-D API, the now-dead DirectDraw. Any tips for those of us stuck with maintaining/porting DirectDraw-based applications?

# re: Windows with C++: Introducing Direct2D

Friday, July 10, 2009 5:54 PM by SSchulz

Looks very promising for porting some old DirectDraw code which mainlying did bitblts. Like Anon asked, any tips in that area?  Also, will Direct2D be supported on Vista?

# re: Windows with C++: Introducing Direct2D

Saturday, July 11, 2009 12:45 AM by KennyKerr

SSchulz: Direct2D will be available on Windows Vista. I’m afraid I don’t have any experience with DirectDraw.

# re: Windows with C++: Introducing Direct2D

Friday, July 31, 2009 5:08 AM by Aidan

Hi. Forgive my ignorance but I am a

Mfc C++ developer and was making the

Move over to C#/.net with wpf, mainly

Because of the richness it allows and

The performance promise of ease of

Access to gpus. However it is proving

Slower than expected and we cannot fully

Understand why. There seems to be little

Written on high-perf graphics and how

Wpf fairs to the other options. So

I am keen to understand what is behind

Your articles gambit comment that native

Code and C++ are still needed for perf.

Are you saying that to truly get performance

We should be writing directly and natively

With directx? And if so are there other

Ways to get access to directx in a managed

Environment other than through wpf? And

Where is the performance cost from using

Wpf that you imply?

If there articles I could read I would appreciate

The redirect.

Thanks!

# re: Windows with C++: Introducing Direct2D

Friday, July 31, 2009 6:25 AM by KennyKerr

Aidan: Managed code promises higher productivity compared to C++ which for the most part is true. In theory it can also match the performance of native code but in practice this is rarely achieved in desktop applications.

The issues with WPF and Direct2D however are not about managed vs. native. It’s about immediate-mode graphics vs. retained-mode graphics. Immediate-mode will almost always be faster in practice regardless of whether it’s native or managed. WPF just happens to be a retained-mode graphics API and thus it will be slower by design.

So WPF is simpler but Direct2D is faster. If you want to get the performance of Direct2D/Direct3D with managed code you’ll have to use the managed Direct3D wrapper libraries. Of course that will be more complex than Direct2D even though you’re using managed code since Direct2D is much simpler than Direct3D.