Khaotic Engine Reborn
Loading...
Searching...
No Matches
texture_class Class Reference

Public Member Functions

 texture_class (const texture_class &)
 
bool Initialize (ID3D11Device *, ID3D11DeviceContext *, std::string)
 
void Shutdown ()
 
ID3D11ShaderResourceView * GetTexture ()
 
int GetWidth ()
 
int GetHeight ()
 

Detailed Description

Definition at line 16 of file texture_class.h.

Constructor & Destructor Documentation

◆ texture_class() [1/2]

texture_class::texture_class ( )

Definition at line 3 of file texture_class.cpp.

4{
5 m_targaData = 0;
6 m_texture = 0;
7 m_textureView = 0;
8}

◆ texture_class() [2/2]

texture_class::texture_class ( const texture_class & other)

Definition at line 11 of file texture_class.cpp.

12{
13}

◆ ~texture_class()

texture_class::~texture_class ( )

Definition at line 16 of file texture_class.cpp.

17{
18}

Member Function Documentation

◆ GetHeight()

int texture_class::GetHeight ( )

Definition at line 239 of file texture_class.cpp.

240{
241 return m_height;
242}

◆ GetTexture()

ID3D11ShaderResourceView * texture_class::GetTexture ( )

Definition at line 117 of file texture_class.cpp.

118{
119 return m_textureView;
120}

◆ GetWidth()

int texture_class::GetWidth ( )

Definition at line 233 of file texture_class.cpp.

234{
235 return m_width;
236}

◆ Initialize()

bool texture_class::Initialize ( ID3D11Device * device,
ID3D11DeviceContext * deviceContext,
std::string filename )

Definition at line 20 of file texture_class.cpp.

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}
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

◆ Shutdown()

void texture_class::Shutdown ( )

Definition at line 87 of file texture_class.cpp.

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}

The documentation for this class was generated from the following files: