Khaotic Engine Reborn
Loading...
Searching...
No Matches
display_plane_class.cpp
1#include "display_plane_class.h"
2
3
4display_plane_class::display_plane_class()
5{
6 m_vertexBuffer = 0;
7 m_indexBuffer = 0;
8}
9
10
11display_plane_class::display_plane_class(const display_plane_class& other)
12{
13}
14
15
16display_plane_class::~display_plane_class()
17{
18}
19
20bool display_plane_class::Initialize(ID3D11Device* device, float width, float height)
21{
22 Logger::Get().Log("Initializing display_plane_class, width: " + std::to_string(width) + ", height: " + std::to_string(height), __FILE__, __LINE__, Logger::LogLevel::Initialize);
23 bool result;
24
25
26 // initialize the vertex and index buffer that hold the geometry for the button.
27 result = InitializeBuffers(device, width, height);
28 if (!result)
29 {
30 Logger::Get().Log("Could not initialize buffers", __FILE__, __LINE__, Logger::LogLevel::Error);
31 return false;
32 }
33
34 return true;
35}
36
37
38void display_plane_class::Shutdown()
39{
40 // Release the vertex and index buffers.
41 ShutdownBuffers();
42
43 return;
44}
45
46
47void display_plane_class::Render(ID3D11DeviceContext* deviceContext)
48{
49 // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
50 RenderBuffers(deviceContext);
51
52 return;
53}
54
55
56int display_plane_class::GetIndexCount()
57{
58 return m_indexCount;
59}
60
61bool display_plane_class::InitializeBuffers(ID3D11Device* device, float width, float height)
62{
63 Logger::Get().Log("Initializing buffers", __FILE__, __LINE__, Logger::LogLevel::Initialize);
64
65 VertexType* vertices;
66 unsigned long* indices;
67 D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
68 D3D11_SUBRESOURCE_DATA vertexData, indexData;
69 HRESULT result;
70 int i;
71
72
73 // Set the number of vertices in the vertex array.
74 m_vertexCount = 6;
75
76 // Set the number of indices in the index array.
77 m_indexCount = m_vertexCount;
78
79 // Create the vertex array.
80 vertices = new VertexType[m_vertexCount];
81
82 // Create the index array.
83 indices = new unsigned long[m_indexCount];
84
85 // Load the vertex array with data.
86 // First triangle.
87 vertices[0].position = XMFLOAT3(-width, height, 0.0f); // Top left.
88 vertices[0].texture = XMFLOAT2(0.0f, 0.0f);
89
90 vertices[1].position = XMFLOAT3(width, -height, 0.0f); // Bottom right.
91 vertices[1].texture = XMFLOAT2(1.0f, 1.0f);
92
93 vertices[2].position = XMFLOAT3(-width, -height, 0.0f); // Bottom left.
94 vertices[2].texture = XMFLOAT2(0.0f, 1.0f);
95
96 // Second triangle.
97 vertices[3].position = XMFLOAT3(-width, height, 0.0f); // Top left.
98 vertices[3].texture = XMFLOAT2(0.0f, 0.0f);
99
100 vertices[4].position = XMFLOAT3(width, height, 0.0f); // Top right.
101 vertices[4].texture = XMFLOAT2(1.0f, 0.0f);
102
103 vertices[5].position = XMFLOAT3(width, -height, 0.0f); // Bottom right.
104 vertices[5].texture = XMFLOAT2(1.0f, 1.0f);
105
106 // Load the index array with data.
107 for (i = 0; i < m_indexCount; i++)
108 {
109 indices[i] = i;
110 }
111
112 // Set up the description of the vertex buffer.
113 vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
114 vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
115 vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
116 vertexBufferDesc.CPUAccessFlags = 0;
117 vertexBufferDesc.MiscFlags = 0;
118 vertexBufferDesc.StructureByteStride = 0;
119
120 // Give the subresource structure a pointer to the vertex data.
121 vertexData.pSysMem = vertices;
122 vertexData.SysMemPitch = 0;
123 vertexData.SysMemSlicePitch = 0;
124
125 // Now finally create the vertex buffer.
126 result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
127 if (FAILED(result))
128 {
129 Logger::Get().Log("Could not create vertex buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
130 return false;
131 }
132
133 // Set up the description of the index buffer.
134 indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
135 indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
136 indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
137 indexBufferDesc.CPUAccessFlags = 0;
138 indexBufferDesc.MiscFlags = 0;
139 indexBufferDesc.StructureByteStride = 0;
140
141 // Give the subresource structure a pointer to the index data.
142 indexData.pSysMem = indices;
143 indexData.SysMemPitch = 0;
144 indexData.SysMemSlicePitch = 0;
145
146 // Create the index buffer.
147 result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
148 if (FAILED(result))
149 {
150 Logger::Get().Log("Could not create index buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
151 return false;
152 }
153
154 // Release the arrays now that the vertex and index buffers have been created and loaded.
155 delete[] vertices;
156 vertices = 0;
157
158 delete[] indices;
159 indices = 0;
160
161 Logger::Get().Log("Buffers initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
162
163 return true;
164}
165
166
167void display_plane_class::ShutdownBuffers()
168{
169 Logger::Get().Log("Shutting down Plane buffers", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
170
171 // Release the index buffer.
172 if (m_indexBuffer)
173 {
174 m_indexBuffer->Release();
175 m_indexBuffer = 0;
176 }
177
178 // Release the vertex buffer.
179 if (m_vertexBuffer)
180 {
181 m_vertexBuffer->Release();
182 m_vertexBuffer = 0;
183 }
184
185 Logger::Get().Log("Plane buffers shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
186
187 return;
188}
189
190
191void display_plane_class::RenderBuffers(ID3D11DeviceContext* deviceContext)
192{
193 unsigned int stride;
194 unsigned int offset;
195
196
197 // Set vertex buffer stride and offset.
198 stride = sizeof(VertexType);
199 offset = 0;
200
201 // Set the vertex buffer to active in the input assembler so it can be rendered.
202 deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
203
204 // Set the index buffer to active in the input assembler so it can be rendered.
205 deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
206
207 // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
208 deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
209
210 return;
211}
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158