-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.cpp
More file actions
563 lines (470 loc) · 19.5 KB
/
renderer.cpp
File metadata and controls
563 lines (470 loc) · 19.5 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include "renderer.hpp"
#include "ezui.hpp"
DX11Renderer::DX11Renderer(HWND hwnd) : hwnd(hwnd) {}
DX11Renderer::~DX11Renderer() {
if (renderTargetView) renderTargetView->Release();
if (swapChain) swapChain->Release();
if (d3dContext) d3dContext->Release();
if (d3dDevice) d3dDevice->Release();
if (vertexBuffer) vertexBuffer->Release();
if (indexBuffer) indexBuffer->Release();
if (vertexShader) vertexShader->Release();
if (pixelShader) pixelShader->Release();
if (inputLayout) inputLayout->Release();
}
void DX11Renderer::initD3D11() {
if (!hwnd) {
ezUI::dbg("Invalid window handle (hwnd)!");
return;
}
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = 0;
swapChainDesc.BufferDesc.Height = 0;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.Windowed = TRUE;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
D3D_FEATURE_LEVEL featureLevels[] = {
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
D3D_FEATURE_LEVEL featureLevel;
HRESULT hr = D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
featureLevels,
ARRAYSIZE(featureLevels),
D3D11_SDK_VERSION,
&swapChainDesc,
&swapChain,
&d3dDevice,
&featureLevel,
&d3dContext
);
if (FAILED(hr)) {
ezUI::dbg("Failed to create D3D11 device and swap chain! HRESULT: " + std::to_string(hr));
return;
}
createRenderTarget();
createBlendState();
createVertexBuffer(1024);
createShaders();
D3D11_VIEWPORT viewport = {};
RECT rect;
GetClientRect(hwnd, &rect);
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = static_cast<float>(rect.right - rect.left);
viewport.Height = static_cast<float>(rect.bottom - rect.top);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
d3dContext->RSSetViewports(1, &viewport);
}
void DX11Renderer::createRenderTarget() {
ID3D11Texture2D* backBuffer = nullptr;
HRESULT hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer);
if (FAILED(hr)) {
ezUI::dbg("Failed to get the swap chain's back buffer! HRESULT: " + std::to_string(hr));
return;
}
hr = d3dDevice->CreateRenderTargetView(backBuffer, nullptr, &renderTargetView);
backBuffer->Release();
if (FAILED(hr)) {
ezUI::dbg("Failed to create render target view! HRESULT: " + std::to_string(hr));
return;
}
d3dContext->OMSetRenderTargets(1, &renderTargetView, nullptr);
}
void DX11Renderer::createBlendState() {
D3D11_BLEND_DESC blendDesc = {};
blendDesc.RenderTarget[0].BlendEnable = TRUE;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
ID3D11BlendState* blendState = nullptr;
HRESULT hr = d3dDevice->CreateBlendState(&blendDesc, &blendState);
if (FAILED(hr)) {
ezUI::dbg("Failed to create blend state! HRESULT: " + std::to_string(hr));
return;
}
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
d3dContext->OMSetBlendState(blendState, blendFactor, 0xffffffff);
blendState->Release();
}
void DX11Renderer::createVertexBuffer(size_t vertexCount, size_t indexCount) {
if (vertexBuffer) {
vertexBuffer->Release();
vertexBuffer = nullptr;
}
if (indexCount > 0 && indexBuffer) {
indexBuffer->Release();
indexBuffer = nullptr;
}
D3D11_BUFFER_DESC vertexBufferDesc = {};
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
vertexBufferDesc.ByteWidth = static_cast<UINT>(sizeof(Vertex) * vertexCount);
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
HRESULT hr = d3dDevice->CreateBuffer(&vertexBufferDesc, nullptr, &vertexBuffer);
if (FAILED(hr)) {
ezUI::dbg("Failed to create vertex buffer! HRESULT: " + std::to_string(hr));
return;
}
if (indexCount > 0) {
D3D11_BUFFER_DESC indexBufferDesc = {};
indexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
indexBufferDesc.ByteWidth = static_cast<UINT>(sizeof(UINT) * indexCount);
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
hr = d3dDevice->CreateBuffer(&indexBufferDesc, nullptr, &indexBuffer);
if (FAILED(hr)) {
ezUI::dbg("Failed to create index buffer! HRESULT: " + std::to_string(hr));
return;
}
}
}
void DX11Renderer::clearScreen(float r, float g, float b, float a) {
float color[4] = { r, g, b, a };
d3dContext->ClearRenderTargetView(renderTargetView, color);
}
void DX11Renderer::present() {
swapChain->Present(1, 0);
}
void DX11Renderer::drawRectangle(float x, float y, float width, float height, const Color& color) {
if (!d3dDevice || !d3dContext) {
ezUI::dbg("Device or Context not initialized!");
return;
}
RECT rect;
GetClientRect(hwnd, &rect);
float left = (2.0f * x / (rect.right - rect.left)) - 1.0f;
float right = (2.0f * (x + width) / (rect.right - rect.left)) - 1.0f;
float top = 1.0f - (2.0f * y / (rect.bottom - rect.top));
float bottom = 1.0f - (2.0f * (y + height) / (rect.bottom - rect.top));
Vertex vertices[] = {
{ XMFLOAT3(left, top, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) },
{ XMFLOAT3(right, top, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) },
{ XMFLOAT3(left, bottom, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) },
{ XMFLOAT3(right, bottom, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) }
};
createVertexBuffer(4);
D3D11_MAPPED_SUBRESOURCE mappedResource;
HRESULT hr = d3dContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) {
ezUI::dbg("Failed to map vertex buffer! HRESULT: " + std::to_string(hr));
return;
}
memcpy(mappedResource.pData, vertices, sizeof(vertices));
d3dContext->Unmap(vertexBuffer, 0);
UINT stride = sizeof(Vertex);
UINT offset = 0;
d3dContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
d3dContext->Draw(4, 0);
}
void DX11Renderer::drawTriangle(float x1, float y1, float x2, float y2, float x3, float y3, const Color& color) {
if (!d3dDevice || !d3dContext) {
ezUI::dbg("Device or Context not initialized!");
return;
}
RECT rect;
GetClientRect(hwnd, &rect);
float nx1 = (2.0f * x1 / (rect.right - rect.left)) - 1.0f;
float ny1 = 1.0f - (2.0f * y1 / (rect.bottom - rect.top));
float nx2 = (2.0f * x2 / (rect.right - rect.left)) - 1.0f;
float ny2 = 1.0f - (2.0f * y2 / (rect.bottom - rect.top));
float nx3 = (2.0f * x3 / (rect.right - rect.left)) - 1.0f;
float ny3 = 1.0f - (2.0f * y3 / (rect.bottom - rect.top));
Vertex vertices[] = {
{ XMFLOAT3(nx1, ny1, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) },
{ XMFLOAT3(nx2, ny2, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) },
{ XMFLOAT3(nx3, ny3, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) }
};
createVertexBuffer(3);
D3D11_MAPPED_SUBRESOURCE mappedResource;
HRESULT hr = d3dContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) {
ezUI::dbg("Failed to map vertex buffer! HRESULT: " + std::to_string(hr));
return;
}
memcpy(mappedResource.pData, vertices, sizeof(vertices));
d3dContext->Unmap(vertexBuffer, 0);
UINT stride = sizeof(Vertex);
UINT offset = 0;
d3dContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dContext->Draw(3, 0);
}
void DX11Renderer::createShaders() {
const char* vsSource = R"(
struct VS_INPUT {
float3 position : POSITION;
float4 color : COLOR;
};
struct PS_INPUT {
float4 position : SV_POSITION;
float4 color : COLOR;
};
PS_INPUT main(VS_INPUT input) {
PS_INPUT output;
output.position = float4(input.position, 1.0);
output.color = input.color;
return output;
}
)";
ID3DBlob* vsBlob = nullptr;
ID3DBlob* errorBlob = nullptr;
HRESULT hr = D3DCompile(vsSource, strlen(vsSource), nullptr, nullptr, nullptr, "main", "vs_4_0", 0, 0, &vsBlob, &errorBlob);
if (FAILED(hr)) {
if (errorBlob) {
ezUI::dbg("Vertex Shader Compilation Error: " + std::string((char*)errorBlob->GetBufferPointer()));
errorBlob->Release();
}
return;
}
hr = d3dDevice->CreateVertexShader(vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), nullptr, &vertexShader);
if (FAILED(hr)) {
ezUI::dbg("Failed to create vertex shader! HRESULT: " + std::to_string(hr));
vsBlob->Release();
return;
}
d3dContext->VSSetShader(vertexShader, nullptr, 0);
D3D11_INPUT_ELEMENT_DESC layout[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
hr = d3dDevice->CreateInputLayout(layout, ARRAYSIZE(layout), vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), &inputLayout);
vsBlob->Release();
if (FAILED(hr)) {
ezUI::dbg("Failed to create input layout! HRESULT: " + std::to_string(hr));
return;
}
d3dContext->IASetInputLayout(inputLayout);
const char* psSource = R"(
struct PS_INPUT {
float4 position : SV_POSITION;
float4 color : COLOR;
};
float4 main(PS_INPUT input) : SV_TARGET {
return input.color;
}
)";
ID3DBlob* psBlob = nullptr;
hr = D3DCompile(psSource, strlen(psSource), nullptr, nullptr, nullptr, "main", "ps_4_0", 0, 0, &psBlob, &errorBlob);
if (FAILED(hr)) {
if (errorBlob) {
ezUI::dbg("Pixel Shader Compilation Error: " + std::string((char*)errorBlob->GetBufferPointer()));
errorBlob->Release();
}
return;
}
hr = d3dDevice->CreatePixelShader(psBlob->GetBufferPointer(), psBlob->GetBufferSize(), nullptr, &pixelShader);
psBlob->Release();
if (FAILED(hr)) {
ezUI::dbg("Failed to create pixel shader! HRESULT: " + std::to_string(hr));
return;
}
d3dContext->PSSetShader(pixelShader, nullptr, 0);
}
void DX11Renderer::drawCircle(float centerX, float centerY, float radius, const Color& color, int segments) {
if (!d3dDevice || !d3dContext) {
ezUI::dbg("Device or Context not initialized!");
return;
}
RECT rect;
GetClientRect(hwnd, &rect);
float screenWidth = static_cast<float>(rect.right - rect.left);
float screenHeight = static_cast<float>(rect.bottom - rect.top);
std::vector<Vertex> vertices;
std::vector<UINT> indices;
float xCenterNorm = (2.0f * centerX / screenWidth) - 1.0f;
float yCenterNorm = 1.0f - (2.0f * centerY / screenHeight);
vertices.push_back({ XMFLOAT3(xCenterNorm, yCenterNorm, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) });
for (int i = 0; i <= segments; ++i) {
float theta = (2.0f * XM_PI * i) / segments;
float x = centerX + radius * cosf(theta);
float y = centerY + radius * sinf(theta);
float xNorm = (2.0f * x / screenWidth) - 1.0f;
float yNorm = 1.0f - (2.0f * y / screenHeight);
vertices.push_back({ XMFLOAT3(xNorm, yNorm, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) });
}
for (int i = 1; i <= segments; ++i) {
indices.push_back(0);
indices.push_back(i);
indices.push_back(i + 1);
}
createVertexBuffer(vertices.size(), indices.size());
D3D11_MAPPED_SUBRESOURCE mappedResource;
HRESULT hr = d3dContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) {
ezUI::dbg("Failed to map vertex buffer! HRESULT: " + std::to_string(hr));
return;
}
memcpy(mappedResource.pData, vertices.data(), sizeof(Vertex) * vertices.size());
d3dContext->Unmap(vertexBuffer, 0);
hr = d3dContext->Map(indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) {
ezUI::dbg("Failed to map index buffer! HRESULT: " + std::to_string(hr));
return;
}
memcpy(mappedResource.pData, indices.data(), sizeof(UINT) * indices.size());
d3dContext->Unmap(indexBuffer, 0);
UINT stride = sizeof(Vertex);
UINT offset = 0;
d3dContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
d3dContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dContext->DrawIndexed(static_cast<UINT>(indices.size()), 0, 0);
}
void DX11Renderer::drawRoundedRectangle(float x, float y, float width, float height, float radius, const Color& color, int segments) {
if (!d3dDevice || !d3dContext) {
ezUI::dbg("Device or Context not initialized!");
return;
}
radius = min(radius, min(width, height) / 2.0f);
RECT rect;
GetClientRect(hwnd, &rect);
float screenWidth = static_cast<float>(rect.right - rect.left);
float screenHeight = static_cast<float>(rect.bottom - rect.top);
std::vector<Vertex> vertices;
std::vector<UINT> indices;
float xCenterNorm = (2.0f * (x + width / 2.0f) / screenWidth) - 1.0f;
float yCenterNorm = 1.0f - (2.0f * (y + height / 2.0f) / screenHeight);
vertices.push_back({ XMFLOAT3(xCenterNorm, yCenterNorm, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) });
float left = x + radius;
float right = x + width - radius;
float top = y + radius;
float bottom = y + height - radius;
int totalSegments = segments * 4;
for (int i = 0; i <= totalSegments; ++i) {
float theta = (XM_2PI * i) / totalSegments;
float cosTheta = cosf(theta);
float sinTheta = sinf(theta);
float cornerX = 0.0f;
float cornerY = 0.0f;
if (theta >= 0 && theta < XM_PIDIV2) {
cornerX = right;
cornerY = bottom;
}
else if (theta >= XM_PIDIV2 && theta < XM_PI) {
cornerX = left;
cornerY = bottom;
}
else if (theta >= XM_PI && theta < 3 * XM_PIDIV2) {
cornerX = left;
cornerY = top;
}
else {
cornerX = right;
cornerY = top;
}
float xPos = cornerX + radius * cosTheta;
float yPos = cornerY + radius * sinTheta;
float xNorm = (2.0f * xPos / screenWidth) - 1.0f;
float yNorm = 1.0f - (2.0f * yPos / screenHeight);
vertices.push_back({ XMFLOAT3(xNorm, yNorm, 0.0f), XMFLOAT4(color.r, color.g, color.b, color.a) });
}
for (int i = 1; i <= totalSegments; ++i) {
indices.push_back(0);
indices.push_back(i);
indices.push_back(i == totalSegments ? 1 : i + 1);
}
createVertexBuffer(vertices.size(), indices.size());
D3D11_MAPPED_SUBRESOURCE mappedResource;
HRESULT hr = d3dContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) {
ezUI::dbg("Failed to map vertex buffer! HRESULT: " + std::to_string(hr));
return;
}
memcpy(mappedResource.pData, vertices.data(), sizeof(Vertex) * vertices.size());
d3dContext->Unmap(vertexBuffer, 0);
hr = d3dContext->Map(indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(hr)) {
ezUI::dbg("Failed to map index buffer! HRESULT: " + std::to_string(hr));
return;
}
memcpy(mappedResource.pData, indices.data(), sizeof(UINT) * indices.size());
d3dContext->Unmap(indexBuffer, 0);
UINT stride = sizeof(Vertex);
UINT offset = 0;
d3dContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
d3dContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
d3dContext->DrawIndexed(static_cast<UINT>(indices.size()), 0, 0);
}
void DX11Renderer::draw(const DrawCommand& command) {
switch (command.type) {
case SHAPE_RECTANGLE:
if (command.shape.rectangle.rounding > 0.0f) {
drawRoundedRectangle(command.shape.rectangle.x, command.shape.rectangle.y, command.shape.rectangle.width, command.shape.rectangle.height, command.shape.rectangle.rounding, command.shape.rectangle.color, 32);
} else {
drawRectangle(command.shape.rectangle.x, command.shape.rectangle.y, command.shape.rectangle.width, command.shape.rectangle.height, command.shape.rectangle.color);
}
break;
case SHAPE_CIRCLE:
drawCircle(command.shape.circle.centerX, command.shape.circle.centerY, command.shape.circle.radius, command.shape.circle.color, command.shape.circle.segments);
break;
case SHAPE_TRIANGLE:
drawTriangle(command.shape.triangle.x1, command.shape.triangle.y1, command.shape.triangle.x2, command.shape.triangle.y2, command.shape.triangle.x3, command.shape.triangle.y3, command.shape.triangle.color);
break;
default:
ezUI::dbg("Invalid shape type!");
break;
}
}
void DX11Renderer::drawElement(const std::string& name) {
auto it = elements.find(name);
if (it != elements.end()) {
for (const auto& command : it->second.commands) {
draw(command);
}
}
}
void DX11Renderer::clearElements() {
elements.clear();
}
void DX11Renderer::registerElement(const std::string& name, int priority, const std::vector<DrawCommand>& commands) {
elements[name] = Element(name, priority, commands);
}
void DX11Renderer::drawAllElements() {
std::vector<Element> sortedElements;
for (const auto& pair : elements) {
sortedElements.push_back(pair.second);
}
std::sort(sortedElements.begin(), sortedElements.end(), [](const Element& a, const Element& b) {
return a.priority > b.priority;
});
for (const auto& element : sortedElements) {
for (const auto& command : element.commands) {
draw(command);
}
}
}
void DX11Renderer::setWindowClickThrough(bool enable) {
LONG_PTR exStyle = GetWindowLongPtr(hwnd, GWL_EXSTYLE);
if (enable) {
if (!(exStyle & WS_EX_TRANSPARENT)) {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TRANSPARENT);
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
}
else {
if (exStyle & WS_EX_TRANSPARENT) {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_TRANSPARENT);
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
}
}