Scope
d2wrap.cpp
1 #include "stdafx.h"
2 #include "d2wrap.h"
3 #include "helpers/hresult_exception.h"
4 
5 namespace d2d {
6 
7 float DPIScale::scaleX = 1.0f;
8 float DPIScale::scaleY = 1.0f;
9 
11  : target(nullptr)
12  , dwrite_factory(nullptr)
13  , pixelformat(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE)) {
14  HResult hr;
15 
16  // Get the size of the HWND client rect and crate render target to fill it
17  RECT rc;
18  GetClientRect(_hwnd, &rc);
19  D2D1_SIZE_U size = D2D1::SizeU( rc.right - rc.left, rc.bottom - rc.top );
20 
21  // Create a Direct2D render target.
22  const D2D1_RENDER_TARGET_PROPERTIES targetProperties = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, pixelformat);
23  try {
24  hr(__FUNCTION__) = factory.pd2d_factory->CreateHwndRenderTarget( targetProperties
25  , D2D1::HwndRenderTargetProperties(_hwnd, size, D2D1_PRESENT_OPTIONS_IMMEDIATELY)
26  , &target);
27 
28  assert(target != nullptr);
29 
30  hr(__FUNCTION__) = DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED
31  , __uuidof(IDWriteFactory)
32  , reinterpret_cast<IUnknown**>(&dwrite_factory));
33 
34  target->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
35  }
36  catch (const hresult_exception& e) {
37  e; // avoid warning C4101 in release build
38  DBOUT(L"Exception in d2d::render_target::render_target, hresult" << e.hr);
39  }
40 }
41 
45 }
46 
47 HRESULT RenderTarget::CreateSolidColorBrush(ID2D1SolidColorBrush** _brush, const D2D1_COLOR_F& _color) {
48  SafeRelease(_brush);
49  return HResult(target->CreateSolidColorBrush(_color, _brush), __FUNCTION__);
50 }
51 
52 HRESULT RenderTarget::CreateBitmap(ID2D1Bitmap** _bitmap, const uint32_t& _yres, const uint32_t& _xres) {
53  HResult hr;
54  // Release the old bitmap
55  SafeRelease(_bitmap);
56 
57  // ... and create the new one
58  try {
59  FLOAT systemdpi_x = 96;
60  FLOAT systemdpi_y = 96;
61  factory.pd2d_factory->GetDesktopDpi(&systemdpi_x, &systemdpi_y);
62 
63  hr(__FUNCTION__) = target->CreateBitmap(
64  D2D1::SizeU(_xres, _yres),
65  NULL,
66  _xres*4, // 4 bytes per pixel (RGBA)
67  // the following provokes an error for testing purposes
68  //D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_R32G32B32A32_UINT), systemdpi.x, systemdpi.y),
69  D2D1::BitmapProperties(pixelformat, systemdpi_x, systemdpi_y),
70  _bitmap );
71  }
72  catch (const hresult_exception& e) {
73  e; // avoid warning C4101 in release build
74  DBOUT(L"Exception in d2d::render_target.CreateBitmap, hresult=" << e.hr);
75  }
76  return hr;
77 }
78 
79 HRESULT RenderTarget::CreateTextFormat(IDWriteTextFormat** _text_format) {
80  SafeRelease(_text_format);
81  HResult hr(dwrite_factory->CreateTextFormat(
82  L"Consolas", // Font family name.
83  NULL, // Font collection (NULL sets it to use the system font collection).
84  DWRITE_FONT_WEIGHT_REGULAR,
85  DWRITE_FONT_STYLE_NORMAL,
86  DWRITE_FONT_STRETCH_NORMAL,
87  14.0f,
88  L"en-us",
89  _text_format ), __FUNCTION__);
90 
91  hr(__FUNCTION__) = (*_text_format)->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
92 
93  return hr;
94 }
95 
96 HRESULT RenderTarget::CreateRectangleGeometry(const D2D1_RECT_F& _rectangle, ID2D1RectangleGeometry **_rectangleGeometry) {
97  SafeRelease(_rectangleGeometry);
98  return HResult(factory.pd2d_factory->CreateRectangleGeometry(_rectangle, _rectangleGeometry), __FUNCTION__);
99 }
100 
102  target->BeginDraw();
103 }
104 
105 D2D1_WINDOW_STATE RenderTarget::CheckWindowState() {
106  return target->CheckWindowState();
107 }
108 
109 void RenderTarget::SetTransform(const D2D1_MATRIX_3X2_F& _transform) {
110  target->SetTransform(_transform);
111 }
112 
114  target->SetTransform(D2D1::Matrix3x2F::Identity());
115  target->Clear(D2D1::ColorF(255.0f, 255.0f, 255.0f,0.f));
116 }
117 
118 void RenderTarget::Clear(const D2D1_COLOR_F& _clearColor) {
119  target->SetTransform(D2D1::Matrix3x2F::Identity());
120  target->Clear(_clearColor);
121 }
122 
123 void RenderTarget::DrawBitmap(ID2D1Bitmap* _bitmap, D2D1_BITMAP_INTERPOLATION_MODE _mode) {
124  ATLASSERT(_bitmap != nullptr);
125  auto size = target->GetSize();
126  auto sizebitmap = _bitmap->GetSize();
127  auto p = _bitmap->GetPixelSize();
128  target->DrawBitmap(_bitmap, D2D1::RectF(0, 0, size.width, size.height), 1, _mode);
129 }
130 
131 void RenderTarget::DrawLine(const D2D1_POINT_2F& _point0, const D2D1_POINT_2F& _point1, ID2D1Brush* _brush, const FLOAT& _strokeWidth) {
132  target->DrawLine(_point0, _point1, _brush, _strokeWidth);
133 }
134 
135 void RenderTarget::DrawText(const CString& _text, const D2D1_RECT_F* _layoutRect, IDWriteTextFormat* _text_format, ID2D1Brush* _brush) {
136  target->DrawText(_text.GetString()
137  , _text.GetLength()
138  , _text_format
139  , _layoutRect
140  , _brush );
141 }
142 
143 void RenderTarget::FillRectangle(const D2D1_RECT_F* _rect, ID2D1Brush* _brush) {
144  target->FillRectangle(_rect, _brush);
145 }
146 
147 D2D1_SIZE_F RenderTarget::GetSize() const {
148  return target->GetSize();
149 }
150 
152  return HResult(target->EndDraw(),__FUNCTION__);
153 }
154 
155 HRESULT RenderTarget::Resize(const D2D1_SIZE_U& _pixelSize) {
156  return HResult(target->Resize(_pixelSize), __FUNCTION__);
157 }
158 
160  return HResult(target->Flush(), __FUNCTION__);
161 }
162 
163 }
void Clear(const D2D1_COLOR_F &_clearColor=D2D1::ColorF(D2D1::ColorF::White))
Clear the window with a certain color.
Definition: d2wrap.cpp:118
IDWriteFactory * dwrite_factory
Direct2D write factory for text rendering (do not use a unique_ptr for COM interfaces) ...
Definition: d2wrap.h:81
void ClearWindow()
Clear the window with black.
Definition: d2wrap.cpp:113
Exception class for HRESULTs.
HRESULT CreateBitmap(ID2D1Bitmap **_bitmap, const uint32_t &_yres, const uint32_t &_xres)
Creates a new Direct2D bitmap.
Definition: d2wrap.cpp:52
void SetTransform(const D2D1_MATRIX_3X2_F &_transform)
Set current transform matrix.
Definition: d2wrap.cpp:109
void DrawLine(const D2D1_POINT_2F &_point0, const D2D1_POINT_2F &_point1, ID2D1Brush *_brush, const FLOAT &_strokeWidth)
Draw a line.
Definition: d2wrap.cpp:131
HRESULT CreateTextFormat(IDWriteTextFormat **_text_format)
Create a text format.
Definition: d2wrap.cpp:79
d2dfactory< D2D1_FACTORY_TYPE_MULTI_THREADED > factory
Direct2D factory.
Definition: d2wrap.h:75
void BeginDraw()
Trigger Direct2D begin draw.
Definition: d2wrap.cpp:101
ID2D1HwndRenderTarget * target
Direct2D render target (do not use a unique_ptr for COM interfaces)
Definition: d2wrap.h:78
Wrappers around the Direct2D interface.
void FillRectangle(const D2D1_RECT_F *_rect, ID2D1Brush *_brush)
Fill a rectangle.
Definition: d2wrap.cpp:143
static float scaleX
x scale factor
Definition: d2wrap.h:15
void SafeRelease(Interface **ppInterfaceToRelease)
A safe release for COM objects.
Definition: helpers.h:25
Parts of the code are modified from Microsoft's Direct2D nbody example: Copyright (c) Microsoft Corpo...
ID2D1Factory * pd2d_factory
Direct2D factory (do not use a unique_ptr for COM interfaces)
Definition: d2wrap.h:42
HRESULT hr
Current HRESULT.
This is the include file for standard system include files, or project specific include files that ar...
HRESULT EndDraw()
Trigger Direct2D end draw.
Definition: d2wrap.cpp:151
HRESULT Resize(const D2D1_SIZE_U &_pixelSize)
Resize render target.
Definition: d2wrap.cpp:155
HRESULT CreateSolidColorBrush(ID2D1SolidColorBrush **_brush, const D2D1_COLOR_F &_color=D2D1::ColorF(D2D1::ColorF::White))
Create a solid color brush.
Definition: d2wrap.cpp:47
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
D2D1_PIXEL_FORMAT pixelformat
current pixel format
Definition: d2wrap.h:84
static float scaleY
y scale factor
Definition: d2wrap.h:18
void DrawText(const CString &_text, const D2D1_RECT_F *_layoutRect, IDWriteTextFormat *_text_format, ID2D1Brush *_brush)
Draw text.
Definition: d2wrap.cpp:135
HRESULT CreateRectangleGeometry(const D2D1_RECT_F &_rectangle, ID2D1RectangleGeometry **_rectangleGeometry)
Create a rectangle geometry.
Definition: d2wrap.cpp:96
D2D1_SIZE_F GetSize() const
Get size of render target.
Definition: d2wrap.cpp:147
~RenderTarget()
Release resources.
Definition: d2wrap.cpp:42
RenderTarget(const RenderTarget &)
disable copy
void DrawBitmap(ID2D1Bitmap *_bitmap, D2D1_BITMAP_INTERPOLATION_MODE _mode=D2D1_BITMAP_INTERPOLATION_MODE_LINEAR)
Draws a Direct2D bitmap.
Definition: d2wrap.cpp:123
HRESULT Flush()
Flush the render target.
Definition: d2wrap.cpp:159
D2D1_WINDOW_STATE CheckWindowState()
Check state of window, e.g.
Definition: d2wrap.cpp:105