Direct2D - Unboxing

Warning! Some information on this page is older than 5 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

May 2012

Just as some bloggers record unboxing of products, I'd like to share my first impressions from playing around with Direct2D library. What is it? As you can guess from its name, it is a library from Microsoft for efficient, hardware-accelerated drawing of immediate-mode 2D graphics. It is successor of the old GDI, as well as GDI+. It is native library, object-oriented, based on COM. It works on Windows 7, as well as Windows Vista (after installing SP2 and Platform Update) and Windows 2008. The API seems modern, clean and very elegant, much like Direct3D 10/11. But of course it could be just a matter of taste. You can find its documentation, as well as headers and libraries in DirectX SDK. Docs say something about Windows 7 SDK, so probably it's there too.

What can it interop with? You can create a render target that will send the graphics to either a window (using HWND), a GDI canvas (HDC), DXGI surface (interop with Direct3D 10/11) or WIC Bitmap. Interop with GDI is also possible in the opposite way - you can obtain a HDC from D2D render target. Microsoft advices using WIC (Windows Imaging Component) for loading bitmap files. Another important library that cooperates with Direct2D is DirectWrite, which enables text rendering.

I didn't find any good tutorial about Direct2D, but I haven't look for it very hard either. I think the official documentation is good enough teaching all aspects of the library. After reviewing the documentation, I'm very impressed not only by the looks of the API, but also by the list of features is supports, like:

Things I don't like so far about the library:

My first test, drawn using following code:

using namespace D2D1;
const D2D1_SIZE_F rtSize = m_RenderTarget->GetSize();


const float gridStep = 24.f;
for(float x = gridStep; x < rtSize.width; x += gridStep)
    m_RenderTarget->DrawLine(Point2F(x, 0.f), Point2F(x, rtSize.height), m_Brush, 1.f);
for(float y = gridStep; y < rtSize.height; y += gridStep)
    m_RenderTarget->DrawLine(Point2F(0.f, y), Point2F(rtSize.width, y), m_Brush, 1.f);

m_RenderTarget->DrawRectangle(RectF(200.f, 400.f, 400.f, 550.f), m_Brush);
m_RenderTarget->DrawRectangle(RectF(240.f, 450.f, 280.f, 500.f), m_Brush);
m_RenderTarget->DrawRectangle(RectF(320.f, 450.f, 360.f, 550.f), m_Brush));
m_RenderTarget->DrawLine(Point2F(200.f, 400.f), Point2F(300.f, 300.f), m_Brush, 1.f);
m_RenderTarget->DrawLine(Point2F(300.f, 300.f), Point2F(400.f, 400.f), m_Brush, 1.f);


Comments | #rendering #direct2d Share


[Download] [Dropbox] [pub] [Mirror] [Privacy policy]
Copyright © 2004-2020