Too much function call when painting using Direct2D, not efficient?
The following code is from a example in Microsoft SDK, compared to GDI,
much more extra calls when painting. Because the function may be called
very frequently, it is not efficient, isn't it? And hr =
CreateDeviceResources(hWnd); uses something implicit, I don't think it is
a good design, because it is not clear, I can find the render target here.
BTW, if I want to past some code, do I have to indent every line with
spaces?
LRESULT DemoApp::OnPaint(HWND hWnd)
{
HRESULT hr = S_OK;
PAINTSTRUCT ps;
if (BeginPaint(hWnd, &ps))
{
// Create render target if not yet created
hr = CreateDeviceResources(hWnd);
if (SUCCEEDED(hr) && !(m_pRT->CheckWindowState() &
D2D1_WINDOW_STATE_OCCLUDED))
{
m_pRT->BeginDraw();
m_pRT->SetTransform(D2D1::Matrix3x2F::Identity());
// Clear the background
m_pRT->Clear(D2D1::ColorF(D2D1::ColorF::White));
D2D1_SIZE_F rtSize = m_pRT->GetSize();
// Create a rectangle same size of current window
D2D1_RECT_F rectangle = D2D1::RectF(0.0f, 0.0f, rtSize.width,
rtSize.height);
// D2DBitmap may have been released due to device loss.
// If so, re-create it from the source bitmap
if (m_pConvertedSourceBitmap && !m_pD2DBitmap)
{
m_pRT->CreateBitmapFromWicBitmap(m_pConvertedSourceBitmap,
NULL, &m_pD2DBitmap);
}
// Draws an image and scales it to the current window size
if (m_pD2DBitmap)
{
m_pRT->DrawBitmap(m_pD2DBitmap, rectangle);
}
hr = m_pRT->EndDraw();
// In case of device loss, discard D2D render target and
D2DBitmap
// They will be re-create in the next rendering pass
if (hr == D2DERR_RECREATE_TARGET)
{
SafeRelease(m_pD2DBitmap);
SafeRelease(m_pRT);
// Force a re-render
hr = InvalidateRect(hWnd, NULL, TRUE)? S_OK : E_FAIL;
}
}
EndPaint(hWnd, &ps);
}
return SUCCEEDED(hr) ? 0 : 1;
}
No comments:
Post a Comment