-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRenderer.cpp
371 lines (272 loc) · 10.6 KB
/
Renderer.cpp
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
#include "Renderer.h"
Renderer* g_pRenderer = new Renderer();
HRESULT Renderer::Initalize(ID3D11Device* pDevice, ID3D11DeviceContext* pDeviceContext, ID3D11Buffer* pVertexBuffer)
{
if (!pDevice || !pDeviceContext || !pVertexBuffer)
return S_FALSE;
m_pDevice = pDevice;
m_pDeviceContext = pDeviceContext;
m_pVertexBuffer = pVertexBuffer;
m_pDeviceContext->IAGetInputLayout(&this->m_pInputLayout);
return S_OK;
}
HRESULT Renderer::Initalize(ID3D11Device* pDevice, ID3D11DeviceContext* pDeviceContext) // this needs more shit
{
HRESULT hr;
D3D11_BUFFER_DESC Desc;
D3D11_MAPPED_SUBRESOURCE Mapped;
if (!pDevice || !pDeviceContext)
return E_INVALIDARG;
m_pDevice = pDevice;
m_pDeviceContext = pDeviceContext;
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, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
ID3DBlob* pVSBlob = NULL;
ID3DBlob* pPSBlob = NULL;
if (FAILED(hr = GeneratePixelShader(&m_pPixelShader, &pPSBlob, g_szShader)))
return hr;
if (FAILED(hr = GenerateVertexShader(&m_pVertexShader, &pVSBlob, g_szShader)))
return hr;
if (FAILED(hr = m_pDevice->CreateInputLayout(layout, _ARRAYSIZE(layout), pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), &this->m_pInputLayout)))
return hr;
if (pVSBlob)
{
pVSBlob->Release();
pVSBlob = NULL;
}
if (pPSBlob)
{
pPSBlob->Release();
pPSBlob = NULL;
}
ZeroMemory(&Desc, sizeof(D3D11_BUFFER_DESC));
Desc.Usage = D3D11_USAGE_DYNAMIC;
Desc.ByteWidth = m_RenderList.m_uMaxVertices * sizeof(Vertex);
Desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
Desc.MiscFlags = 0;
if (FAILED(hr = m_pDevice->CreateBuffer(&Desc, NULL, &m_pVertexBuffer)))
return hr;
ZeroMemory(&Desc, sizeof(D3D11_BUFFER_DESC));
Desc.Usage = D3D11_USAGE_DYNAMIC;
Desc.ByteWidth = sizeof(XMMATRIX);
Desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
Desc.MiscFlags = 0;
if (FAILED(hr = m_pDevice->CreateBuffer(&Desc, NULL, &m_pViewMatrix)))
return hr;
m_ViewMatrix = DirectX::XMMatrixOrthographicOffCenterLH(0.0f, (float)g_pGraphicDevice->m_iScreenWidth, (float)g_pGraphicDevice->m_iScreenHeight, 0.0f, -100.0f, 100.0f);
if (FAILED(hr = m_pDeviceContext->Map(m_pViewMatrix, 0, D3D11_MAP_WRITE_DISCARD, 0, &Mapped)))
return hr;
memcpy(Mapped.pData, &m_ViewMatrix, sizeof(XMMATRIX));
m_pDeviceContext->Unmap(m_pViewMatrix, 0);
return S_OK;
}
template<size_t N>
HRESULT Renderer::GeneratePixelShader(ID3D11PixelShader** pShader, ID3DBlob** ppBlob, const char (&szShader)[N])
{
ID3DBlob* pD3DErrorMsgBlob;
HRESULT hr;
if (!m_pDevice)
return E_FAIL;
if (FAILED(hr = D3DCompile(szShader, N, NULL, NULL, NULL, "PS", "ps_4_0", 0, 0, ppBlob, &pD3DErrorMsgBlob)))
return hr;
if (FAILED(hr = m_pDevice->CreatePixelShader((*ppBlob)->GetBufferPointer(), (*ppBlob)->GetBufferSize(), NULL, pShader)))
return hr;
return S_OK;
}
HRESULT Renderer::GeneratePixelShader(ID3D11PixelShader** pShader, ID3DBlob** ppBlob, const char* szShader, size_t cchSource)
{
ID3DBlob* pD3DErrorMsgBlob;
HRESULT hr;
if (!m_pDevice)
return E_FAIL;
if (FAILED(hr = D3DCompile(szShader, cchSource, NULL, NULL, NULL, "PS", "ps_4_0", 0, 0, ppBlob, &pD3DErrorMsgBlob)))
return hr;
if (FAILED(hr = m_pDevice->CreatePixelShader((*ppBlob)->GetBufferPointer(), (*ppBlob)->GetBufferSize(), NULL, pShader)))
return hr;
return S_OK;
}
template<size_t N>
HRESULT Renderer::GenerateVertexShader(ID3D11VertexShader** pShader, ID3DBlob** ppBlob, const char (&szShader)[N])
{
ID3DBlob* pD3DErrorMsgBlob;
HRESULT hr;
if (!m_pDevice)
return E_FAIL;
if (FAILED(hr = D3DCompile(szShader, N, NULL, NULL, NULL, "VS", "vs_4_0", 0, 0, ppBlob, &pD3DErrorMsgBlob)))
return hr;
if (FAILED(hr = m_pDevice->CreateVertexShader((*ppBlob)->GetBufferPointer(), (*ppBlob)->GetBufferSize(), NULL, pShader)))
return hr;
return S_OK;
}
HRESULT Renderer::GenerateVertexShader(ID3D11VertexShader** pShader, ID3DBlob** ppBlob, const char* szShader, size_t cchSource)
{
ID3DBlob* pD3DErrorMsgBlob;
HRESULT hr;
if (!m_pDevice)
return E_FAIL;
if (FAILED(hr = D3DCompile(szShader, cchSource, NULL, NULL, NULL, "VS", "vs_4_0", 0, 0, ppBlob, &pD3DErrorMsgBlob)))
return hr;
if (FAILED(hr = m_pDevice->CreateVertexShader((*ppBlob)->GetBufferPointer(), (*ppBlob)->GetBufferSize(), NULL, pShader)))
return hr;
return S_OK;
}
HRESULT Renderer::SaveState()
{
if (!m_pDeviceContext)
return E_FAIL;
m_pDeviceContext->IAGetInputLayout(&m_pOriginalInputLayout);
m_pDeviceContext->VSGetShader(&m_pOriginalVertexShader, NULL, 0);
m_pDeviceContext->PSGetShader(&m_pOriginalPixelShader, NULL, 0);
return S_OK;
}
HRESULT Renderer::RestoreState()
{
if (!m_pDeviceContext || !m_pOriginalInputLayout)
return E_FAIL;
m_pDeviceContext->IASetInputLayout(m_pOriginalInputLayout);
m_pDeviceContext->VSSetShader(m_pOriginalVertexShader, NULL, 0);
m_pDeviceContext->PSSetShader(m_pOriginalPixelShader, NULL, 0);
return S_OK;
}
HRESULT Renderer::Begin()
{
HRESULT hr = SaveState();
m_pDeviceContext->PSSetShader(m_pPixelShader, NULL, 0);
m_pDeviceContext->VSSetShader(m_pVertexShader, NULL, 0);
m_pDeviceContext->VSSetConstantBuffers(0, 1, &m_pViewMatrix);
m_pDeviceContext->IASetInputLayout(m_pInputLayout);
UINT stride = sizeof(Vertex);
UINT offset = 0;
m_pDeviceContext->IASetVertexBuffers(0, 1, &m_pVertexBuffer, &stride, &offset);
return hr;
}
HRESULT Renderer::End()
{
m_RenderList.clear();
return RestoreState();
}
void Renderer::Draw()
{
return Draw(m_RenderList);
}
void Renderer::Draw(const RenderList& list)
{
D3D11_MAPPED_SUBRESOURCE mapped;
if (list.m_vertices.empty())
return;
if (FAILED(this->m_pDeviceContext->Map(this->m_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
return;
memcpy(mapped.pData, list.m_vertices.data(), sizeof(Vertex) * list.m_vertices.size());
this->m_pDeviceContext->Unmap(this->m_pVertexBuffer, 0);
UINT pos = 0;
for (const auto& batch : list.m_batches)
{
this->m_pDeviceContext->IASetPrimitiveTopology(batch.topology);
this->m_pDeviceContext->Draw(batch.count, pos);
pos += batch.count;
}
}
void Renderer::DrawPixel(int x, int y, const Color& color)
{
DrawFilledRect(x, y, 1, 1, color);
}
void Renderer::DrawLine(int x0, int y0, int x1, int y1, const Color& color)
{
float rgba[4];
color.GetFloatColor(rgba);
Vertex v[]
{
{ XMFLOAT4((float)x0, (float)y0, 0.0f, 1.0f), XMFLOAT4(rgba) },
{ XMFLOAT4((float)x1, (float)y1, 0.0f, 1.0f), XMFLOAT4(rgba) }
};
m_RenderList.AddVertices(v, D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
}
void Renderer::DrawFilledRect(int x, int y, int width, int height, const Color& color)
{
float rgba[4];
color.GetFloatColor(rgba);
Vertex v[]
{
{ XMFLOAT4((float)x, (float)y, 0.f, 1.f), XMFLOAT4(rgba) },
{ XMFLOAT4((float)(x + width), (float)y, 0.f, 1.f), XMFLOAT4(rgba) },
{ XMFLOAT4((float)x, (float)(y + height), 0.f, 1.f), XMFLOAT4(rgba) },
{ XMFLOAT4((float)(x + width), (float)y, 0.f, 1.f), XMFLOAT4(rgba) },
{ XMFLOAT4((float)(x + width), (float)(y + height), 0.f, 1.f), XMFLOAT4(rgba) },
{ XMFLOAT4((float)x, (float)(y + height), 0.f, 1.f), XMFLOAT4(rgba) }
};
m_RenderList.AddVertices(v, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}
void Renderer::DrawFilledRect(const RenderList& list, const Vector4& rect, const Color& color)
{
//Vertex v[]
//{
// { rect.x, rect.y, 0.f, color },
// { rect.x + rect.z, rect.y, 0.f, color },
// { rect.x, rect.y + rect.w, 0.f, color },
// { rect.x + rect.z, rect.y, 0.f, color },
// { rect.x + rect.z, rect.y + rect.w, 0.f, color },
// { rect.x, rect.y + rect.w, 0.f, color }
//};
//m_RenderList.AddVertices(v, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
}
void Renderer::DrawRectOutline(int x, int y, int w, int h, D3DCOLOR color)
{
Vertex vertices[4];
BuildVertex(XMFLOAT4((FLOAT)x, (FLOAT)(y + h), 0, 1), color, vertices, 0); //near up left
BuildVertex(XMFLOAT4((FLOAT)x, (FLOAT)y, 0, 1), color, vertices, 1); //near left
BuildVertex(XMFLOAT4((FLOAT)(x + w), (FLOAT)(y + h), 0, 1), color, vertices, 2); //far up right
BuildVertex(XMFLOAT4((FLOAT)(x + w), (FLOAT)y, 0, 1), color, vertices, 3); //far right
m_RenderList.AddVertices(vertices, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
//D3D11_MAPPED_SUBRESOURCE mapped;
//if (FAILED(this->m_pDeviceContext->Map(this->m_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
// return;
//Vertex* pVertices = (Vertex*)mapped.pData;
//BuildVertex(XMFLOAT4((FLOAT)x, (FLOAT)(y + h), 0, 1), color, pVertices, 0); //near up left
//BuildVertex(XMFLOAT4((FLOAT)x, (FLOAT)y, 0, 1), color, pVertices, 1); //near left
//BuildVertex(XMFLOAT4((FLOAT)(x + w), (FLOAT)(y + h), 0, 1), color, pVertices, 2); //far up right
//BuildVertex(XMFLOAT4((FLOAT)(x + w), (FLOAT)y, 0, 1), color, pVertices, 3); //far right
//this->m_pDeviceContext->Unmap(this->m_pVertexBuffer, 0);
//UINT stride = sizeof(Vertex);
//UINT offset = 0;
//this->m_pDeviceContext->IASetVertexBuffers(0, 1, &this->m_pVertexBuffer, &stride, &offset);
//this->m_pDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
//this->m_pDeviceContext->IASetInputLayout(this->m_pInputLayout);
//this->m_pDeviceContext->Draw(4, 0);
}
void Renderer::DrawRectBordered(int x, int y, int width, int height, int thickness, const Color& color)
{
DrawFilledRect(x, y, width, thickness, color);
DrawFilledRect(x, y, thickness, height, color);
DrawFilledRect(x + width, y, thickness, height, color);
DrawFilledRect(x, y + height, width + thickness, thickness, color);
}
void Renderer::DrawRectCorners(int x, int y, int w, int h, int thickness, const Color& color)
{
int v_leg = h / 4;
int h_leg = v_leg / 2;
//top near
DrawFilledRect(x, y, h_leg, thickness, color); //h
DrawFilledRect(x, y, thickness, v_leg, color); //v
//top far
DrawFilledRect((x + w) - h_leg, y, h_leg, thickness, color); //h
DrawFilledRect(x + w, y, thickness, v_leg, color); //v
//btm near
DrawFilledRect(x, y + h, h_leg, thickness, color); // h
DrawFilledRect(x, (y + h) - v_leg, thickness, v_leg, color); //v
//btm far
DrawFilledRect((x + w) - (h_leg - thickness), y + h, h_leg, thickness, color); //h
DrawFilledRect(x + w, (y + h) - v_leg, thickness, v_leg, color); //v
}
void Renderer::DrawHealthBar(int x, int y, int height, int health, int max_health)
{
int R = 255 - (int)(health * (255.f / max_health));
int G = (int)(health * (255.f / max_health));
health = (height - ((height * health) / max_health));
DrawFilledRect(x - 4, y + health, 2, height - health + 1, Color(R, G, 0));
DrawRectBordered(x - 5, y - 1, 3, height + 2, 1, Color::Black());
}