xoreos  0.0.5
materialman.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/util.h"
26 
28 
30 
31 namespace Graphics {
32 
33 namespace Shader {
34 
36 }
37 
39  deinit();
40 }
41 
43  status("Initialising default materials...");
44 
45  ShaderMaterial *material = new ShaderMaterial(ShaderMan.getShaderObject("default/colour.frag", SHADER_FRAGMENT), "defaultWhite");
46  float *color = (float *)(material->getVariableData("_colour"));
47  if (color) {
48  color[0] = 1.0f;
49  color[1] = 1.0f;
50  color[2] = 1.0f;
51  color[3] = 1.0f;
52  }
53  _resourceMap[material->getName()] = material;
54 
55  material = new ShaderMaterial(ShaderMan.getShaderObject("default/colour.frag", SHADER_FRAGMENT), "defaultBlack");
56  color = (float *)(material->getVariableData("_colour"));
57  if (color) {
58  color[0] = 0.0f;
59  color[1] = 0.0f;
60  color[2] = 0.0f;
61  color[3] = 1.0f;
62  }
63  _resourceMap[material->getName()] = material;
64 }
65 
67  for (std::map<Common::UString, ShaderMaterial *>::iterator iter = _resourceMap.begin(); iter != _resourceMap.end(); ++iter) {
68  delete iter->second;
69  }
70  _resourceMap.clear();
71 }
72 
74  std::map<Common::UString, ShaderMaterial *>::iterator iter = _resourceMap.begin();
75  while (iter != _resourceMap.end()) {
76  ShaderMaterial *material = iter->second;
77  if (material->useCount() == 0) {
78  iter = delResource(iter);
79  } else {
80  iter++;
81  }
82  }
83 }
84 
86  if (!material) {
87  return;
88  }
89 
90  std::map<Common::UString, ShaderMaterial *>::iterator iter = _resourceMap.find(material->getName());
91  if (iter == _resourceMap.end()) {
92  _resourceMap[material->getName()] = material;
93  }
94 }
95 
97  if (!material) {
98  return;
99  }
100 
101  std::map<Common::UString, ShaderMaterial *>::iterator iter = _resourceMap.find(material->getName());
102  if (iter != _resourceMap.end()) {
103  delResource(iter);
104  }
105 }
106 
108  std::map<Common::UString, ShaderMaterial *>::iterator iter = _resourceMap.find(name);
109  if (iter != _resourceMap.end()) {
110  return iter->second;
111  } else {
112  return 0;
113  }
114 }
115 
116 std::map<Common::UString, ShaderMaterial *>::iterator MaterialManager::delResource(std::map<Common::UString, ShaderMaterial *>::iterator iter) {
117  std::map<Common::UString, ShaderMaterial *>::iterator inext = iter;
118  inext++;
119  delete iter->second;
120  _resourceMap.erase(iter);
121 
122  return inext;
123 }
124 
125 } // End of namespace Shader
126 
127 } // End of namespace Graphics
void addMaterial(ShaderMaterial *material)
Adds a material to be managed.
Definition: materialman.cpp:85
std::map< Common::UString, ShaderMaterial * > _resourceMap
Definition: materialman.h:64
std::map< Common::UString, ShaderMaterial * >::iterator delResource(std::map< Common::UString, ShaderMaterial *>::iterator iter)
A class holding an UTF-8 string.
Definition: ustring.h:48
void cleanup()
Remove any resource that has a usage count of zero.
Definition: materialman.cpp:73
void * getVariableData(uint32 index) const
The material manager.
Definition: materialman.h:41
void delMaterial(ShaderMaterial *material)
Forcibly remove the material from the map.
Definition: materialman.cpp:96
const Common::UString & getName() const
Utility templates and functions.
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
void init()
Initialise material management, including default material creation.
Definition: materialman.cpp:42
The global shader material manager.
ShaderMaterial * getMaterial(const Common::UString &name)
Returns a material with the given name, or zero if it does not exist.
#define ShaderMan
Shortcut for accessing the shader manager.
Definition: shader.h:293
void status(const char *s,...)
Definition: util.cpp:52
void deinit()
Deinitialise material management subsystem.
Definition: materialman.cpp:66