Khaotic Engine Reborn
Loading...
Searching...
No Matches
text_class.cpp
1#include "text_class.h"
2
3
4text_class::text_class()
5{
6 m_vertexBuffer = 0;
7 m_indexBuffer = 0;
8}
9
10
11text_class::text_class(const text_class& other)
12{
13}
14
15
16text_class::~text_class()
17{
18}
19
20bool text_class::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, int screenWidth, int screenHeight, int maxLength, font_class* Font, char* text,
21 int positionX, int positionY, float red, float green, float blue)
22{
23 bool result;
24
25
26 // Store the screen width and height.
27 m_screenWidth = screenWidth;
28 m_screenHeight = screenHeight;
29
30 // Store the maximum length of the sentence.
31 m_maxLength = maxLength;
32
33 // Initalize the sentence.
34 result = InitializeBuffers(device, deviceContext, Font, text, positionX, positionY, red, green, blue);
35 if (!result)
36 {
37 return false;
38 }
39
40 return true;
41}
42
43void text_class::Shutdown()
44{
45 // Release the vertex and index buffers.
46 ShutdownBuffers();
47
48 return;
49}
50
51void text_class::Render(ID3D11DeviceContext* deviceContext)
52{
53 // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
54 RenderBuffers(deviceContext);
55
56 return;
57}
58
59int text_class::GetIndexCount()
60{
61 return m_indexCount;
62}
63
64bool text_class::InitializeBuffers(ID3D11Device* device, ID3D11DeviceContext* deviceContext, font_class* Font, char* text, int positionX, int positionY, float red, float green, float blue)
65{
66 VertexType* vertices;
67 unsigned long* indices;
68 D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
69 D3D11_SUBRESOURCE_DATA vertexData, indexData;
70 HRESULT result;
71 int i;
72
73 // Set the vertex and index count.
74 m_vertexCount = 6 * m_maxLength;
75 m_indexCount = m_vertexCount;
76
77 // Create the vertex array.
78 vertices = new VertexType[m_vertexCount];
79
80 // Create the index array.
81 indices = new unsigned long[m_indexCount];
82
83 // Initialize vertex array to zeros at first.
84 memset(vertices, 0, (sizeof(VertexType) * m_vertexCount));
85
86 // Initialize the index array.
87 for (i = 0; i < m_indexCount; i++)
88 {
89 indices[i] = i;
90 }
91
92 // Set up the description of the dynamic vertex buffer.
93 vertexBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
94 vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
95 vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
96 vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
97 vertexBufferDesc.MiscFlags = 0;
98 vertexBufferDesc.StructureByteStride = 0;
99
100 // Give the subresource structure a pointer to the vertex data.
101 vertexData.pSysMem = vertices;
102 vertexData.SysMemPitch = 0;
103 vertexData.SysMemSlicePitch = 0;
104
105 // Create the vertex buffer.
106 result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
107 if (FAILED(result))
108 {
109 return false;
110 }
111
112 // Set up the description of the static index buffer.
113 indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
114 indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
115 indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
116 indexBufferDesc.CPUAccessFlags = 0;
117 indexBufferDesc.MiscFlags = 0;
118 indexBufferDesc.StructureByteStride = 0;
119
120 // Give the subresource structure a pointer to the index data.
121 indexData.pSysMem = indices;
122 indexData.SysMemPitch = 0;
123 indexData.SysMemSlicePitch = 0;
124
125 // Create the index buffer.
126 result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
127 if (FAILED(result))
128 {
129 return false;
130 }
131
132 // Release the vertex array as it is no longer needed.
133 delete[] vertices;
134 vertices = 0;
135
136 // Release the index array as it is no longer needed.
137 delete[] indices;
138 indices = 0;
139
140 // Now add the text data to the sentence buffers.
141 result = UpdateText(deviceContext, Font, text, positionX, positionY, red, green, blue);
142 if (!result)
143 {
144 return false;
145 }
146
147 return true;
148}
149
150void text_class::ShutdownBuffers()
151{
152 // Release the index buffer.
153 if (m_indexBuffer)
154 {
155 m_indexBuffer->Release();
156 m_indexBuffer = 0;
157 }
158
159 // Release the vertex buffer.
160 if (m_vertexBuffer)
161 {
162 m_vertexBuffer->Release();
163 m_vertexBuffer = 0;
164 }
165
166 return;
167}
168
169bool text_class::UpdateText(ID3D11DeviceContext* deviceContext, font_class* Font, char* text, int positionX, int positionY, float red, float green, float blue)
170{
171 int numLetters;
172 VertexType* vertices;
173 float drawX, drawY;
174 HRESULT result;
175 D3D11_MAPPED_SUBRESOURCE mappedResource;
176 VertexType* verticesPtr;
177
178 // Store the color of the sentence.
179 m_pixelColor = XMFLOAT4(red, green, blue, 1.0f);
180
181 // Get the number of letters in the sentence.
182 numLetters = (int)strlen(text);
183
184 // Check for possible buffer overflow.
185 if (numLetters > m_maxLength)
186 {
187 return false;
188 }
189
190 // Create the vertex array.
191 vertices = new VertexType[m_vertexCount];
192
193 // Initialize vertex array to zeros at first.
194 memset(vertices, 0, (sizeof(VertexType) * m_vertexCount));
195
196 // Calculate the X and Y pixel position on the screen to start drawing to.
197 drawX = (float)(((m_screenWidth / 2) * -1) + positionX);
198 drawY = (float)((m_screenHeight / 2) - positionY);
199
200 // Use the font class to build the vertex array from the sentence text and sentence draw location.
201 Font->BuildVertexArray((void*)vertices, text, drawX, drawY);
202
203 // Lock the vertex buffer so it can be written to.
204 result = deviceContext->Map(m_vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
205 if (FAILED(result))
206 {
207 return false;
208 }
209
210 // Get a pointer to the data in the vertex buffer.
211 verticesPtr = (VertexType*)mappedResource.pData;
212
213 // Copy the data into the vertex buffer.
214 memcpy(verticesPtr, (void*)vertices, (sizeof(VertexType) * m_vertexCount));
215
216 // Unlock the vertex buffer.
217 deviceContext->Unmap(m_vertexBuffer, 0);
218
219 // Release the vertex array as it is no longer needed.
220 delete[] vertices;
221 vertices = 0;
222
223 return true;
224}
225
226void text_class::RenderBuffers(ID3D11DeviceContext* deviceContext)
227{
228 unsigned int stride, offset;
229
230
231 // Set vertex buffer stride and offset.
232 stride = sizeof(VertexType);
233 offset = 0;
234
235 // Set the vertex buffer to active in the input assembler so it can be rendered.
236 deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
237
238 // Set the index buffer to active in the input assembler so it can be rendered.
239 deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
240
241 // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
242 deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
243
244 return;
245}
246
247XMFLOAT4 text_class::GetPixelColor()
248{
249 return m_pixelColor;
250}