Sign in to follow this  

Using Direct2D how would i fill a font with a gradient or radial brush?

This topic is 2667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I`ve managed to color my text red but how do i color the text in using a color gradient or radial brush?

Here is part of the code where I guess it has to go..

static const WCHAR sc_helloWorld[] = L"Direct2D Template";

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

m_pRenderTarget->BeginDraw();

// Sets background of window to black
m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::Black));

m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_pRedBrush
);

hr = m_pRenderTarget->EndDraw();

The 2 brushes I would like to use are the following :

ID2D1LinearGradientBrush *m_pLinearGradientBrush;
ID2D1RadialGradientBrush *m_pRadialGradientBrush;

I want to use these brushes to create a color gradient fill within the text itself not in a rectangle that all the examples show.

I`ve managed to create color gradient fills in rectangles using the following code..

// Translate for the linear gradient brush.

m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(D2D1::SizeF(200, 5)));



// Demonstrate a linear gradient brush.

m_pRenderTarget->FillRectangle(&rcBrushRect, m_pLinearGradientBrush);

But this obviously doesn`t achieve what I want which is to fill the text in with a gradient fill.

Share this post


Link to post
Share on other sites
I've never done such kind of thing with Direct2D, but I guess it goes like this:


m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_pLinearGradientBrush //<--
);

Share this post


Link to post
Share on other sites
Unfortunately i`ve already tried that as that was the obvious thing to do but nop thats not the answer as it doesn`t fill the font with a gradient.

Share this post


Link to post
Share on other sites
Actually you were kinda right.. the way to do it was to use a color first then use a gradient fill like this..

m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_pRedBrush
);

m_pRenderTarget->DrawText(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
m_pTextFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_pLinearGradientBrush
);

And that seems to work.. COOL!

Share this post


Link to post
Share on other sites
You shouldn't need to draw the text twice. I took the time and tested it for you in my own application. So, using this:


//DrawTextLayout() is nearly the same as DrawText(), but it's faster when drawing
//the same text multiple times. That what is important for you is the brush at the end of the function.
D2DObjects->RenderTarget->DrawTextLayout(TextOrig,Layout,D2DObjects->BrushCollection.LinearReflectBrushHigh);



I get this:

I hadn't got another gradient-brush handy, so I hope you can see that it's a gradient, and it works fine.


You really should find the issue on this, drawing text is not a very fast operation, and on top of that you don't use a layout. Can you show me the code you are using to create the gradient-brush?

Share this post


Link to post
Share on other sites

This topic is 2667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this