-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextclass.cpp
More file actions
546 lines (420 loc) · 12.4 KB
/
Copy pathtextclass.cpp
File metadata and controls
546 lines (420 loc) · 12.4 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
///////////////////////////////////////////////////////////////////////////////
// Filename: textclass.cpp
///////////////////////////////////////////////////////////////////////////////
#include "textclass.h"
TextClass::TextClass()
{
m_Font = 0;
m_FontShader = 0;
m_sentence1 = 0;
m_sentence2 = 0;
m_sentence3 = 0;
m_sentence4 = 0;
polygonNum = 1;
fpsNum = 0;
cpuUsage = 0;
}
TextClass::TextClass(const TextClass& other)
{
}
TextClass::~TextClass()
{
}
void TextClass::getpolygonnum(int vertexNum, int modelNum)
{
polygonNum = (vertexNum / 3) * modelNum;
}
const char* TextClass::polygunnumtochar()
{
static char buffer[50];
// itoa를 사용하여 정수를 문자열로 변환
_itoa_s(polygonNum, buffer, 10); // 10은 진수(base)
// const char*로 사용
const char* resultStr = buffer;
return resultStr;
}
void TextClass::getmodelnum(int modelnum)
{
this->modelNum = modelnum;
}
const char* TextClass::modelnumtochar()
{
static char buffer[50];
// itoa를 사용하여 정수를 문자열로 변환
_itoa_s(modelNum, buffer, 10); // 10은 진수(base)
// const char*로 사용
const char* resultStr = buffer;
return resultStr;
}
void TextClass::getFps(int fps)
{
fpsNum = fps;
}
void TextClass::getCPUUsage(int cpuusage)
{
cpuUsage = cpuusage;
}
std::string TextClass::ScreenResolution(int screenWidth, int screenHeight)
{
std::string width = to_string(screenWidth);
std::string height = to_string(screenHeight);
std::string resolution = "Screen Resolution: " + width + "X" + height;
return resolution;
}
bool TextClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, HWND hwnd,
int screenWidth, int screenHeight, XMMATRIX baseViewMatrix)
{
bool result;
// Store the screen width and height.
m_screenWidth = screenWidth;
m_screenHeight = screenHeight;
// Store the base view matrix.
m_baseViewMatrix = baseViewMatrix;
// Create the font object.
m_Font = new FontClass;
if(!m_Font)
{
return false;
}
// Initialize the font object.
result = m_Font->Initialize(device, L"./data/fontdata.txt", L"./data/font.dds");
if(!result)
{
MessageBox(hwnd, L"Could not initialize the font object.", L"Error", MB_OK);
return false;
}
// Create the font shader object.
m_FontShader = new FontShaderClass;
if(!m_FontShader)
{
return false;
}
// Initialize the font shader object.
result = m_FontShader->Initialize(device, hwnd);
if(!result)
{
MessageBox(hwnd, L"Could not initialize the font shader object.", L"Error", MB_OK);
return false;
}
// Initialize the first sentence.
result = InitializeSentence(&m_sentence1, 32, device);
if(!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence1, modelnumtochar(), 50, 50, 1.0f, 1.0f, 1.0f, deviceContext);
if(!result)
{
return false;
}
// Initialize the first sentence.
result = InitializeSentence(&m_sentence2, 32, device);
if(!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence2, polygunnumtochar(), 50, 70, 1.0f, 1.0f, 1.0f, deviceContext);
if(!result)
{
MessageBox(hwnd, L"polygon sentence error", L"Error", MB_OK);
return false;
}
result = InitializeSentence(&m_sentence3, 32, device);
if (!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence3, "", 50, 90, 1.0f, 1.0f, 1.0f, deviceContext);
if (!result)
{
MessageBox(hwnd, L"polygon sentence error", L"Error", MB_OK);
return false;
}
result = InitializeSentence(&m_sentence4, 32, device);
if (!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence4, "CPU Usage", 50, 110, 1.0f, 1.0f, 1.0f, deviceContext);
if (!result)
{
MessageBox(hwnd, L"CPU Usage sentence error", L"Error", MB_OK);
return false;
}
result = InitializeSentence(&m_sentence5, 32, device);
if (!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence5, ScreenResolution(m_screenWidth,m_screenHeight).c_str(), 50, 130, 1.0f, 1.0f, 1.0f, deviceContext);
if (!result)
{
MessageBox(hwnd, L"Screen Resolution sentence error", L"Error", MB_OK);
return false;
}
return true;
}
void TextClass::Shutdown()
{
// Release the first sentence.
ReleaseSentence(&m_sentence1);
ReleaseSentence(&m_sentence2);
ReleaseSentence(&m_sentence3);
ReleaseSentence(&m_sentence4);
ReleaseSentence(&m_sentence5);
// Release the font shader object.
if(m_FontShader)
{
m_FontShader->Shutdown();
delete m_FontShader;
m_FontShader = 0;
}
// Release the font object.
if(m_Font)
{
m_Font->Shutdown();
delete m_Font;
m_Font = 0;
}
return;
}
bool TextClass::Render(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX orthoMatrix)
{
bool result;
// Draw the first sentence.
result = RenderSentence(deviceContext, m_sentence1, worldMatrix, orthoMatrix);
if(!result)
{
return false;
}
// Draw the second sentence.
result = RenderSentence(deviceContext, m_sentence2, worldMatrix, orthoMatrix);
if(!result)
{
return false;
}
result = RenderSentence(deviceContext, m_sentence3, worldMatrix, orthoMatrix);
if (!result)
{
return false;
}
result = RenderSentence(deviceContext, m_sentence4, worldMatrix, orthoMatrix);
if (!result)
{
return false;
}
result = RenderSentence(deviceContext, m_sentence5, worldMatrix, orthoMatrix);
if (!result)
{
return false;
}
return true;
}
bool TextClass::UpdateSentenceCPUFPS(ID3D11DeviceContext* deviceContext)
{
bool result;
std::string fpsString = "FPS: " + to_string(fpsNum);
std::string CPUUsage = "CPU Usage: " + to_string(cpuUsage) + "%";
// FPS 텍스트를 업데이트
result = UpdateSentence(m_sentence3, fpsString.c_str(), 50, 90, 1.0f, 1.0f, 1.0f, deviceContext);
if (!result)
{
return false;
}
result = UpdateSentence(m_sentence4, CPUUsage.c_str(), 50, 110, 1.0f, 1.0f, 1.0f, deviceContext);
if (!result)
{
MessageBox(NULL, L"UpdateSentence CPU part error", L"Error", MB_OK);
return false;
}
return true;
}
bool TextClass::InitializeSentence(SentenceType** sentence, int maxLength, ID3D11Device* device)
{
VertexType* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
int i;
// Create a new sentence object.
*sentence = new SentenceType;
if(!*sentence)
{
return false;
}
// Initialize the sentence buffers to null.
(*sentence)->vertexBuffer = 0;
(*sentence)->indexBuffer = 0;
// Set the maximum length of the sentence.
(*sentence)->maxLength = maxLength;
// Set the number of vertices in the vertex array.
(*sentence)->vertexCount = 6 * maxLength;
// Set the number of indexes in the index array.
(*sentence)->indexCount = (*sentence)->vertexCount;
// Create the vertex array.
vertices = new VertexType[(*sentence)->vertexCount];
if(!vertices)
{
return false;
}
// Create the index array.
indices = new unsigned long[(*sentence)->indexCount];
if(!indices)
{
return false;
}
// Initialize vertex array to zeros at first.
memset(vertices, 0, (sizeof(VertexType) * (*sentence)->vertexCount));
// Initialize the index array.
for(i=0; i<(*sentence)->indexCount; i++)
{
indices[i] = i;
}
// Set up the description of the dynamic vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * (*sentence)->vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &(*sentence)->vertexBuffer);
if(FAILED(result))
{
return false;
}
// Set up the description of the static index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * (*sentence)->indexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the index data.
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// Create the index buffer.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &(*sentence)->indexBuffer);
if(FAILED(result))
{
return false;
}
// Release the vertex array as it is no longer needed.
delete [] vertices;
vertices = 0;
// Release the index array as it is no longer needed.
delete [] indices;
indices = 0;
return true;
}
bool TextClass::UpdateSentence(SentenceType* sentence, const char* text, int positionX, int positionY,
float red, float green, float blue, ID3D11DeviceContext* deviceContext)
{
int numLetters;
VertexType* vertices;
float drawX, drawY;
HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
VertexType* verticesPtr;
// Store the color of the sentence.
sentence->red = red;
sentence->green = green;
sentence->blue = blue;
// Get the number of letters in the sentence.
numLetters = (int)strlen(text);
// Check for possible buffer overflow.
if(numLetters > sentence->maxLength)
{
return false;
}
// Create the vertex array.
vertices = new VertexType[sentence->vertexCount];
if(!vertices)
{
return false;
}
// Initialize vertex array to zeros at first.
memset(vertices, 0, (sizeof(VertexType) * sentence->vertexCount));
//// Calculate the X and Y pixel position on the screen to start drawing to.
drawX = (float)(((m_screenWidth / 2) * -1) + positionX);
drawY = (float)((m_screenHeight / 2) - positionY);
/*drawX = (float)positionX;
drawY = (float)positionY;*/
// Use the font class to build the vertex array from the sentence text and sentence draw location.
m_Font->BuildVertexArray((void*)vertices, text, drawX, drawY);
// Lock the vertex buffer so it can be written to.
result = deviceContext->Map(sentence->vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if(FAILED(result))
{
return false;
}
// Get a pointer to the data in the vertex buffer.
verticesPtr = (VertexType*)mappedResource.pData;
// Copy the data into the vertex buffer.
memcpy(verticesPtr, (void*)vertices, (sizeof(VertexType) * sentence->vertexCount));
// Unlock the vertex buffer.
deviceContext->Unmap(sentence->vertexBuffer, 0);
// Release the vertex array as it is no longer needed.
delete [] vertices;
vertices = 0;
return true;
}
void TextClass::ReleaseSentence(SentenceType** sentence)
{
if(*sentence)
{
// Release the sentence vertex buffer.
if((*sentence)->vertexBuffer)
{
(*sentence)->vertexBuffer->Release();
(*sentence)->vertexBuffer = 0;
}
// Release the sentence index buffer.
if((*sentence)->indexBuffer)
{
(*sentence)->indexBuffer->Release();
(*sentence)->indexBuffer = 0;
}
// Release the sentence.
delete *sentence;
*sentence = 0;
}
return;
}
bool TextClass::RenderSentence(ID3D11DeviceContext* deviceContext, SentenceType* sentence, XMMATRIX worldMatrix,
XMMATRIX orthoMatrix)
{
unsigned int stride, offset;
XMFLOAT4 pixelColor;
bool result;
// Set vertex buffer stride and offset.
stride = sizeof(VertexType);
offset = 0;
// Set the vertex buffer to active in the input assembler so it can be rendered.
deviceContext->IASetVertexBuffers(0, 1, &sentence->vertexBuffer, &stride, &offset);
// Set the index buffer to active in the input assembler so it can be rendered.
deviceContext->IASetIndexBuffer(sentence->indexBuffer, DXGI_FORMAT_R32_UINT, 0);
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Create a pixel color vector with the input sentence color.
pixelColor = XMFLOAT4(sentence->red, sentence->green, sentence->blue, 1.0f);
// Render the text using the font shader.
result = m_FontShader->Render(deviceContext, sentence->indexCount, worldMatrix, m_baseViewMatrix,
orthoMatrix, m_Font->GetTexture(), pixelColor);
if(!result)
{
false;
}
return true;
}