-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderDirect2D.cpp
More file actions
146 lines (115 loc) · 4.93 KB
/
renderDirect2D.cpp
File metadata and controls
146 lines (115 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <windows.h>
#include "renderDirect2D.h"
namespace gameApp {
void RenderDirect2D::BeginDraw() {
getInstance().createDeviceResources();
getInstance().m_pRenderTarget->GetTransform(&getInstance().prevTransform);
getInstance().m_pRenderTarget->BeginDraw();
}
void RenderDirect2D::EndDraw() {
HRESULT hres = getInstance().m_pRenderTarget->EndDraw();
if (hres == D2DERR_RECREATE_TARGET) {
DiscardDeviceResources();
}
getInstance().ResetTransformation();
}
void RenderDirect2D::ClearWindow(unsigned int color = 0x7FFFFFFF) {
getInstance().m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
getInstance().m_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF(color)));
}
WindowSize RenderDirect2D::GetWindowSize() {
D2D1_SIZE_F window = getInstance().m_pRenderTarget->GetSize();
return { window.width, window.height };
}
void RenderDirect2D::DrawLine(float x0, float y0, float x1, float y1, unsigned int hex, float alpha, float width, const struct TransformationList& transformations) {
ID2D1SolidColorBrush* brush = NULL;
getInstance().m_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF(hex, alpha)),
&brush);
getInstance().setTransformation(transformations);
getInstance().m_pRenderTarget->DrawLine(
D2D1::Point2F(x0, y0),
D2D1::Point2F(x1, y1),
brush,
width
);
brush->Release();
ResetTransformation();
}
void RenderDirect2D::setTransformation(const struct TransformationList& transformations) {
D2D1_MATRIX_3X2_F transform = getInstance().prevTransform;
for (unsigned int i = 0; i < transformations.length; i++) {
if (transformations.transformations[i].option > 3) {
continue;
}
switch (transformations.transformations[i].option) {
case 0:
transform = transform * D2D1::Matrix3x2F::Rotation(
transformations.transformations[i].param1,
D2D1::Point2F(transformations.transformations[i].x, transformations.transformations[i].y)
);
continue;
case 1:
transform = transform * D2D1::Matrix3x2F::Scale(
transformations.transformations[i].param1,
transformations.transformations[i].param2,
D2D1::Point2F(transformations.transformations[i].x, transformations.transformations[i].y)
);
continue;
case 2:
transform = transform * D2D1::Matrix3x2F::Skew(
transformations.transformations[i].param1,
transformations.transformations[i].param2,
D2D1::Point2F(transformations.transformations[i].x, transformations.transformations[i].y)
);
continue;
case 3:
transform = transform * D2D1::Matrix3x2F::Translation(
transformations.transformations[i].param1,
transformations.transformations[i].param2
);
continue;
default:
continue;
}
}
getInstance().m_pRenderTarget->SetTransform(transform);
}
void RenderDirect2D::OnResize(UINT width, UINT height) {
if (getInstance().m_pRenderTarget) {
getInstance().m_pRenderTarget->Resize(D2D1::SizeU(width, height));
}
}
void RenderDirect2D::DrawRect(const struct RectFloat& rect, unsigned int hex, float alpha, const struct TransformationList& transformations) {
D2D1_RECT_F rectangle = D2D1::RectF(
rect.x,
rect.y,
rect.x + rect.width,
rect.y + rect.height
);
ID2D1SolidColorBrush* brush = NULL;
getInstance().m_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF(hex, alpha)),
&brush);
getInstance().setTransformation(transformations);
getInstance().m_pRenderTarget->DrawRectangle(&rectangle, brush);
brush->Release();
ResetTransformation();
}
void RenderDirect2D::FillRect(const struct RectFloat& rect, unsigned int hex, float alpha, const struct TransformationList& transformations) {
D2D1_RECT_F rectangle = D2D1::RectF(
rect.x,
rect.y,
rect.x + rect.width,
rect.y + rect.height
);
ID2D1SolidColorBrush* brush = NULL;
getInstance().m_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF( hex, alpha )),
&brush);
getInstance().setTransformation(transformations);
getInstance().m_pRenderTarget->FillRectangle(&rectangle, brush);
brush->Release();
ResetTransformation();
}
}