-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextclass.cpp
More file actions
404 lines (318 loc) · 10.8 KB
/
Textclass.cpp
File metadata and controls
404 lines (318 loc) · 10.8 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
///////////////////////////////////////////////////////////////////////////////
// Filename: textclass.cpp
///////////////////////////////////////////////////////////////////////////////
#include "textclass.h"
TextClass::TextClass()
{
m_Font = 0;
m_FontShader = 0;
m_sentence1 = 0;
m_sentence2 = 0;
}
TextClass::TextClass(const TextClass& other)
{
}
TextClass::~TextClass()
{
}
bool TextClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, ColorShaderClass* shader, 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, deviceContext, "data\\fonts\\font2.fnt", "data\\fonts\\font2_0.tga");
if (!result)
{
MessageBox(hwnd, L"Could not initialize the font object.", L"Error", MB_OK);
return false;
}
// Create the font shader object. - passed in so dont need to initialise
//m_FontShader = new ColorShaderClass;
m_FontShader = shader;
if (!m_FontShader)
{
return false;
}
//m_FontShader->Initialize(device, hwnd); //make a brand new dedicated shader
// Initialize the first sentence.
result = InitializeSentence(&m_sentence1, 60, device);
if (!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence1, "Hello", 100, 100,m_screenWidth, m_screenHeight, 1.0f, 1.0f, 1.0f, deviceContext);
if (!result)
{
return false;
}
// Initialize the first sentence.
result = InitializeSentence(&m_sentence2, 16, device);
if (!result)
{
return false;
}
// Now update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence2, "Goodbye", 100, 200, m_screenWidth, m_screenHeight, 1.0f, 1.0f, 0.0f, deviceContext);
if (!result)
{
return false;
}
return true;
}
void TextClass::Shutdown()
{
// Release the first sentence.
ReleaseSentence(&m_sentence1);
// Release the second sentence.
ReleaseSentence(&m_sentence2);
// Release the font shader object. - dont delete, this is just a reference to it
if (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;
}
return true; //dont bother with the second sentence
// Draw the second sentence.
result = RenderSentence(deviceContext, m_sentence2, worldMatrix, orthoMatrix);
if (!result)
{
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;
}
//During the creation of the vertex buffer description for the sentence we set the Usage type to dynamic as we may want to change the contents of the sentence at any time.
// 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;
}
//The index buffer is setup as a normal static buffer since the contents will never need to change.
// 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;
}
TextClass::SentenceType* TextClass::getSentence(int i)
{
return m_sentence1;
}
bool TextClass::UpdateSentence(SentenceType* sentence, char* text, int positionX, int positionY, int width, int height, float red, float green, float blue,
ID3D11DeviceContext* deviceContext)
{
int numLetters;
VertexType* vertices;
float drawX, drawY;
HRESULT result;
D3D11_MAPPED_SUBRESOURCE mappedResource;
VertexType* verticesPtr;
//Set the color and size of the sentence.
// 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));
m_screenWidth = width;
m_screenHeight = height;
// 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);
// 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);
//Copy the vertex array information into the sentence vertex buffer.
// 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. (lit from the front to make simple)
result = m_FontShader->RenderTexture(deviceContext, sentence->indexCount, worldMatrix, m_baseViewMatrix, orthoMatrix, m_Font->GetTexture(), XMFLOAT3(0.f, 0.f, -100.f), COLOUR_WHITE, COLOUR_WHITE);
if (!result)
{
return false;
}
return true;
}
bool TextClass::SetBuffers(ID3D11DeviceContext* deviceContext, SentenceType* sentence)
{
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);
return true;
}