본문 바로가기
Direct2D

"Hello, World" by Direct2D

by leo21c 2014. 7. 9.
SMALL

 <참고>
 http://msdn.microsoft.com/en-us/library/windows/desktop/dd756692(v=vs.85).aspx

1. Include Direcr2D Header
    d2d1.h header를 incude 한다.

 #include "d2d1.h"

2. load library file
    d2d1.lib 파일을 load 한다.

 #pragma comment(lib, "d2d1.lib") 

3. Create an ID2D1Factory
   ID2D1Factory를 생성한다.

 ID2D1Factory* pD2DFactory = NULL;
 HRESULT hr = D2D1CreateFactory(
    D2D1_FACTORY_TYPE_SINGLE_THREADED,
    &pD2DFactory
    );

4. Create an ID2D1RenderTarget
   
ID2D1RenderTarget을 생성한다. ID2D1RenderTarget은 일반적으로 ID2D1HwndRenderTarget, ID2D1DCRenderTarget을 사용한다.
   
ID2D1HwndRenderTarget은 Hwnd를 보고 알수 있겠지만 Window handle을 이용해서 타켓을 지정하는 것이고, ID2D1DCRenderTarget는 DC를 보고 확인할 수 있듯이 Device Context를 이용해서 타겟을 지정한다. Target이란 그림을 그릴 곳을 지정하는 것이다.

 // Obtain the size of the drawing area.
 RECT rc;GetClientRect(hwnd, &rc);

 // Create a Direct2D render target  
 ID2D1HwndRenderTarget* pRT = NULL;
 HRESULT hr = pD2DFactory->CreateHwndRenderTarget(
    D2D1::RenderTargetProperties(),
    D2D1::HwndRenderTargetProperties(
        hwnd,
        D2D1::SizeU(
            rc.right - rc.left,
            rc.bottom - rc.top)
    ),
    &pRT
 );

 Rendering Target은 GPU를 이용해서 렌더링을 가속하고, 리소스를 화면에 표현한다. 또한 CPU를 이용해서 명령어를 처리하고 리소스를 생성하는 일을 한다. Direct2D가 GPU만을 사용하는 것이 아니라 화면에 처리하는 부분만 이용한다.

5. Create a Brush
   brush를 생성한다.

 ID2D1SolidColorBrush* pBlackBrush = NULL;
 if (SUCCEEDED(hr))
 {
    pRT->CreateSolidColorBrush(
        D2D1::ColorF(D2D1::ColorF::Black),
        &pBlackBrush
        ); 
 }

6. Create TextFormat
 Text Format을 생성한다.

 DWriteTextFormat* pTextFormat;

 // Create a text format using Gabriola with a font size of 72.
 // This sets the default font, weight, stretch, style, and locale.
 if (SUCCEEDED(hr))
 {
    hr = pDWriteFactory_->CreateTextFormat(
    L"Gabriola",   // Font family name.
    NULL,          // Font collection (NULL sets it to use the system font collection).
    DWRITE_FONT_WEIGHT_REGULAR,
    DWRITE_FONT_STYLE_NORMAL,
    DWRITE_FONT_STRETCH_NORMAL,
    72.0f,
    L"en-us",
    &pTextFormat
    );
 }

7. Draw Text
 Text를 화면에 그린다.

 static const WCHAR sc_helloWorld[] = L"Hello, World!";

 // Retrieve the size of the render target.
 D2D1_SIZE_F renderTargetSize = pRT->GetSize();

 pRT->BeginDraw();

 pRT->SetTransform(D2D1::Matrix3x2F::Identity());
 pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));

 pRT->DrawText(
      sc_helloWorld,
      ARRAYSIZE(sc_helloWorld) - 1,
      pTextFormat,
      D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
      pBlackBrush
    );

 hr = pRT->EndDraw();

7. Release Resources
 리소스를 반환한다.

 SafeRelease(pBlackBrush);
 SafeRelease(pTextFormat);
 SafeRelease(pRT); 
 SafeRelease(pD2DFactory);



LIST