TextureStore.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <graph/TextureStore.h>
00022 #include <graph/OptionsDisplay.h>
00023 #include <image/ImageFactory.h>
00024 #include <GLEXT/GLTexture.h>
00025 #include <common/Defines.h>
00026 
00027 TextureStore *TextureStore::instance_ = 0;
00028 
00029 TextureStore *TextureStore::instance()
00030 {
00031         if (!instance_)
00032         {
00033                 instance_ = new TextureStore;
00034         }
00035         return instance_;
00036 }
00037 
00038 TextureStore::TextureStore()
00039 {
00040 }
00041 
00042 TextureStore::~TextureStore()
00043 {
00044 }
00045 
00046 GLTexture *TextureStore::loadTexture(const std::string &name, 
00047                                                                          const std::string &aname, 
00048                                                                          bool invert)
00049 {
00050         std::string wholeName;
00051         wholeName += name;
00052         wholeName += "::";
00053         wholeName += aname;
00054 
00055         // Try to find the texture in the cache first
00056         std::map<std::string, GLTexture *>::iterator itor =
00057                 skins_.find(wholeName);
00058         if (itor != skins_.end())
00059         {
00060                 return (*itor).second;
00061         }
00062 
00063         // Load tank skin as bitmap
00064         Image *map = 0;
00065         if (aname[0])
00066         {
00067                 map = ImageFactory::loadImage(name, aname, invert);
00068                 if (!map->getBits())
00069                 {
00070                         S3D::dialogMessage("Scorched3D load texture", S3D::formatStringBuffer(
00071                                                   "Failed to load texture file \"%s\",\n"
00072                                                   "alpha file \"%s\"",
00073                                                   name.c_str(),
00074                                                   aname.c_str()));
00075                         return 0;
00076                 }
00077         }
00078         else
00079         {
00080                 map = ImageFactory::loadImage(name);
00081                 if (!map->getBits())
00082                 {
00083                         S3D::dialogMessage("Scorched3D load texture", S3D::formatStringBuffer(
00084                                                   "Failed to load texture file \"%s\"",
00085                                                   name.c_str()));
00086                         return 0;
00087                 }
00088         }
00089 
00090         // HACK for skin creator
00091 #ifdef dDOUBLE
00092         // Use smaller tank skins for texture size 0
00093         // Resize the bitmap
00094         if (OptionsDisplay::instance()->getTexSize() == 0)
00095         {
00096                 map->resize(map->getWidth() / 2, 
00097                                    map->getHeight() / 2);
00098         }
00099 #endif
00100 
00101         // Create skin texture from bitmap
00102         GLTexture *texture = new GLTexture;
00103         if (!texture->create(*map))
00104         {
00105                 delete map;
00106                 S3D::dialogMessage("Scorched3D create texture", S3D::formatStringBuffer(
00107                                           "Failed to create texture \"%s\"",
00108                                           name.c_str()));
00109                 return 0;
00110         }
00111         delete map;
00112 
00113         skins_[wholeName] = texture;
00114         return texture;
00115 }
00116 

Generated on Mon Feb 16 15:14:46 2009 for Scorched3D by  doxygen 1.5.3