65.9K
CodeProject 正在变化。 阅读更多。
Home

编程 Direct2D – 第 1 部分

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.71/5 (26投票s)

2010 年 7 月 19 日

CPOL

5分钟阅读

viewsIcon

135882

downloadIcon

1

Direct2D API 简介。

大多数原生 Windows 程序员都熟悉 GDI,这是 Windows 提供的图形 API。GDI 在 Windows 应用程序中被广泛使用。后来,微软通过 Windows 下的 GDI+ 引入了基于类的、更好的服务。原生程序员和托管开发人员都在使用 GDI/GDI+ 类和 API。

但 GDI 太老了,是在 Windows 的早期版本中引入的,随着时间的推移,图形硬件的功能不断增强,创建视觉丰富的 UI 的需求也在不断增长。在 Windows 7 中,微软引入了一个全新的 2D 图形子系统,称为 Direct2D ,它基于其著名的 Direct3D 平台,用于创建高质量的 2D 渲染。这有望在未来几年取代 GDI API。Direct2D 不仅在 Windows 7 上可用,而且微软通过最新的服务包升级将其在 Windows Vista 上也可用了。 无论您是否知道,使用 DirectX 平台, Windows Aero 系统自 Windows Vista 起就已经利用了图形硬件。 

Direct2D API 的主要优点是它具有硬件加速功能,并提供高质量的 2D 渲染。与传统的 API 相比,它使用户能够付出更少的努力来创建视觉丰富的应用程序。

GDI 使用像素图形,但 Direct2D 也支持矢量图形,其中使用数学公式来绘制线条和曲线。矢量图形提供独立于设备分辨率的高质量渲染,而像素图形则依赖于分辨率,这可能导致图形出现锯齿。

大多数 GDI API 不使用抗锯齿和透明度。当然,有一些函数可以做到这一点,但利用这些功能始终需要编程成本。此外,如果我们应用透明度和抗锯齿,计算将使用 CPU 进行。Direct2D 可以利用图形硬件,并将计算密集型任务委派给 GPU。 

Direct2D 构建在 Direct3D 组件之上。分层架构如下所示。

您可以看到底层是 Direct3D,它利用 DXGI(DirectX 图形基础结构),DXGI 管理与图形相关的低级任务,这些任务独立于 DirectX 图形运行时。DXGI 为图形组件提供了一个通用框架。同时,当硬件加速不可用时,会提供一个高性能的软件栅格化器。Direct2D API 的另一个优点是它使用了轻量级的 COM。它几乎就像一个简单的 C++ 类。没有 BSTRS、COM 变体、接口、公寓等。

让我们开始分析一个简单的 Direct2D 程序。

有 3 个主要组件对于在您的应用程序中启用 D2D 渲染是必不可少的。

下面的示例提供了在 MFC 应用程序中启用 Direct2D 渲染的步骤。很难描述所有 API 的参数。请在 MSDN 中查阅它们。

创建您的窗口

这里我以基于对话框的应用程序为例进行渲染。如果您使用其他类型的应用程序,如 SDI 或 MDI,函数和消息可能会有所不同。无需重复造轮子,让向导创建您的主对话框。

准备您的渲染引擎

让我们将所有渲染任务委派给另一个窗口,而不是在您的对话框类中管理所有内容。我将此类命名为 Direct2DHandler。

Direct2DHandler 的声明如下所示,它公开了以下函数。

#pragma once
#include <d2d1.h>
#include <d2d1helper.h>

class Direct2DHandler
{
public:
	Direct2DHandler( HWND hWnd ); // ctor
	~Direct2DHandler(void); // dtor
	HRESULT Initialize(); // Initialize the rendering
	HRESULT OnRender(); // Called from OnPaint function
	void OnResize(UINT width, UINT height);

private:
	HRESULT CreateDeviceResources(); // Create resources for drawing
	void DiscardDeviceResources(); // Release resources for drawing

private:

	HWND					m_hWnd;
	ID2D1Factory*			m_pDirect2dFactory;
	ID2D1HwndRenderTarget*	m_pRenderTarget;
	ID2D1SolidColorBrush*	m_pLightSlateGrayBrush;
	ID2D1LinearGradientBrush* m_pLinearGradientBrush;
};

构造函数

