-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextureManager.cpp
131 lines (111 loc) · 4.06 KB
/
TextureManager.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <boost/scoped_array.hpp>
#include <stdio.h>
#include "TextureManager.h"
#include "Exceptions.h"
namespace typing
{
//////////////////////////////////////////////////////////////////////////
// Texture
//////////////////////////////////////////////////////////////////////////
void Texture::Load(const std::string& textureName)
{
FILE *textureFile = fopen(textureName.c_str(), "rb");
if (!textureFile)
{
throw FileNotFoundException(textureName);
}
unsigned char expectedHeader[12] = { 0,0,2,0,0,0,0,0,0,0,0,0 };
unsigned char header[12];
if (fread(header, sizeof(unsigned char), sizeof(header), textureFile) != sizeof(header) ||
memcmp(header, expectedHeader, sizeof(expectedHeader)))
{
fclose(textureFile);
throw FileCorruptException(textureName);
}
unsigned char info[6];
if (fread(info, sizeof(unsigned char), sizeof(info), textureFile) != sizeof(info))
{
fclose(textureFile);
throw FileCorruptException(textureName);
}
unsigned int width = info[1] * 256 + info[0];
unsigned int height = info[3] * 256 + info[2];
unsigned int bpp = info[4];
if (width == 0 || height == 0 || (bpp != 24 && bpp != 32))
{
fclose(textureFile);
throw FileCorruptException(textureName);
}
unsigned int size = width * height * (bpp / 8);
boost::scoped_array<unsigned char> data(new unsigned char[size]);
if (!data.get())
{
fclose(textureFile);
throw MemoryAllocationError(__FUNCTION__);
}
if (fread(data.get(), 1, size, textureFile) != size)
{
fclose(textureFile);
throw FileCorruptException(textureName);
}
unsigned char temp;
for (unsigned int i = 0; i < size; i += (bpp / 8))
{
temp = data[i];
data[i] = data[i + 2];
data[i + 2] = temp;
}
fclose(textureFile);
glGenTextures(1, &m_id);
glBindTexture(GL_TEXTURE_2D, m_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
glPixelStorei(GL_UNPACK_ALIGNMENT, bpp == 32 ? 2 : 1);
glTexImage2D(GL_TEXTURE_2D, 0,
bpp == 32 ? GL_RGBA8 : GL_RGB8,
width, height, 0,
bpp == 32 ? GL_BGRA : GL_BGR,
GL_UNSIGNED_BYTE, data.get());
}
void Texture::Bind() const
{
glBindTexture(GL_TEXTURE_2D, m_id);
}
//////////////////////////////////////////////////////////////////////////
// TextureManager
//////////////////////////////////////////////////////////////////////////
std::auto_ptr<TextureManager> TextureManager::m_singleton(new TextureManager);
TextureManager& TextureManager::GetTextureManager()
{
return *(m_singleton.get());
}
void TextureManager::Add(const std::string& textureName)
{
if (m_textureMap.find(textureName) == m_textureMap.end())
{
TexturePtr texture(new Texture());
texture->Load(textureName);
m_textureMap[textureName] = texture;
}
}
const Texture& TextureManager::Get(const std::string& textureName) const
{
TextureMap::const_iterator iter = m_textureMap.find(textureName);
if (iter == m_textureMap.end())
{
throw MediaNotLoadedException(textureName);
}
else
{
return *(iter->second);
}
}
void TextureManager::Bind(const std::string& textureName) const
{
Get(textureName).Bind();
}
}