From ca0c1d3e6f8b5fa4eb2e0a86fcf47b12fb600786 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 27 Sep 2008 17:16:15 +0000 Subject: mission targets, texture unloading, private messages --- src/render/textures.cc | 67 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 10 deletions(-) (limited to 'src/render/textures.cc') diff --git a/src/render/textures.cc b/src/render/textures.cc index a041c32..b6b7f05 100644 --- a/src/render/textures.cc +++ b/src/render/textures.cc @@ -19,14 +19,18 @@ namespace render { std::map Textures::registry; -size_t Textures::index = 0; GLuint Textures::textures[MAXTEXTURES]; void Textures::init() { - clear(); con_print << "^BLoading textures..." << std::endl; + if (registry.size()) { + clear(); + } else { + memset(textures,0, sizeof(textures)); + } + // "no texture" bitmap load("textures/common/notex"); @@ -51,14 +55,54 @@ void Textures::shutdown() clear(); } +void Textures::list() +{ + for (iterator it = registry.begin(); it != registry.end(); it++) { + con_print << " " << (*it).first << " " << (*it).second << std::endl; + } + con_print << registry.size() << " loaded textures" << std::endl; +} + void Textures::clear() { - if (index) - glDeleteTextures(index, textures); + for (size_t i=0; i < MAXTEXTURES; i++) { + if (textures[i]) { + glDeleteTextures(1, &textures[i]); + } + } registry.clear(); memset(textures,0, sizeof(textures)); - index = 0; +} + +void Textures::unload(std::string name) +{ + iterator it = registry.find(name); + if (it != registry.end()) { + con_debug << " unloading " << (*it).first << std::endl; + size_t id = (*it).second; + if (textures[id]) { + glDeleteTextures(1, &textures[id]); + } + registry.erase(it); + + } +} + +void Textures::unload(size_t id) +{ + // find in map + for (iterator it = registry.begin(); it != registry.end(); it++) { + if ((*it).second == id) { + con_debug << " unloading " << (*it).first << std::endl; + size_t id = (*it).second; + if (textures[id]) { + glDeleteTextures(1, &textures[id]); + } + registry.erase(it); + break; + } + } } size_t Textures::load(std::string name, bool filter) @@ -68,7 +112,12 @@ size_t Textures::load(std::string name, bool filter) if (it != registry.end()) return (*it).second; - if (index == MAXTEXTURES) { + // find first available texture + size_t id = 0; + while ((id < MAXTEXTURES) && (textures[id])) { + id++; + } + if (id == MAXTEXTURES) { con_error << "Texture limit " << MAXTEXTURES << " exceeded!" << std::endl; registry[name] = 0; return 0; @@ -105,8 +154,6 @@ size_t Textures::load(std::string name, bool filter) return 0; } - size_t id = index; - glGenTextures(1, &textures[id]); glBindTexture(GL_TEXTURE_2D, textures[id]); @@ -123,7 +170,6 @@ size_t Textures::load(std::string name, bool filter) // add to the registry registry[name] = id; - index++; // delete image data delete image; @@ -157,7 +203,7 @@ size_t Textures::bind(std::string name, bool filter) size_t Textures::bind(size_t texture, bool filter) { size_t id = texture; - if (texture >= index) + if (!textures[id]) id = 0; glBindTexture(GL_TEXTURE_2D, textures[id]); @@ -175,4 +221,5 @@ void Textures::set_filter(bool filter) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } } + } -- cgit v1.2.3