除了调用 CoInitialize() 函数和初始化成员变量(大部分为 NULL)之外,什么都不做。

Initialize 函数

创建 Direct2D 因子对象,这与 GDI 中的 HDC(上下文变量)类似。

HRESULT Direct2DHandler::Initialize()
{
    HRESULT hr = S_OK;

    // Create a Direct2D factory.
    hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);

    return hr;
}

OnResize

该函数处理渲染目标(render target)的渲染区域。在此示例中,我们将使用整个窗口区域。

void Direct2DHandler::OnResize(UINT width, UINT height)
{
    if (m_pRenderTarget)
    {
        // Note: This method can fail, but it's okay to ignore the
        // error here, because the error will be returned again
        // the next time EndDraw is called.
        m_pRenderTarget->Resize(D2D1::SizeU(width, height));
    }
}

OnRender

该函数执行以下操作:

  1. 创建绘制的设备资源(如果尚未创建)。
  2. 将场景渲染到窗口。
  3. 如果发生错误,则释放资源。

如果您有 3D 编程经验,您可能知道矩阵的作用以及它如何影响渲染对象。矩阵可用于表示对象的坐标系统。我们可以应用旋转、平移(移动)等(统称为变换)到矩阵上,然后可以使用它来指定对象的渲染方式。在这里,我们使用 D2D1::Matrix3x2F::Identity() 帮助函数,它创建一个单位矩阵(对象将在原点进行渲染,没有任何变换)。

m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());

我们可以用默认颜色清除渲染区域(对话框背景会根据 Windows 的主题填充一些默认颜色。这里我选择了白色)。

m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

重要的是,您为场景绘制的所有内容都必须在 BeginDraw() 和 EndDraw() API 之间完成。此代码的其余部分非常直接,您可以轻松理解。 请参见下面的源代码。

HRESULT Direct2DHandler::OnRender()
{
	HRESULT hr = S_OK;

	hr = CreateDeviceResources();

	if (SUCCEEDED(hr))
	{
		m_pRenderTarget->BeginDraw();
		m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
		m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
		D2D1_SIZE_F rtSize = m_pRenderTarget->GetSize();
		// Draw a grid background.
		int width = static_cast<int>(rtSize.width);
		int height = static_cast<int>(rtSize.height);

		// Draw two rectangles.
		D2D1_RECT_F rectangle1 = D2D1::RectF(
			rtSize.width/2 - 50.0f,
			rtSize.height/2 - 50.0f,
			rtSize.width/2 + 50.0f,
			rtSize.height/2 + 50.0f
			);

		D2D1_RECT_F rectangle2 = D2D1::RectF(
			rtSize.width/2 - 100.0f,
			rtSize.height/2 - 100.0f,
			rtSize.width/2 + 100.0f,
			rtSize.height/2 + 100.0f
			);

		// Draw the outline of a rectangle.
		m_pRenderTarget->FillRectangle(&rectangle2, m_pLinearGradientBrush);

		// Draw a filled rectangle.
		m_pRenderTarget->FillRectangle(&rectangle1, m_pLightSlateGrayBrush);

		hr = m_pRenderTarget->EndDraw();

	}
	if (hr == D2DERR_RECREATE_TARGET)
	{
		hr = S_OK;
		DiscardDeviceResources();
	}
	return hr;
}

CreateDeviceResources

是一个稍微冗长的函数,用于初始化渲染所需的资源,例如我们创建的画笔、笔刷、位图等。在这里,我主要创建一个浅色实心笔刷和线性渐变笔刷,以如上图所示进行渲染。注释已内联,请与代码一起阅读。了解渐变笔刷将很有趣。如果您在 Photoshop 中创建过渐变笔刷,事情会变得容易得多。我们可以设置停止点来控制渐变的结束,还可以使用线条来表示渐变的方向和角度。很简单,不是吗?就像我们创建一个普通的渐变一样。请参阅下面的代码。

