Kiwano Engine v1.3.x
helper.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/math/Math.h>
23#include <kiwano/utils/Logger.h>
24#include <kiwano/render/Color.h>
25#include <kiwano/platform/win32/ComPtr.hpp>
26#include <d2d1_1.h>
27#include <dwrite.h>
28
29namespace kiwano
30{
31namespace DX
32{
33
34template <typename T>
35inline void SafeRelease(T*& ptr)
36{
37 if (ptr != nullptr)
38 {
39 ptr->Release();
40 ptr = nullptr;
41 }
42}
43
44template <typename T>
45inline T* SafeAcquire(T* ptr)
46{
47 if (ptr != nullptr)
48 {
49 ptr->AddRef();
50 }
51 return ptr;
52}
53
54//
55// Point2F
56//
57
58inline const D2D1_POINT_2F& ConvertToPoint2F(const Vec2& vec2)
59{
60 return reinterpret_cast<const D2D1_POINT_2F&>(vec2);
61}
62
63inline D2D1_POINT_2F& ConvertToPoint2F(Vec2& vec2)
64{
65 return reinterpret_cast<D2D1_POINT_2F&>(vec2);
66}
67
68inline const D2D1_POINT_2F* ConvertToPoint2F(const Vec2* vec2)
69{
70 return reinterpret_cast<const D2D1_POINT_2F*>(vec2);
71}
72
73inline D2D1_POINT_2F* ConvertToPoint2F(Vec2* vec2)
74{
75 return reinterpret_cast<D2D1_POINT_2F*>(vec2);
76}
77
78//
79// Size
80//
81
82inline const D2D1_SIZE_F& ConvertToSizeF(const Vec2& vec2)
83{
84 return reinterpret_cast<const D2D1_SIZE_F&>(vec2);
85}
86
87inline D2D1_SIZE_F& ConvertToSizeF(Vec2& vec2)
88{
89 return reinterpret_cast<D2D1_SIZE_F&>(vec2);
90}
91
92inline const D2D1_SIZE_F* ConvertToSizeF(const Vec2* vec2)
93{
94 return reinterpret_cast<const D2D1_SIZE_F*>(vec2);
95}
96
97inline D2D1_SIZE_F* ConvertToSizeF(Vec2* vec2)
98{
99 return reinterpret_cast<D2D1_SIZE_F*>(vec2);
100}
101
102inline const D2D1_SIZE_U& ConvertToSizeU(const math::Vec2T<uint32_t>& vec2)
103{
104 return reinterpret_cast<const D2D1_SIZE_U&>(vec2);
105}
106
107inline D2D1_SIZE_U& ConvertToSizeU(math::Vec2T<uint32_t>& vec2)
108{
109 return reinterpret_cast<D2D1_SIZE_U&>(vec2);
110}
111
112inline const D2D1_SIZE_U* ConvertToSizeU(const math::Vec2T<uint32_t>* vec2)
113{
114 return reinterpret_cast<const D2D1_SIZE_U*>(vec2);
115}
116
117inline D2D1_SIZE_U* ConvertToSizeU(math::Vec2T<uint32_t>* vec2)
118{
119 return reinterpret_cast<D2D1_SIZE_U*>(vec2);
120}
121
122//
123// RectF
124//
125
126inline const D2D1_RECT_F& ConvertToRectF(const Rect& rect)
127{
128 return reinterpret_cast<const D2D1_RECT_F&>(rect);
129}
130
131inline D2D1_RECT_F& ConvertToRectF(Rect& rect)
132{
133 return reinterpret_cast<D2D1_RECT_F&>(rect);
134}
135
136inline const D2D1_RECT_F* ConvertToRectF(const Rect* rect)
137{
138 return reinterpret_cast<const D2D1_RECT_F*>(rect);
139}
140
141inline D2D1_RECT_F* ConvertToRectF(Rect* rect)
142{
143 return reinterpret_cast<D2D1_RECT_F*>(rect);
144}
145
146//
147// ColorF
148//
149inline const D2D1_COLOR_F& ConvertToColorF(const Color& color)
150{
151 return reinterpret_cast<const D2D1_COLOR_F&>(color);
152}
153
154inline D2D1_COLOR_F& ConvertToColorF(Color& color)
155{
156 return reinterpret_cast<D2D1_COLOR_F&>(color);
157}
158
159inline const D2D1_COLOR_F* ConvertToColorF(const Color* color)
160{
161 return reinterpret_cast<const D2D1_COLOR_F*>(color);
162}
163
164inline D2D1_COLOR_F* ConvertToColorF(Color* color)
165{
166 return reinterpret_cast<D2D1_COLOR_F*>(color);
167}
168
169//
170// MatrixF
171//
172
173inline const D2D1_MATRIX_3X2_F& ConvertToMatrix3x2F(const Matrix3x2& matrix)
174{
175 return reinterpret_cast<const D2D1_MATRIX_3X2_F&>(matrix);
176}
177
178inline D2D1_MATRIX_3X2_F& ConvertToMatrix3x2F(Matrix3x2& matrix)
179{
180 return reinterpret_cast<D2D1_MATRIX_3X2_F&>(matrix);
181}
182
183inline const D2D1_MATRIX_3X2_F* ConvertToMatrix3x2F(const Matrix3x2* matrix)
184{
185 return reinterpret_cast<const D2D1_MATRIX_3X2_F*>(matrix);
186}
187
188inline D2D1_MATRIX_3X2_F* ConvertToMatrix3x2F(Matrix3x2* matrix)
189{
190 return reinterpret_cast<D2D1_MATRIX_3X2_F*>(matrix);
191}
192
193// Converts a length in device-independent pixels (DIPs) to a length in physical pixels.
194inline float ConvertDipsToPixels(float dips, float dpi)
195{
196 static const float dips_per_inch = 96.0f;
197 return math::Floor(dips * dpi / dips_per_inch + 0.5f); // Round to nearest integer.
198}
199
200} // namespace DX
201} // namespace kiwano