2 c++ opengl static static-methods
我想有一个加载文件的函数(在这种情况下是一个OpenGL纹理),但实际上只加载文件一次,每次调用它之后它只返回它最初加载的内容.
这样做有什么好办法?
谢谢.
你需要一些地方来存储那个州.它既可以在对象内部,也可以作为静态变量.让我们说:
class TextureLoader {
public:
TextureLoader() {}
GLuint loadTexture(std::string const & filename){
std::map<std::string, GLuint>::iterator it = m_loadedTextures.find(filename);
if(it == m_loadedTextures.end()){
GLuint id = load(filename);
m_loadedTextures[filename] = id;
return id;
}
else{
return it->second;
}
}
~TextureLoader(){
// iterate and delete textures
}
private:
GLuint load(std::string const & filename){
// real loading
}
std::map<std::string, GLuint> m_loadedTextures;
};
Run Code Online (Sandbox Code Playgroud)