How to transform one of DirectWrite text objects while keep others no change

StackOverflow https://stackoverflow.com/questions/12523355

  •  03-07-2021
  •  | 
  •  

سؤال

I have several DirectWrite text objects on a same render target and I want to transform one of them at one time while keep others no change, In Direct2D, there are several type of transforms

  • Render target transform
  • Geometry transform
  • Brush transform

I can not use render target transform since it affects all the objects on it

I can not use geometry transform, since text is not a geometry, and there is no transform method for IDWriteTextLayout.

So the only choice for me is brush transform, but when I try to use the solid brush to draw a transformed rectangle, it still draw it in the original place(see code below), the sdk demo show an example of bitmap brush transform, so my question is: does transform works for other type of brush? like solid brush?

here is my code, first, calculate a translate matrix based on the time elapsed, then use this matrix to translate the brush and at last draw the rectangle. please take a look and tell me whether this is the case or if I can do it in some way else?

VOID CalculateTranslationMatrix(D2D1_MATRIX_3X2_F* matrix) 
{ 
    static float totalTime = 0.0f; 

    // Get start time 
    static DWORD startTime = timeGetTime(); 

    // Get current time 
    DWORD currentTime = timeGetTime(); 

    // Calculate time elapsed 
    float timeElapsed = (currentTime - startTime) * 0.001f; 

    // Accumulate total time elapsed 
    totalTime += timeElapsed; 

    // Build up the translation matrix 
    matrix->_11 = 1.0f; 
    matrix->_12 = 0.0f; 
    matrix->_21 = 0.0f; 
    matrix->_22 = 1.0f; 
    matrix->_31 = totalTime; 
    matrix->_32 = totalTime; 
} 

VOID DrawRectangle(HWND hwnd) 
{ 
    CreateD2DResource(hwnd) ; 

    g_pRenderTarget->BeginDraw() ; 

    // Clear background color to white 
    g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White)); 

    D2D1_MATRIX_3X2_F matrix; 
    CalculateTranslationMatrix(&matrix); 
    g_pBlackBrush->SetTransform(&matrix); 

    // Draw Rectangle 
    g_pRenderTarget->DrawRectangle( 
        D2D1::RectF(10.f, 10.f, 50.f, 50.f), 
        g_pBlackBrush 
        ); 

    HRESULT hr = g_pRenderTarget->EndDraw() ; 
    if (FAILED(hr)) 
    { 
        MessageBox(NULL, "Draw failed!", "Error", 0) ; 

        return ; 
    } 
} 
هل كانت مفيدة؟

المحلول

You could use render target transform, and just apply and restore states, eg:

  • Before draw, call g_pRenderTarget->GetTransform to get copy of the current transform
  • Set your desired object transform
  • Draw your object
  • use g_pRenderTarget->SetTransform using the value you saved before the draw.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top