Strange CBitmapRenderTarget::GetBitmap API - Direct2D MFC

413 views Asked by At

I am completely confused by MFC wrapper for directd2d intefaces. Take a look at the following for example:-

BOOL CreateCompatibleRenderTarget(
   CBitmapRenderTarget& bitmapTarget,
   CD2DSizeF sizeDesired = CD2DSizeF(0.,
   0.),
   CD2DSizeU sizePixelDesired = CD2DSizeU(0,
   0),
   D2D1_PIXEL_FORMAT* desiredFormat = NULL,
   D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS options = D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE
);

bitmapTarget When this method returns, contains the address of a pointer to a new bitmap render target. This parameter is passed uninitialized.

I am completely puzzled by what I should pass to the the function. As on contrary to documentation it receives the object and not the pointer to the uninitialized as in Directd2d IDL. And the object must be initialized.

Now one can tell that CBitmapRenderTarget is an object created with default contstructor. However this is not working with the GetBimap member of the CBitmapRenderTarget which also follows the same patter in documentation:-

BOOL GetBitmap(
   CD2DBitmap& bitmap
);

bitmap When this method returns, contains the valid bitmap for this render target. This bitmap can be used for drawing operations.

However the CD2DBitmap DOES NOT HAVE the default ctor, so I cannot create the object in a first place. The question is how do I correctly call to GetBitmap of CBitmapRenderTarget API. How do I create the uninitialized CD2DBitmap object ???

1

There are 1 answers

1
Thierry Campiche On

I encountered same issue. Looking at the CD2DBitmap implementation, there's no constructor without argument, and one with the only the parent CRenderTarget* ans an argument, but it's protected so not usable from outside. So apparently the only way is to use one of the 3 public constructors which are only crating Bitmap from existing resources (from handle, resource id or file path).

On my case, as a workaround because my intent is to replace this bitbap by a new one (GetBitmap), I created the Bitamp from a PNG file stored on my resources :

    CD2DBitmap bitmap(GetRenderTarget(), (UINT)IDB_LOGO_PETIT, _T("PNG"));
    m_pTraceRenderTarget->GetBitmap(bitmap);

But you can use any other CD2DBitmap constructor:

CD2DBitmap(CRenderTarget* pParentTarget, UINT uiResID, LPCTSTR lpszType = NULL, CD2DSizeU sizeDest = CD2DSizeU(0, 0), BOOL bAutoDestroy = TRUE);
CD2DBitmap(CRenderTarget* pParentTarget, LPCTSTR lpszPath, CD2DSizeU sizeDest = CD2DSizeU(0, 0), BOOL bAutoDestroy = TRUE);
CD2DBitmap(CRenderTarget* pParentTarget, HBITMAP hbmpSrc, CD2DSizeU sizeDest = CD2DSizeU(0, 0), BOOL bAutoDestroy = TRUE);