Я обычно использовал цель рендеринга постоянного тока, но увидел, что у меня появляется небольшое мерцание, поэтому я добавил цель рендеринга растрового изображения, чтобы запускать операции рисования, а затем рисовать на постоянном токе, действующем как двойной буфер
мое высокое использование памяти (почти 60 МБ) связано с
pDCRenderTarget->CreateCompatibleRenderTarget(&pBitmapRenderTarget);
при изменении размера окна происходит огромное потребление памяти.
( обратите внимание, что впервые публикую проблему, так как почти всегда нахожу вопрос уже заданным)
Мой main.cpp:
#include
#include
#pragma comment(lib, "d2d1")
// Global variables
ID2D1Factory* pFactory = nullptr;
ID2D1DCRenderTarget* pDCRenderTarget = nullptr;
ID2D1BitmapRenderTarget* pBitmapRenderTarget = nullptr;
ID2D1Factory* GetFactory() {
if (pFactory == nullptr) {
// Create the Direct2D factory
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);
}
return pFactory;
}
// Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
// Bind the DC render target to the HDC
RECT rc;
GetClientRect(hwnd, &rc);
HDC hdc = GetDC(hwnd);
pDCRenderTarget->BindDC(hdc, &rc);
// Create a compatible bitmap render target
pDCRenderTarget->CreateCompatibleRenderTarget(&pBitmapRenderTarget);
// Begin drawing on the bitmap render target
ID2D1Bitmap* pBitmap = nullptr;
pBitmapRenderTarget->BeginDraw();
// Clear the background
pBitmapRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
// Draw a rectangle
ID2D1SolidColorBrush* pBrush = nullptr;
pBitmapRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Blue), &pBrush);
pBitmapRenderTarget->FillRectangle(D2D1::RectF(50, 50, 200, 200), pBrush);
// End drawing
pBitmapRenderTarget->EndDraw();
// Get the bitmap from the render target
pBitmapRenderTarget->GetBitmap(&pBitmap);
// Draw the bitmap to the DC render target
pDCRenderTarget->BeginDraw();
pDCRenderTarget->DrawBitmap(pBitmap, D2D1::RectF(0, 0, rc.right, rc.bottom)); // Destination rectangle
pDCRenderTarget->EndDraw();
// Release resources
pBrush->Release();
pBitmap->Release();
pBitmapRenderTarget->Release();
pBitmapRenderTarget = nullptr;
break;
}
case WM_DESTROY:
PostQuitMessage(0);
pDCRenderTarget->Release();
pDCRenderTarget = nullptr;
if (pFactory) pFactory->Release();
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
// Main Entry Point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Register the window class
const wchar_t CLASS_NAME[] = L"Direct2DDoubleBufferingWindow";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window
HWND hwnd = CreateWindowEx(
0, // Optional window styles
CLASS_NAME, // Window class
L"Direct2D Double Buffering", // Window title
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
nullptr, nullptr, hInstance, nullptr);
if (!hwnd)
return -1;
// Create a DC render target
D2D1_RENDER_TARGET_PROPERTIES rtProps = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE));
GetFactory()->CreateDCRenderTarget(&rtProps, &pDCRenderTarget);
ShowWindow(hwnd, nCmdShow);
// Run the message loop
MSG msg = {};
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... uch-memory