-
Notifications
You must be signed in to change notification settings - Fork 0
/
wind2dmultipathtest.c
386 lines (336 loc) · 9.11 KB
/
wind2dmultipathtest.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// 24 september 2015
#define UNICODE
#define _UNICODE
#define STRICT
#define STRICT_TYPED_ITEMIDS
#define CINTERFACE
#define COBJMACROS
// see https://github.com/golang/go/issues/9916#issuecomment-74812211
#define INITGUID
#define _USE_MATH_DEFINES
// get Windows version right; right now Windows Vista
#define WINVER 0x0600 /* according to Microsoft's winnls.h */
#define _WIN32_WINNT 0x0600 /* according to Microsoft's sdkddkver.h */
#define _WIN32_WINDOWS 0x0600 /* according to Microsoft's pdh.h */
#define _WIN32_IE 0x0700 /* according to Microsoft's sdkddkver.h */
#define NTDDI_VERSION 0x06000000 /* according to Microsoft's sdkddkver.h */
#include <windows.h>
#include <commctrl.h>
#include <stdint.h>
#include <uxtheme.h>
#include <string.h>
#include <wchar.h>
#include <windowsx.h>
#include <vsstyle.h>
#include <vssym32.h>
#include <stdarg.h>
#include <oleacc.h>
#include <stdio.h>
#include <shobjidl.h>
#include <math.h>
#include <d2d1.h>
static ID2D1Factory *d2dfactory = NULL;
HRESULT initDraw(void)
{
D2D1_FACTORY_OPTIONS opts;
ZeroMemory(&opts, sizeof (D2D1_FACTORY_OPTIONS));
// TODO make this an option
opts.debugLevel = D2D1_DEBUG_LEVEL_NONE;
return D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
&IID_ID2D1Factory,
&opts,
(void **) (&d2dfactory));
}
void uninitDraw(void)
{
ID2D1Factory_Release(d2dfactory);
}
ID2D1HwndRenderTarget *makeHWNDRenderTarget(HWND hwnd)
{
D2D1_RENDER_TARGET_PROPERTIES props;
D2D1_HWND_RENDER_TARGET_PROPERTIES hprops;
HDC dc;
RECT r;
ID2D1HwndRenderTarget *rt;
// we need a DC for the DPI
// we *could* just use the screen DPI but why when we have a window handle and its DC has a DPI
dc = GetDC(hwnd);
ZeroMemory(&props, sizeof (D2D1_RENDER_TARGET_PROPERTIES));
props.type = D2D1_RENDER_TARGET_TYPE_DEFAULT;
props.pixelFormat.format = DXGI_FORMAT_UNKNOWN;
props.pixelFormat.alphaMode = D2D1_ALPHA_MODE_UNKNOWN;
props.dpiX = GetDeviceCaps(dc, LOGPIXELSX);
props.dpiY = GetDeviceCaps(dc, LOGPIXELSY);
props.usage = D2D1_RENDER_TARGET_USAGE_NONE;
props.minLevel = D2D1_FEATURE_LEVEL_DEFAULT;
ReleaseDC(hwnd, dc);
GetClientRect(hwnd, &r);
ZeroMemory(&hprops, sizeof (D2D1_HWND_RENDER_TARGET_PROPERTIES));
hprops.hwnd = hwnd;
hprops.pixelSize.width = r.right - r.left;
hprops.pixelSize.height = r.bottom - r.top;
hprops.presentOptions = D2D1_PRESENT_OPTIONS_NONE;
ID2D1Factory_CreateHwndRenderTarget(d2dfactory,
&props,
&hprops,
&rt);
return rt;
}
void initdraw(void);
void uninitdraw(void);
HRESULT draw(ID2D1RenderTarget *);
ID2D1HwndRenderTarget *rt = NULL;
LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HRESULT hr;
RECT client;
WINDOWPOS *wp = (WINDOWPOS *) lParam;
D2D1_SIZE_U size;
switch (uMsg) {
case WM_PAINT:
if (rt == NULL)
rt = makeHWNDRenderTarget(hwnd);
hr = draw((ID2D1RenderTarget *) rt);
switch (hr) {
case S_OK:
ValidateRect(hwnd, NULL);
break;
case D2DERR_RECREATE_TARGET:
// DON'T validate the rect
// instead, simply drop the render target
// we'll get another WM_PAINT and make the render target again
ID2D1HwndRenderTarget_Release(rt);
rt = NULL;
break;
}
return 0;
case WM_WINDOWPOSCHANGED:
if ((wp->flags & SWP_NOSIZE) != 0)
break;
if (rt == NULL)
rt = makeHWNDRenderTarget(hwnd);
else {
GetClientRect(hwnd, &client);
size.width = client.right - client.left;
size.height = client.bottom - client.top;
// don't track the error; we'll get that in EndDraw()
// see https://msdn.microsoft.com/en-us/library/windows/desktop/dd370994%28v=vs.85%29.aspx
ID2D1HwndRenderTarget_Resize(rt, &size);
}
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
int main(void)
{
WNDCLASSW wc;
HWND mainwin;
MSG msg;
CoInitialize(NULL);
initDraw();
initdraw();
ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = L"mainwin";
wc.lpfnWndProc = wndproc;
wc.hInstance = GetModuleHandle(NULL);
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
RegisterClassW(&wc);
mainwin = CreateWindowExW(0,
L"mainwin", L"mainwin",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(mainwin, SW_SHOWDEFAULT);
UpdateWindow(mainwin);
while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
if (rt != NULL)
ID2D1HwndRenderTarget_Release(rt);
uninitdraw();
uninitDraw();
CoUninitialize();
return 0;
}
ID2D1EllipseGeometry *topleft[4];
ID2D1GeometryGroup *topleftGroup;
ID2D1EllipseGeometry *topright[4];
ID2D1GeometryGroup *toprightGroup;
ID2D1PathGeometry *bottomleft;
ID2D1PathGeometry *bottomright;
ID2D1EllipseGeometry *mkcircle(int x, int y, int rad)
{
D2D1_ELLIPSE e;
ID2D1EllipseGeometry *eg;
e.point.x = x;
e.point.y = y;
e.radiusX = rad;
e.radiusY = rad;
ID2D1Factory_CreateEllipseGeometry(d2dfactory,
&e,
&eg);
return eg;
}
void addCircle(ID2D1GeometrySink *sink, int x, int y, int rad)
{
D2D1_POINT_2F begin;
D2D1_ARC_SEGMENT a;
begin.x = x + rad;
begin.y = y;
ID2D1GeometrySink_BeginFigure(sink,
begin,
D2D1_FIGURE_BEGIN_FILLED);
// TODO why doesn't a single arc work?
a.point.x = x - rad;
a.point.y = y;
a.size.width = rad;
a.size.height = rad;
a.rotationAngle = 180;
a.sweepDirection = D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE;
a.arcSize = D2D1_ARC_SIZE_LARGE;
ID2D1GeometrySink_AddArc(sink,
&a);
a.point.x = x + rad;
a.point.y = y;
a.size.width = rad;
a.size.height = rad;
a.rotationAngle = 180;
a.sweepDirection = D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE;
a.arcSize = D2D1_ARC_SIZE_LARGE;
ID2D1GeometrySink_AddArc(sink,
&a);
ID2D1GeometrySink_EndFigure(sink,
D2D1_FIGURE_END_CLOSED);
}
void initdraw(void)
{
ID2D1GeometrySink *sink;
topleft[0] = mkcircle(120, 120, 25);
topleft[1] = mkcircle(120, 120, 50);
topleft[2] = mkcircle(120, 120, 75);
topleft[3] = mkcircle(120, 120, 100);
ID2D1Factory_CreateGeometryGroup(d2dfactory,
D2D1_FILL_MODE_ALTERNATE,
(ID2D1Geometry **) topleft,
4,
&topleftGroup);
topright[0] = mkcircle(340, 120, 25);
topright[1] = mkcircle(340, 120, 50);
topright[2] = mkcircle(340, 120, 75);
topright[3] = mkcircle(340, 120, 100);
ID2D1Factory_CreateGeometryGroup(d2dfactory,
D2D1_FILL_MODE_WINDING,
(ID2D1Geometry **) topright,
4,
&toprightGroup);
ID2D1Factory_CreatePathGeometry(d2dfactory,
&bottomleft);
ID2D1PathGeometry_Open(bottomleft,
&sink);
ID2D1GeometrySink_SetFillMode(sink,
D2D1_FILL_MODE_ALTERNATE);
addCircle(sink, 120, 340, 25);
addCircle(sink, 120, 340, 50);
addCircle(sink, 120, 340, 75);
addCircle(sink, 120, 340, 100);
ID2D1GeometrySink_Close(sink);
ID2D1Factory_CreatePathGeometry(d2dfactory,
&bottomright);
ID2D1PathGeometry_Open(bottomright,
&sink);
ID2D1GeometrySink_SetFillMode(sink,
D2D1_FILL_MODE_WINDING);
addCircle(sink, 340, 340, 25);
addCircle(sink, 340, 340, 50);
addCircle(sink, 340, 340, 75);
addCircle(sink, 340, 340, 100);
ID2D1GeometrySink_Close(sink);
}
void uninitdraw(void)
{
ID2D1PathGeometry_Release(bottomright);
ID2D1PathGeometry_Release(bottomleft);
ID2D1GeometryGroup_Release(toprightGroup);
ID2D1EllipseGeometry_Release(topright[3]);
ID2D1EllipseGeometry_Release(topright[2]);
ID2D1EllipseGeometry_Release(topright[1]);
ID2D1EllipseGeometry_Release(topright[0]);
ID2D1GeometryGroup_Release(topleftGroup);
ID2D1EllipseGeometry_Release(topleft[3]);
ID2D1EllipseGeometry_Release(topleft[2]);
ID2D1EllipseGeometry_Release(topleft[1]);
ID2D1EllipseGeometry_Release(topleft[0]);
}
HRESULT draw(ID2D1RenderTarget *rt)
{
COLORREF rgb;
D2D1_COLOR_F color;
ID2D1SolidColorBrush *fillBrush, *drawBrush;
ID2D1RenderTarget_BeginDraw(rt);
color.r = 0.82;
color.g = 0.86;
color.b = 0.96;
color.a = 1.0;
ID2D1RenderTarget_CreateSolidColorBrush(rt,
&color,
NULL,
&fillBrush);
color.r = 0.25;
color.g = 0.39;
color.b = 0.69;
color.a = 1.0;
ID2D1RenderTarget_CreateSolidColorBrush(rt,
&color,
NULL,
&drawBrush);
rgb = GetSysColor(COLOR_BTNFACE);
color.r = ((float) (rgb & 0xFF)) / 255;
color.g = ((float) ((rgb & 0xFF00) >> 8)) / 255;
color.b = ((float) ((rgb & 0xFF0000) >> 16)) / 255;
color.a = 1.0;
ID2D1RenderTarget_Clear(rt, &color);
ID2D1RenderTarget_FillGeometry(rt,
(ID2D1Geometry *) topleftGroup,
(ID2D1Brush *) fillBrush,
NULL);
ID2D1RenderTarget_DrawGeometry(rt,
(ID2D1Geometry *) topleftGroup,
(ID2D1Brush *) drawBrush,
1.0,
NULL);
ID2D1RenderTarget_FillGeometry(rt,
(ID2D1Geometry *) toprightGroup,
(ID2D1Brush *) fillBrush,
NULL);
ID2D1RenderTarget_DrawGeometry(rt,
(ID2D1Geometry *) toprightGroup,
(ID2D1Brush *) drawBrush,
1.0,
NULL);
ID2D1RenderTarget_FillGeometry(rt,
(ID2D1Geometry *) bottomleft,
(ID2D1Brush *) fillBrush,
NULL);
ID2D1RenderTarget_DrawGeometry(rt,
(ID2D1Geometry *) bottomleft,
(ID2D1Brush *) drawBrush,
1.0,
NULL);
ID2D1RenderTarget_FillGeometry(rt,
(ID2D1Geometry *) bottomright,
(ID2D1Brush *) fillBrush,
NULL);
ID2D1RenderTarget_DrawGeometry(rt,
(ID2D1Geometry *) bottomright,
(ID2D1Brush *) drawBrush,
1.0,
NULL);
ID2D1SolidColorBrush_Release(fillBrush);
ID2D1SolidColorBrush_Release(drawBrush);
return ID2D1RenderTarget_EndDraw(rt, NULL, NULL);
}