У меня есть окно DirectX, которое отображается с помощью Direct2D. Моя проблема, когда размер окна изменен, нарисованный 2D-контент масштабируется по размеру окна, поскольку размер renderTarget изменяется. Я не хочу использовать ДЕТСКОЕ ОКНО, которое равно размеру монитора. Я должен рисовать в том же окне. Я нашел HwndRenderTarget, многие из которых помогают рендерить 2D независимо от изменения размера окна. Я не уверен, что это используется для этого.
Моя цель 2D-рендеринга:
D2D1_FACTORY_OPTIONS options2d;
options2d.debugLevel = D2D1_DEBUG_LEVEL_NONE;
result = D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_MULTI_THREADED, options2d, &m_factory2d);
m_screenSize.x = screenWidth; m_screenSize.y = screenHeight;
if (FAILED(result))
{
return false;
}
// set up the D2D render target using the back buffer
m_swapChain->GetBuffer(0, IID_PPV_ARGS(&m_dxgiBackbuffer));
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED));
m_factory2d->CreateDxgiSurfaceRenderTarget(m_dxgiBackbuffer, props, &m_d2dRenderTarget);
Цель рендеринга HWND:
RECT rc;
GetClientRect(hwnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
D2D1_RENDER_TARGET_PROPERTIES RTprops = D2D1::RenderTargetProperties();
D2D1_HWND_RENDER_TARGET_PROPERTIES RT_hWndProps = D2D1::HwndRenderTargetProperties(hwnd, size,D2D1_PRESENT_OPTIONS::D2D1_PRESENT_OPTIONS_IMMEDIATELY);
result = m_factory2d->CreateHwndRenderTarget(RTprops, RT_hWndProps, &m_d2dHwndRenderTarget);
и:
void D2DClass::TryRenderHWNDText() {
static const WCHAR sc_helloWorld[] = L"Hello, World!";
// Retrieve the size of the render target.
D2D1_SIZE_F renderTargetSize = m_d2dHwndRenderTarget->GetSize();
m_d2dHwndRenderTarget->BeginDraw();
m_d2dHwndRenderTarget->Clear(ColorF(ColorF::Red));
m_d2dHwndRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
m_whiteBrush->SetColor(ColorF(ColorF::Orange));
if(textFormat){
m_d2dHwndRenderTarget->DrawTextW(
sc_helloWorld,
ARRAYSIZE(sc_helloWorld) - 1,
textFormat,
D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height),
m_whiteBrush
);
}
m_d2dHwndRenderTarget->EndDraw();
}
в цикле рендеринга:
bool GraphicsClass::Render()
{
m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);
/*RENDER 2D renderTarget NOT hwndTarget and 3D target*/
m_Direct3D->EndScene();
m_Direct2D->TryRenderHWNDText();
}
НачалоСцена:
void D3DClass::BeginScene(float red, float green, float blue, float alpha)
{
float color[4];
// Setup the color to clear the buffer to.
color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;
// Clear the back buffer.
m_deviceContext->ClearRenderTargetView(m_renderTargetView, color);
// Clear the depth buffer.
m_deviceContext->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
return;
}
Конечная сцена:
void D3DClass::EndScene()
{
// Present the back buffer to the screen since rendering is complete.
if (m_vsync_enabled)
{
// Lock to screen refresh rate.
m_swapChain->Present(1, 0);
}
else
{
// Present as fast as possible.
m_swapChain->Present(0, 0);
}
return;
}
Я вижу 2D и 3D, но не HWNDrenderTarget. Что мне здесь не хватает? Есть ли решение для моей цели?
Спасибо за любой совет