Scope
d2wrap.h
Go to the documentation of this file.
1 #pragma once
2 #include "helpers/helpers.h"
3 #include "ScopeDefines.h" // to get SCOPE_DIRECT2D_DEBUG
4 
10 namespace d2d {
11 
13 class DPIScale {
15  static float scaleX;
16 
18  static float scaleY;
19 
20 public:
22  static void Initialize(ID2D1Factory *pFactory) {
23  FLOAT X, Y;
24  pFactory->GetDesktopDpi(&X,&Y);
25  scaleX = X/96.0f;
26  scaleY = Y/96.0f;
27  }
28 
30  template <typename T>
31  static D2D1_POINT_2F PixelsToDips(T x, T y) {
32  return D2D1::Point2F(static_cast<float>(x) / scaleX, static_cast<float>(y) / scaleY);
33  }
34 };
35 
36 
38 template <D2D1_FACTORY_TYPE policy_class>
39 class d2dfactory {
40 public:
42  ID2D1Factory* pd2d_factory;
43 
45  HRESULT hr;
46 
49  : hr(0) {
50  #ifdef _DEBUG
51  if ( SCOPE_DIRECT2D_DEBUG ) {
52  D2D1_FACTORY_OPTIONS options;
53  options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
54  D2D1CreateFactory(policy_class, options, &pd2d_factory);
55  }
56  #endif
57  #ifndef _DEBUG
58  D2D1CreateFactory(policy_class, &pd2d_factory);
59  #endif
60  DPIScale::Initialize(pd2d_factory);
61  }
62 
65  SafeRelease(&pd2d_factory);
66  }
67 };
68 
69 
71 class RenderTarget {
72 
73 protected:
76 
78  ID2D1HwndRenderTarget* target;
79 
81  IDWriteFactory* dwrite_factory;
82 
84  D2D1_PIXEL_FORMAT pixelformat;
85 
86 protected:
88  RenderTarget(const RenderTarget& );
89 
92 
93 public:
95  RenderTarget(HWND _hwnd);
96 
98  ~RenderTarget();
99 
103  HRESULT CreateSolidColorBrush(ID2D1SolidColorBrush** _brush, const D2D1_COLOR_F& _color = D2D1::ColorF(D2D1::ColorF::White));
104 
108  HRESULT CreateBitmap(ID2D1Bitmap** _bitmap, const uint32_t& _yres, const uint32_t& _xres);
109 
111  HRESULT CreateTextFormat(IDWriteTextFormat** _text_format);
112 
114  HRESULT CreateRectangleGeometry(const D2D1_RECT_F& _rectangle, ID2D1RectangleGeometry** _rectangleGeometry);
120  void BeginDraw();
121 
123  void SetTransform(const D2D1_MATRIX_3X2_F& _transform);
124 
126  void Clear(const D2D1_COLOR_F& _clearColor = D2D1::ColorF(D2D1::ColorF::White));
127 
129  void ClearWindow();
130 
136  void DrawBitmap(ID2D1Bitmap* _bitmap, D2D1_BITMAP_INTERPOLATION_MODE _mode = D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
137 
139  void DrawLine(const D2D1_POINT_2F& _point0, const D2D1_POINT_2F& _point1, ID2D1Brush* _brush, const FLOAT& _strokeWidth);
140 
142  void DrawText(const CString& _text, const D2D1_RECT_F* _layoutRect, IDWriteTextFormat* _text_format, ID2D1Brush* _brush);
143 
145  void FillRectangle(const D2D1_RECT_F* _rect, ID2D1Brush* _brush);
146 
148  HRESULT Flush();
149 
151  HRESULT EndDraw();
155  HRESULT Resize(const D2D1_SIZE_U& _pixelSize);
156 
158  D2D1_WINDOW_STATE CheckWindowState();
159 
161  D2D1_SIZE_F GetSize() const;
162 
163 
164 
165 };
166 
167 
168 }
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
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
Helper class to convert from screen mouse coordinates to device independent pixels for Direct2D...
Definition: d2wrap.h:13
static void Initialize(ID2D1Factory *pFactory)
Initialize with desktop dpis.
Definition: d2wrap.h:22
d2dfactory< D2D1_FACTORY_TYPE_MULTI_THREADED > factory
Direct2D factory.
Definition: d2wrap.h:75
void BeginDraw()
Trigger Direct2D begin draw.
Definition: d2wrap.cpp:101
RenderTarget & operator=(const RenderTarget &)
disable assignment
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
ID2D1Factory * pd2d_factory
Direct2D factory (do not use a unique_ptr for COM interfaces)
Definition: d2wrap.h:42
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
Simple wrapper around a Direct2D factory.
Definition: d2wrap.h:39
D2D1_PIXEL_FORMAT pixelformat
current pixel format
Definition: d2wrap.h:84
~d2dfactory()
Safely release the factory.
Definition: d2wrap.h:64
static D2D1_POINT_2F PixelsToDips(T x, T y)
Convert screen pixels to device independent points.
Definition: d2wrap.h:31
static float scaleY
y scale factor
Definition: d2wrap.h:18
d2dfactory()
Create the factory and initialize the global DPIScale object, if debug build activate Direct2D debug ...
Definition: d2wrap.h:48
Wrapper around a Direct2D render target and the underlying Direct2D factory and IDWriteFactory.
Definition: d2wrap.h:71
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
Various helper functions and classes for Scope.
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 hr
for error logging
Definition: d2wrap.h:45
HRESULT Flush()
Flush the render target.
Definition: d2wrap.cpp:159
D2D1_WINDOW_STATE CheckWindowState()
Check state of window, e.g.
Definition: d2wrap.cpp:105