-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.h
More file actions
114 lines (96 loc) · 3.33 KB
/
Texture.h
File metadata and controls
114 lines (96 loc) · 3.33 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
#pragma once
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "DXCore.h"
#include <string>
class Texture {
ID3D11Texture2D* texture;
//DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
public:
ID3D11ShaderResourceView* srv;
void init(DXCOre* core,int width, int height, int channels, DXGI_FORMAT format, unsigned char* data) {
D3D11_TEXTURE2D_DESC texDesc;
memset(&texDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
texDesc.Width = width;
texDesc.Height = height;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = format;
texDesc.SampleDesc.Count = 1;
texDesc.Usage = D3D11_USAGE_DEFAULT;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
D3D11_SUBRESOURCE_DATA initData;
memset(&initData, 0, sizeof(D3D11_SUBRESOURCE_DATA));
initData.pSysMem = data;
initData.SysMemPitch = width * channels;
core->device->CreateTexture2D(&texDesc, &initData, &texture);
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
srvDesc.Format = format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
core->device->CreateShaderResourceView(texture, &srvDesc, &srv);
}
void free() {
srv->Release();
texture->Release();
}
void load(DXCOre* core ,std::string filename) {
int width = 0;
int height = 0;
int channels = 0;
unsigned char* texels = stbi_load(filename.c_str(), &width, &height, &channels, 0);
if (channels == 3) {
channels = 4;
unsigned char* texelsWithAlpha = new unsigned char[width * height * channels];
for (int i = 0; i < (width * height); i++) {
texelsWithAlpha[i * 4] = texels[i * 3];
texelsWithAlpha[(i * 4) + 1] = texels[(i * 3) + 1];
texelsWithAlpha[(i * 4) + 2] = texels[(i * 3) + 2];
texelsWithAlpha[(i * 4) + 3] = 255;
}
// Initialize texture using width, height, channels, and texelsWithAlpha
init(core, width, height, channels, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, texelsWithAlpha);
delete[] texelsWithAlpha;
}
else {
// Initialize texture using width, height, channels, and texels
init(core, width, height, channels, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, texels);
}
stbi_image_free(texels);
}
};
class TextureManager
{
public:
std::map<std::string, Texture*> textures;
void load(DXCOre* core, std::string filename)
{
std::map<std::string, Texture*>::iterator it = textures.find(filename);
if (it != textures.end())
{
return;
}
Texture* texture = new Texture();
texture->load(core, filename);
textures.insert({ filename, texture });
}
ID3D11ShaderResourceView* find(std::string name)
{
return textures[name]->srv;
}
void unload(std::string name)
{
textures[name]->free();
textures.erase(name);
}
~TextureManager()
{
for (auto it = textures.cbegin(); it != textures.cend(); )
{
it->second->free();
textures.erase(it++);
}
}
};