-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaterialManager.cpp
147 lines (131 loc) · 2.91 KB
/
MaterialManager.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "MaterialManager.h"
#include<stdio.h>
#include<cstdlib>
#include<iostream>
#include<string.h>
#include<fstream>
#include "dirent.h"
#include "Logger.h"
MaterialManager* MaterialManager::m_pInstance = 0;
MaterialManager::MaterialManager()
{
}
MaterialManager *MaterialManager::getInstance()
{
if (m_pInstance == 0)
{
m_pInstance = new MaterialManager();
}
return m_pInstance;
}
void MaterialManager::initialize()
{
Logging::GRAPHICS->info("Loading Materials");
DIR *pDIR;
struct dirent *entry;
if( pDIR=opendir("./Data/Materials") ){
while(entry = readdir(pDIR)){
if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 )
loadMaterial(string("./Data/Materials/") + entry->d_name);
}
closedir(pDIR);
}
Logging::GRAPHICS->info("Done Loading Materials");
}
void MaterialManager::loadMaterial(string sFileName)
{
Material *newMaterial = new Material();
if (newMaterial->loadMaterial(sFileName.c_str()))
{
string *sName = newMaterial->getName();
m_materials[*sName] = newMaterial;
}
else
{
Logging::GRAPHICS->error("Material failed to load: " + sFileName);
}
}
/*
* returns a string that is not currently being used
*/
string MaterialManager::getSafeName(string baseName)
{
if (!hasMaterial(baseName))
{
return baseName;
}
else
{
int nNum = 1;
while (true)
{
char buff[10];
itoa(nNum, buff, 10);
string sNum = string(buff);
string newName = baseName + sNum;
if (!hasMaterial(newName))
{
return newName;
}
nNum++;
}
}
}
/* returns the name of the material */
string MaterialManager::addMaterialSafe(Material *material)
{
string matName = getSafeName(*(material->getName()));
material->setName(matName);
m_materials[matName] = material;
return matName;
}
bool MaterialManager::hasMaterial(string sName)
{
if (m_materials.find(sName) != m_materials.end())
{
return true;
}
return false;
}
string MaterialManager::renameMaterial(string sCurrentName, string sNewName)
{
Material *mat = m_materials[sCurrentName];
m_materials.erase(sCurrentName);
sNewName = getSafeName(sNewName);
mat->setName(sNewName);
m_materials[sNewName] = mat;
return sNewName;
}
void MaterialManager::deleteMaterial(string sName)
{
if (m_materials.find(sName) != m_materials.end())
{
delete m_materials[sName];
m_materials.erase(sName);
}
}
void MaterialManager::deleteAllMaterials()
{
m_materials.clear();
}
void MaterialManager::saveAllMaterials()
{
for (std::map<std::string, Material *>::iterator it = m_materials.begin(); it != m_materials.end(); it++)
{
Material *mat = (*it).second;
string sFileName = "Data/Materials/" + (*it).first + ".amtl";
mat->saveMaterial(sFileName.c_str());
}
}
void MaterialManager::useMaterial(string sName)
{
m_materials[sName]->use();
}
Material *MaterialManager::getMaterial(string sName)
{
return m_materials[sName];
}
map<string,Material *> *MaterialManager::getMaterials()
{
return &m_materials;
}