HRESULT Direct2DHandler::CreateDeviceResources()
{
    HRESULT hr = S_OK;

    if (!m_pRenderTarget)
    {
        RECT rc;
        GetClientRect(m_hWnd, &rc);

        D2D1_SIZE_U size = D2D1::SizeU(
            rc.right - rc.left,
            rc.bottom - rc.top
            );

        // Create a Direct2D render target.
        hr = m_pDirect2dFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(m_hWnd, size),
            &m_pRenderTarget
            );

        if (SUCCEEDED(hr))
        {
            // Create a gray brush.
            hr = m_pRenderTarget->CreateSolidColorBrush(
                D2D1::ColorF(D2D1::ColorF::LightSlateGray),
                &m_pLightSlateGrayBrush
                );
        }

		// Create an array of gradient stops to put in the gradient stop
		// collection that will be used in the gradient brush.
		ID2D1GradientStopCollection *pGradientStops = NULL;

		D2D1_GRADIENT_STOP gradientStops[2];
		gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Maroon, 1);
		gradientStops[0].position = 0.0f;
		gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::Red, 1);
		gradientStops[1].position = 1.0f;
		// Create the ID2D1GradientStopCollection from a previously
		// declared array of D2D1_GRADIENT_STOP structs.
		hr = m_pRenderTarget->CreateGradientStopCollection(
			gradientStops,
			2,
			D2D1_GAMMA_2_2,
			D2D1_EXTEND_MODE_CLAMP,
			&pGradientStops
			);
		// The line that determines the direction of the gradient starts at
		// the upper-left corner of the square and ends at the lower-right corner.

		if (SUCCEEDED(hr))
		{
			hr = m_pRenderTarget->CreateLinearGradientBrush(
				D2D1::LinearGradientBrushProperties(
				D2D1::Point2F(0, 0),
				D2D1::Point2F(300, 300)),
				pGradientStops,
				&m_pLinearGradientBrush
				);
		}

    }

    return hr;
}

DiscardDeviceResources

只是释放分配的资源。它直接使用下面代码中描述的 Helper 宏 SafeRelease。

void Direct2DHandler::DiscardDeviceResources()
{
    SafeRelease(&m_pRenderTarget);
    SafeRelease(&m_pLightSlateGrayBrush);
    SafeRelease(&m_pLinearGradientBrush);
}

template<class Interface>
inline void SafeRelease(
    Interface **ppInterfaceToRelease
    )
{
    if (*ppInterfaceToRelease != NULL)
    {
        (*ppInterfaceToRelease)->Release();

        (*ppInterfaceToRelease) = NULL;
    }
}

在对话框类中初始化渲染

在 OnInitDialog 函数中,

为 Direct2DHandler 类分配内存并初始化它。您也可以从 OnCreate 函数调用此函数,但要确保存在有效的窗口句柄。

m_pRender = new Direct2DHandler( m_hWnd );
m_pRender->Initialize();

禁用默认背景绘制

禁用默认的 OnErasBkgrnd 函数,因为它可能会在绘制时导致闪烁。此外,我们占据了对话框的整个客户区,背景清除在 OnRender 函数本身中完成。

BOOL CDirect2DDemoDlg::OnEraseBkgnd(CDC* pDC)
{
    return FALSE;//CDialogEx::OnEraseBkgnd(pDC);
}

OnSize (WM_SIZE)

函数处理窗口大小事件。它会根据窗口大小调整渲染目标的大小。

void CDirect2DDemoDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
    if( m_pRender )
        m_pRender->OnResize( cx, cy );
}

OnPaint 函数

调用 Direct2DHandler 的 OnRender 函数来绘制场景。

void CDirect2DDemoDlg::OnPaint()
{
    if (IsIconic())
    {
        // code for handling when window is minimized. Check original CPP file attached.
    }
    else
    {
        // CDialogEx::OnPaint(); no need to call this.
        if( m_pRender )
            m_pRender->OnRender();
    }
}

链接 (Linking)

应用程序必须通过项目设置链接到 d2d1.lib,或在源文件中使用 #pragma comment( lib, “d2d1.lib” )。

d2d1helper

d2d1helper.h 包含一些有用的函数来帮助绘图。有关更多详细信息,请参阅头文件。

好的,这就是为您的 Windows 应用程序创建 Direct2D 渲染的基础。请继续查看附带的源代码。

项目文件是 Visual Studio 2010 格式的,但基本上您只需要 Direct2DHandler 类。您也可以使用其他早期版本的 Visual Studio 以及 Windows 7/Vista SDK。

 

© . All rights reserved.