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:
- Device and Context: The foundation for all rendering operations.
- Resources: Bitmaps, brushes, geometries, and text formats.
- Transforms: Manipulating the rendering space.
- Effects: Applying visual filters and post-processing.
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)
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
D2D1_RECT_F: Defines a rectangle with floating-point coordinates.D2D1_POINT_2F: Defines a point with floating-point coordinates.D2D1_COLOR_F: Defines an RGBA color with floating-point values.
Getting Started with Direct2D
To begin using Direct2D, you typically need to:
- Create an
ID2D1Factoryobject. - Create a render target (e.g.,
ID2D1HwndRenderTarget). - Use the render target to create resources like brushes and geometries.
- 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();