ExplosionTextures.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 <image/ImageFactory.h>
00022 #include <XML/XMLFile.h>
00023 #include <sprites/ExplosionTextures.h>
00024 #include <landscape/Landscape.h>
00025 #include <lang/LangResource.h>
00026 #include <common/Defines.h>
00027 #include <stdio.h>
00028 
00029 ExplosionTextures *ExplosionTextures::instance_ = 0;
00030 
00031 ExplosionTextures *ExplosionTextures::instance()
00032 {
00033         if (!instance_)
00034         {
00035                 instance_ = new ExplosionTextures;
00036         }
00037 
00038         return instance_;
00039 }
00040 
00041 ExplosionTextures::ExplosionTextures()
00042 {
00043 
00044 }
00045 
00046 ExplosionTextures::~ExplosionTextures()
00047 {
00048 
00049 }
00050 
00051 bool ExplosionTextures::addTextureToSet(GLTextureSet &set,
00052                                                                                 const char *texPath)
00053 {
00054         ImageHandle bitmap =
00055                 ImageFactory::loadImageHandle(
00056                         (char *) texPath, (char *) texPath, false);
00057         GLTexture *texture = new GLTexture;
00058 
00059         if (!texture->create(bitmap)) return false;
00060         set.addTexture(texture);
00061         return true;
00062 }
00063 
00064 bool ExplosionTextures::createTextures(ProgressCounter *counter)
00065 {
00066         if (counter) counter->setNewOp(LANG_RESOURCE("EXPLOSION_TEXTURES", "Explosion Textures"));
00067 
00068         std::string file1 = S3D::getDataFile("data/textures/smoke01.bmp");
00069         ImageHandle bitmap = ImageFactory::loadImageHandle(file1.c_str(), file1.c_str(), false);
00070         smokeTexture.create(bitmap);
00071         DIALOG_ASSERT(smokeTexture.textureValid());
00072 
00073         std::string file2 = S3D::getDataFile("data/textures/smoke02.bmp");
00074         ImageHandle bitmap2 = ImageFactory::loadImageHandle(file2.c_str(), file2.c_str(), false);
00075         smokeTexture2.create(bitmap2);
00076         DIALOG_ASSERT(smokeTexture2.textureValid());
00077 
00078         std::string file3 = S3D::getDataFile("data/textures/particle.bmp");
00079         ImageHandle bitmap3 = ImageFactory::loadImageHandle(file3.c_str(), file3.c_str(), false);
00080         particleTexture.create(bitmap3);
00081         DIALOG_ASSERT(particleTexture.textureValid());
00082 
00083         ImageHandle talkBitmap = ImageFactory::loadAlphaImageHandle(S3D::getDataFile("data/textures/talk.bmp"));
00084         talkTexture.create(talkBitmap);
00085         DIALOG_ASSERT(talkTexture.textureValid());
00086 
00087         std::string file5 = S3D::getDataFile("data/textures/rain.bmp");
00088         std::string file5m = S3D::getDataFile("data/textures/rainm.bmp");
00089         ImageHandle bitmap5 = ImageFactory::loadImageHandle(file5m.c_str(), file5.c_str(), false);
00090         rainTexture.create(bitmap5);
00091         DIALOG_ASSERT(rainTexture.textureValid());
00092 
00093         std::string file6 = S3D::getDataFile("data/textures/snow.bmp");
00094         std::string file6m = S3D::getDataFile("data/textures/snowm.bmp");
00095         ImageHandle bitmap6 = ImageFactory::loadImageHandle(file6.c_str(), file6m.c_str(), false);
00096         snowTexture.create(bitmap6);
00097         DIALOG_ASSERT(snowTexture.textureValid());
00098 
00099         XMLFile file;
00100         if (!file.readFile(S3D::getDataFile("data/textureset.xml")))
00101         {
00102                 S3D::dialogMessage("ExplosionTextures", S3D::formatStringBuffer(
00103                                           "Failed to parse \"%s\"\n%s", 
00104                                           "data/textureset.xml",
00105                                           file.getParserError()));
00106                 return false;
00107         }
00108         if (!file.getRootNode())
00109         {
00110                 S3D::dialogMessage("ExplosionTextures", S3D::formatStringBuffer(
00111                                           "Failed to find explosion textures definition file \"%s\"",
00112                                           "data/textureset.xml"));
00113                 return false;           
00114         }
00115 
00116     std::list<XMLNode *>::iterator childrenItor;
00117         std::list<XMLNode *> &children = file.getRootNode()->getChildren();
00118     for (childrenItor = children.begin();
00119                  childrenItor != children.end();
00120                  childrenItor++)
00121     {
00122                 // Check if it is a texture set
00123         XMLNode *currentNode = (*childrenItor);
00124                 if (strcmp(currentNode->getName(), "textureset"))
00125                 {
00126                         S3D::dialogMessage("ExplosionTextures",
00127                                                   "Failed to find textureset node");
00128                         return false;
00129                 }
00130                 XMLNode *name = 0;
00131                 if (!currentNode->getNamedParameter("name", name)) return false;
00132                 const char *setName = name->getContent();
00133 
00134                 // Create and store the new texture set
00135                 GLTextureSet *set = new GLTextureSet();
00136                 textureSets[setName] = set;
00137 
00138                 // Load all the textures
00139                 std::list<XMLNode *>::iterator textureItor;
00140                 std::list<XMLNode *> &textures = currentNode->getChildren();
00141                 for (textureItor = textures.begin();
00142                         textureItor != textures.end();
00143                         textureItor++)
00144                 {
00145                         // Check if it is a texture
00146                         XMLNode *currentTexture = (*textureItor);
00147                         if (strcmp(currentTexture->getName(), "texture"))
00148                         {
00149                                 S3D::dialogMessage("ExplosionTextures",
00150                                                         "Failed to find texture sub-node");
00151                                 return false;
00152                         }
00153 
00154                         // Load texture
00155                         std::string texFile = 
00156                                 S3D::getDataFile(S3D::formatStringBuffer("data/%s", currentTexture->getContent()));
00157                         if (!addTextureToSet(*set, texFile.c_str()))
00158                         {
00159                                 S3D::dialogMessage("ExplosionTextures", S3D::formatStringBuffer(
00160                                                         "Failed to load texture %s",
00161                                                         texFile.c_str()));
00162                                 return false;
00163                         }
00164                 }
00165         }
00166 
00167         return true;
00168 }
00169 
00170 GLTextureSet *ExplosionTextures::getTextureSetByName(const char *name)
00171 {
00172         DIALOG_ASSERT(!textureSets.empty());
00173 
00174         GLTextureSet *result = (*textureSets.begin()).second;
00175         std::map<std::string, GLTextureSet*>::iterator itor =
00176                 textureSets.find(name);
00177         if (itor != textureSets.end())
00178         {
00179                 result = (*itor).second;
00180         }
00181         return result;
00182 }
00183 
00184 Image &ExplosionTextures::getScorchBitmap(const char *name)
00185 {
00186         if (name[0])
00187         {
00188                 std::map<std::string, Image*>::iterator findItor =
00189                         scorchedBitmaps.find(name);
00190                 if (findItor != scorchedBitmaps.end())
00191                 {
00192                         return *(*findItor).second;
00193                 }
00194 
00195                 std::string fileName = S3D::getDataFile(name);
00196                 if (S3D::fileExists(fileName))
00197                 {
00198                         Image *map = ImageFactory::loadImage(fileName);
00199                         scorchedBitmaps[name] = map;
00200                         return *map;
00201                 }
00202         }
00203         return Landscape::instance()->getScorchMap();
00204 }

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