Scope
D2ChannelRender.cpp
1 #include "StdAfx.h"
2 #include "D2ChannelRender.h"
3 #include "helpers/hresult_exception.h"
4 
5 namespace d2d {
6 
7 D2ChannelRender::D2ChannelRender()
8  : render_target(nullptr)
9  , bitmap(nullptr)
10  , text_brush(nullptr)
11  , text_format(nullptr)
12  , hwnd(NULL)
13  , scaletext(L"__ µm x __ µm") {
14 }
15 
18 }
19 
24 }
25 
26 void D2ChannelRender::Create(const HWND& _hwnd, const uint32_t& _xres, const uint32_t& _yres) {
27  std::lock_guard<std::mutex> lock(mutex);
28  hwnd = _hwnd;
29  render_target.reset(new RenderTarget(hwnd));
30  HResult hr;
31  hr(__FUNCTION__) = render_target->CreateBitmap(&bitmap, _yres, _xres);
32  hr(__FUNCTION__) = render_target->CreateSolidColorBrush(&text_brush);
33  hr(__FUNCTION__) = render_target->CreateTextFormat(&text_format);
34 }
35 
36 bool D2ChannelRender::Resize(const uint32_t& _xres, const uint32_t& _yres) {
37  std::lock_guard<std::mutex> lock(mutex);
38  HResult hr(render_target->Resize(D2D1::SizeU(_xres, _yres)), __FUNCTION__);
39  return SUCCEEDED(hr);
40 }
41 
42 bool D2ChannelRender::ResizeBitmap(const uint32_t& _xres, const uint32_t& _yres) {
43  std::lock_guard<std::mutex> lock(mutex);
45  HResult hr(render_target->CreateBitmap(&bitmap, _yres, _xres), __FUNCTION__);
46  return SUCCEEDED(hr);
47 }
48 
50  HResult hr;
51  std::lock_guard<std::mutex> lock(mutex);
52  // Do not draw if window not visible
53  if ( 0 == (D2D1_WINDOW_STATE_OCCLUDED & render_target->CheckWindowState()) ) {
54  render_target->BeginDraw();
55  render_target->SetTransform(D2D1::Matrix3x2F::Identity());
56  render_target->Clear(D2D1::ColorF(D2D1::ColorF::Red));
57 
58  render_target->DrawBitmap(bitmap);
59  render_target->DrawText(scaletext.c_str()
60  , &D2D1::RectF(0, 0, render_target->GetSize().width, render_target->GetSize().height)
61  , text_format
62  , text_brush
63  );
64  hr(__FUNCTION__) = render_target->Flush();
65  hr(__FUNCTION__) = render_target->EndDraw();
66  if ((HRESULT)hr == D2DERR_RECREATE_TARGET) {
67  hr(__FUNCTION__) = S_OK;
69  }
70  }
71 }
72 
73 void D2ChannelRender::UpdateScaleText(const std::wstring& _text) {
74  std::lock_guard<std::mutex> lock(mutex);
75  scaletext = _text;
76 }
77 
78 
79 }
ID2D1SolidColorBrush * text_brush
Direct2D brush for text (do not use a unique_ptr for COM interfaces)
virtual bool Resize(const uint32_t &_xres, const uint32_t &_yres)
Resizes the render target.
~D2ChannelRender()
Discard resources and delete the render_target.
virtual void UpdateScaleText(const std::wstring &_text)
Update the scale text.
HWND hwnd
Window handle.
Wrappers around the Direct2D interface.
void SafeRelease(Interface **ppInterfaceToRelease)
A safe release for COM objects.
Definition: helpers.h:25
void DiscardDeviceResources()
Safely release all Direct2D resources.
virtual void Render()
Renders the bitmap and the scale text.
This is the include file for standard system include files, or project specific include files that ar...
IDWriteTextFormat * text_format
Direct2D text format (do not use a unique_ptr for COM interfaces)
std::unique_ptr< RenderTarget > render_target
Direct2D render target.
std::mutex mutex
mutex for protection
ID2D1Bitmap * bitmap
Direct2D bitmap (do not use a unique_ptr for COM interfaces)
Wrapper around a Direct2D render target and the underlying Direct2D factory and IDWriteFactory.
Definition: d2wrap.h:71
bool ResizeBitmap(const uint32_t &_xres, const uint32_t &_yres)
Resizes the bitmap.
virtual void Create(const HWND &_hwnd, const uint32_t &_xres, const uint32_t &_yres)
Creates the render target for a hwnd, a bitmap with the desired resolution, a brushes for painting ...
std::wstring scaletext
String with the scale information, ala L"9.5 µm".