Community

Direct2D API Reference

This section provides comprehensive documentation for the Direct2D API, a hardware-accelerated, immediate-mode 2D graphics API that provides high performance for 2D rendering and is designed for both Windows Store apps and traditional desktop applications.

Core Concepts

Direct2D focuses on efficiency and flexibility, allowing developers to create visually rich 2D graphics with ease. Key concepts include:

ID2D1Factory Interface

The ID2D1Factory interface is the starting point for using Direct2D. You use it to create Direct2D objects such as geometries, brushes, and render targets.

Methods

  • CreateGeometryGroup (ID2D1GeometryGroup)
  • CreateRectangleGeometry (ID2D1RectangleGeometry)
  • CreateEllipseGeometry (ID2D1EllipseGeometry)
  • CreateGeometryGroup (ID2D1GeometryGroup)
  • CreateRadialGradientBrush (D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES, D2D1_BRUSH_PROPERTIES, ID2D1RadialGradientBrush)
ID2D1HwndRenderTarget Interface

An ID2D1HwndRenderTarget is a render target that draws directly to a window (HWND). It handles window resizing and device loss.

Methods

  • BeginDraw (void)
  • Clear (D2D1_COLOR_F)
  • DrawLine (D2D1_POINT_2F, D2D1_POINT_2F, ID2D1Brush, float, ID2D1StrokeStyle)
  • FillRectangle (D2D1_RECT_F, ID2D1Brush)
  • EndDraw (D2D1_TAG*, D2D1_TAG*)

Common Structures

Getting Started with Direct2D

To begin using Direct2D, you typically need to:

  1. Create an ID2D1Factory object.
  2. Create a render target (e.g., ID2D1HwndRenderTarget).
  3. Use the render target to create resources like brushes and geometries.
  4. Begin drawing, issue drawing commands, and end drawing.

Example Snippet

#include <d2d1.h>

// ... later in your code

ID2D1Factory* pFactory;
D2DCreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);

// Create render target, brushes, geometries...

// Drawing loop
// pRenderTarget->BeginDraw();
// pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::CornflowerBlue));
// pRenderTarget->FillRectangle(rect, pBrush);
// pRenderTarget->EndDraw(nullptr, nullptr);

// Release resources when done
// pFactory->Release();
// pRenderTarget->Release();