EDN Admin
Well-known member
I am working my way through http://msdn.microsoft.com/en-us/library/ff381399%28v=VS.85%29.aspx
Learn to Program for Windows in C++ and currently I am "playing" with http://msdn.microsoft.com/en-us/library/ff684181%28v=vs.85%29.aspx
Your First Direct2D Program . I would be gratefull if someone could clarify a thing or two for me.
1. My first question is about this bit of code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; if (pRenderTarget != NULL)
{
D2D1_SIZE_F size = pRenderTarget->GetSize();
<span style="color:Blue; const <span style="color:Blue; float x = size.width / 2;
<span style="color:Blue; const <span style="color:Blue; float y = size.height / 2;
<span style="color:Blue; const <span style="color:Blue; float radius = min(x, y);
ellipse = D2D1::Ellipse(D2D1:
oint2F(x, y), radius, radius);
}
[/code]
Why all floats are declared as constants? The code compiles and works OK when x, y and radius are floats. I am also curious why D2D1:
oint2F in D2D1_ELLIPSE structure is also declared as constant?
2. The second question is about how the circle is actually drawn in the window. Trying to check if I got all the information right I tried to write my own version of the program. Doing so I was running/debugging the program after addition of each step.
When I had everything except Resize() function I noticed some behaviour that Im having difficulty to understand. In the original program it can be reproduced by commenting out the
case WM_SIZE. The thing is that although Resize() and CalculateLayout() are not called and ellipse structure is not updated on window resize the drawn circle is different each time it is drawn.
CalculateLayout() where ellipse structure is created, is called when resources are created. The circle is drawn correctly. When the window is resized in such a way that client area gets smaller nothing gets drawn. However when the client area gets bigger
OnPaint() is called and the client area is repainted. Because resize() was not called, ellipse structure is exactly the same as when it was first created, however pained circle is not. It does not much ellipse structure, it resizes with a window and ellipse
is ignored. Why?
3. And finally question about SafeRelease.
Why do we need to use pointer to pointer with SafeRelease? To my beginners eye this code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; template <<span style="color:Blue; class T> <span style="color:Blue; void SafeRelease(T *ppT)
{
<span style="color:Blue; if (ppT)
{
(ppT)->Release();
ppT = NULL;
}
}
SafeRelease(pRenderTarget); <span style="color:Green; //Safe release call
[/code]
should work as good as this:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; template <<span style="color:Blue; class T> <span style="color:Blue; void SafeRelease(T **ppT)
{
<span style="color:Blue; if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
SafeRelease(&pRenderTarget); <span style="color:Green; //Safe release call
[/code]
However it doesnt. Could you please shed some light on the mechanism behind saferelease (or recommend some reading)?
View the full article
Learn to Program for Windows in C++ and currently I am "playing" with http://msdn.microsoft.com/en-us/library/ff684181%28v=vs.85%29.aspx
Your First Direct2D Program . I would be gratefull if someone could clarify a thing or two for me.
1. My first question is about this bit of code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; if (pRenderTarget != NULL)
{
D2D1_SIZE_F size = pRenderTarget->GetSize();
<span style="color:Blue; const <span style="color:Blue; float x = size.width / 2;
<span style="color:Blue; const <span style="color:Blue; float y = size.height / 2;
<span style="color:Blue; const <span style="color:Blue; float radius = min(x, y);
ellipse = D2D1::Ellipse(D2D1:

}
[/code]
Why all floats are declared as constants? The code compiles and works OK when x, y and radius are floats. I am also curious why D2D1:

2. The second question is about how the circle is actually drawn in the window. Trying to check if I got all the information right I tried to write my own version of the program. Doing so I was running/debugging the program after addition of each step.
When I had everything except Resize() function I noticed some behaviour that Im having difficulty to understand. In the original program it can be reproduced by commenting out the
case WM_SIZE. The thing is that although Resize() and CalculateLayout() are not called and ellipse structure is not updated on window resize the drawn circle is different each time it is drawn.
CalculateLayout() where ellipse structure is created, is called when resources are created. The circle is drawn correctly. When the window is resized in such a way that client area gets smaller nothing gets drawn. However when the client area gets bigger
OnPaint() is called and the client area is repainted. Because resize() was not called, ellipse structure is exactly the same as when it was first created, however pained circle is not. It does not much ellipse structure, it resizes with a window and ellipse
is ignored. Why?
3. And finally question about SafeRelease.
Why do we need to use pointer to pointer with SafeRelease? To my beginners eye this code:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; template <<span style="color:Blue; class T> <span style="color:Blue; void SafeRelease(T *ppT)
{
<span style="color:Blue; if (ppT)
{
(ppT)->Release();
ppT = NULL;
}
}
SafeRelease(pRenderTarget); <span style="color:Green; //Safe release call
[/code]
should work as good as this:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; template <<span style="color:Blue; class T> <span style="color:Blue; void SafeRelease(T **ppT)
{
<span style="color:Blue; if (*ppT)
{
(*ppT)->Release();
*ppT = NULL;
}
}
SafeRelease(&pRenderTarget); <span style="color:Green; //Safe release call
[/code]
However it doesnt. Could you please shed some light on the mechanism behind saferelease (or recommend some reading)?
View the full article