@@ -96,6 +96,7 @@ static float GetPenThickness (const NUIE::Pen& pen)
9696
9797Direct2DContext::Direct2DContext () :
9898 NUIE::NativeDrawingContext (),
99+ hwnd (NULL ),
99100 width (0 ),
100101 height (0 ),
101102 renderTarget (nullptr )
@@ -110,20 +111,8 @@ Direct2DContext::~Direct2DContext ()
110111
111112void Direct2DContext::Init (void * nativeHandle)
112113{
113- HWND hwnd = (HWND ) nativeHandle;
114-
115- RECT clientRect;
116- GetClientRect (hwnd, &clientRect);
117- width = clientRect.right - clientRect.left ;
118- height = clientRect.bottom - clientRect.top ;
119-
120- D2D1_SIZE_U size = D2D1::SizeU (width, height);
121- D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties ();
122- D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties = D2D1::HwndRenderTargetProperties (hwnd, size);
123-
124- direct2DHandler.direct2DFactory ->CreateHwndRenderTarget (renderTargetProperties, hwndRenderTargetProperties, &renderTarget);
125- renderTarget->SetDpi (96 .0f , 96 .0f );
126- DBGASSERT (renderTarget != nullptr );
114+ hwnd = (HWND ) nativeHandle;
115+ CreateRenderTarget ();
127116}
128117
129118void Direct2DContext::BlitToWindow (void *)
@@ -171,9 +160,10 @@ void Direct2DContext::BeginDraw (Phase phase)
171160void Direct2DContext::EndDraw (Phase phase)
172161{
173162 if (phase == Phase::Draw) {
174- // TODO: recreate in case of error
175- DBGONLY (HRESULT success = ) renderTarget->EndDraw ();
176- DBGASSERT (SUCCEEDED (success));
163+ HRESULT result = renderTarget->EndDraw ();
164+ if (result == D2DERR_RECREATE_TARGET ) {
165+ CreateRenderTarget ();
166+ }
177167 } else if (phase == Phase::DrawConnections) {
178168 renderTarget->SetAntialiasMode (D2D1_ANTIALIAS_MODE_ALIASED );
179169 }
@@ -290,4 +280,21 @@ NUIE::Size Direct2DContext::MeasureText (const NUIE::Font& font, const std::wstr
290280 return NUIE::Size (metrics.width * SafetyTextRatio, metrics.height * SafetyTextRatio);
291281}
292282
283+ void Direct2DContext::CreateRenderTarget ()
284+ {
285+ RECT clientRect;
286+ GetClientRect (hwnd, &clientRect);
287+ width = clientRect.right - clientRect.left ;
288+ height = clientRect.bottom - clientRect.top ;
289+
290+ D2D1_SIZE_U size = D2D1::SizeU (width, height);
291+ D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties ();
292+ D2D1_HWND_RENDER_TARGET_PROPERTIES hwndRenderTargetProperties = D2D1::HwndRenderTargetProperties (hwnd, size);
293+
294+ SafeRelease (&renderTarget);
295+ direct2DHandler.direct2DFactory ->CreateHwndRenderTarget (renderTargetProperties, hwndRenderTargetProperties, &renderTarget);
296+ renderTarget->SetDpi (96 .0f , 96 .0f );
297+ DBGASSERT (renderTarget != nullptr );
298+ }
299+
293300}
0 commit comments