Khaotic Engine Reborn
Loading...
Searching...
No Matches
texture_class.cpp
1#include "texture_class.h"
2
3texture_class::texture_class()
4{
5 m_targaData = 0;
6 m_texture = 0;
7 m_textureView = 0;
8}
9
10
11texture_class::texture_class(const texture_class& other)
12{
13}
14
15
16texture_class::~texture_class()
17{
18}
19
20bool texture_class::Initialize(ID3D11Device * device, ID3D11DeviceContext * deviceContext, std::string filename)
21{
22 Logger::Get().Log(("Iinitializing texture: %s", filename), __FILE__, __LINE__, Logger::LogLevel::Initialize);
23
24 bool result;
25 D3D11_TEXTURE2D_DESC textureDesc;
26 HRESULT hResult;
27 unsigned int rowPitch;
28 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
29 // Load the targa image data into memory.
30 result = LoadTarga(filename);
31 if (!result)
32 {
33 Logger::Get().Log("Failed to load targa data", __FILE__, __LINE__, Logger::LogLevel::Error);
34 return false;
35 }
36 // Setup the description of the texture.
37 textureDesc.Height = m_height;
38 textureDesc.Width = m_width;
39 textureDesc.MipLevels = 0;
40 textureDesc.ArraySize = 1;
41 textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
42 textureDesc.SampleDesc.Count = 1;
43 textureDesc.SampleDesc.Quality = 0;
44 textureDesc.Usage = D3D11_USAGE_DEFAULT;
45 textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
46 textureDesc.CPUAccessFlags = 0;
47 textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
48
49 // Create the empty texture.
50 hResult = device->CreateTexture2D(&textureDesc, NULL, &m_texture);
51 if (FAILED(hResult))
52 {
53 Logger::Get().Log("Failed to create texture", __FILE__, __LINE__, Logger::LogLevel::Error);
54 return false;
55 }
56
57 // Set the row pitch of the targa image data.
58 rowPitch = (m_width * 4) * sizeof(unsigned char);
59 // Copy the targa image data into the texture.
60 deviceContext->UpdateSubresource(m_texture, 0, NULL, m_targaData, rowPitch, 0);
61 // Setup the shader resource view description.
62 srvDesc.Format = textureDesc.Format;
63 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
64 srvDesc.Texture2D.MostDetailedMip = 0;
65 srvDesc.Texture2D.MipLevels = -1;
66
67 // Create the shader resource view for the texture.
68 hResult = device->CreateShaderResourceView(m_texture, &srvDesc, &m_textureView);
69 if (FAILED(hResult))
70 {
71 Logger::Get().Log("Failed to create shader resource view", __FILE__, __LINE__, Logger::LogLevel::Error);
72 return false;
73 }
74
75 // Generate mipmaps for this texture.
76 deviceContext->GenerateMips(m_textureView);
77
78 // Release the targa image data now that the image data has been loaded into the texture.
79 delete[] m_targaData;
80 m_targaData = 0;
81
82 Logger::Get().Log("Texture initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
83
84 return true;
85}
86
87void texture_class::Shutdown()
88{
89
90 Logger::Get().Log("Shutting down texture", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
91 // Release the texture view resource.
92 if (m_textureView)
93 {
94 m_textureView->Release();
95 m_textureView = 0;
96 }
97
98 // Release the texture.
99 if (m_texture)
100 {
101 m_texture->Release();
102 m_texture = 0;
103 }
104
105 // Release the targa data.
106 if (m_targaData)
107 {
108 delete[] m_targaData;
109 m_targaData = 0;
110 }
111
112 Logger::Get().Log("Texture shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
113
114 return;
115}
116
117ID3D11ShaderResourceView* texture_class::GetTexture()
118{
119 return m_textureView;
120}
121
122bool texture_class::LoadTarga(std::string filename)
123{
124
125 Logger::Get().Log(("Loading targa file: %s", filename), __FILE__, __LINE__);
126 int error, bpp, imageSize, index, i, j, k;
127 FILE* filePtr;
128 unsigned int count;
129 TargaHeader targaFileHeader;
130 unsigned char* targaImage;
131
132
133 // Open the targa file for reading in binary.
134 error = fopen_s(&filePtr, filename.c_str(), "rb");
135 if (error != 0)
136 {
137 Logger::Get().Log("Failed to open targa file. Working directory: " + std::filesystem::current_path().string(), __FILE__, __LINE__, Logger::LogLevel::Error);
138 return false;
139 }
140
141 // Read in the file header.
142 count = (unsigned int)fread(&targaFileHeader, sizeof(TargaHeader), 1, filePtr);
143 if (count != 1)
144 {
145 Logger::Get().Log("Failed to read targa file header", __FILE__, __LINE__, Logger::LogLevel::Error);
146 return false;
147 }
148
149 // Get the important information from the header.
150 m_height = (int)targaFileHeader.height;
151 m_width = (int)targaFileHeader.width;
152 bpp = (int)targaFileHeader.bpp;
153
154 // Check that it is 32 bit and not 24 bit.
155 if (bpp != 32 && bpp != 24)
156 {
157 Logger::Get().Log("Targa file is not 32 or 24 bit", __FILE__, __LINE__, Logger::LogLevel::Error);
158 return false;
159 }
160
161 // Calculate the size of the 32 bit image data.
162 imageSize = m_width * m_height * (bpp / 8);
163
164 // Allocate memory for the targa image data.
165 targaImage = new unsigned char[imageSize];
166
167 // Read in the targa image data.
168 count = (unsigned int)fread(targaImage, 1, imageSize, filePtr);
169 if (count != imageSize)
170 {
171 Logger::Get().Log("Failed to read targa image data", __FILE__, __LINE__, Logger::LogLevel::Error);
172 return false;
173 }
174
175 // Close the file.
176 error = fclose(filePtr);
177 if (error != 0)
178 {
179 Logger::Get().Log("Failed to close targa file", __FILE__, __LINE__, Logger::LogLevel::Error);
180 return false;
181 }
182
183 // Allocate memory for the targa destination data.
184 m_targaData = new unsigned char[imageSize];
185
186 // Initialize the index into the targa destination data array.
187 index = 0;
188
189 // Initialize the index into the targa image data.
190 k = (m_width * m_height * (bpp / 8)) - (m_width * (bpp / 8));
191
192 // Now copy the targa image data into the targa destination array in the correct order since the targa format is stored upside down and also is not in RGBA order.
193 for (j = 0; j < m_height; j++)
194 {
195 for (i = 0; i < m_width; i++)
196 {
197 if (index + 3 < imageSize) // Ajout de la vérification ici
198 {
199 m_targaData[index + 0] = targaImage[k + 2]; // Red.
200 m_targaData[index + 1] = targaImage[k + 1]; // Green.
201 m_targaData[index + 2] = targaImage[k + 0]; // Blue
202 if (bpp == 32)
203 {
204 m_targaData[index + 3] = targaImage[k + 3]; // Alpha
205 }
206 }
207 else
208 {
209 Logger::Get().Log("Index out of bounds", __FILE__, __LINE__, Logger::LogLevel::Error);
210 return false;
211 }
212
213 // Increment the indexes into the targa data.
214 k += (bpp / 8);
215 index += (bpp / 8);
216 }
217
218 // Set the targa image data index back to the preceding row at the beginning of the column since its reading it in upside down.
219 k -= (m_width * (bpp / 8) * 2);
220 }
221
222
223 // Release the targa image data now that it was copied into the destination array.
224 delete[] targaImage;
225 targaImage = 0;
226
227 Logger::Get().Log(("targa file %s loaded", filename), __FILE__, __LINE__);
228
229 return true;
230}
231
232
233int texture_class::GetWidth()
234{
235 return m_width;
236}
237
238
239int texture_class::GetHeight()
240{
241 return m_height;
242}
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