ModelID.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 <common/ModelID.h>
00022 #include <common/Defines.h>
00023 #include <net/NetBuffer.h>
00024 #include <XML/XMLParser.h>
00025 #include <3dsparse/TreeModelFactory.h>
00026 
00027 ModelID::ModelID()
00028 {
00029 }
00030 
00031 ModelID::ModelID(const ModelID &other) : 
00032         type_(other.type_),
00033         meshName_(other.meshName_),
00034         skinName_(other.skinName_)
00035 {
00036 }
00037 
00038 ModelID::~ModelID()
00039 {
00040 }
00041 
00042 ModelID &ModelID::operator=(const ModelID &other)
00043 {
00044         type_ = other.type_;
00045         meshName_ = other.meshName_;
00046         skinName_ = other.skinName_;
00047         return *this;
00048 }
00049 
00050 bool ModelID::initFromString(
00051                 const char *type,
00052                 const char *meshName,
00053                 const char *skinName)
00054 {
00055         type_ = type;
00056         meshName_ = meshName;
00057         skinName_ = skinName;
00058         return true;
00059 }
00060 
00061 
00062 bool ModelID::initFromNode(const char *directory, XMLNode *modelNode)
00063 {
00064         XMLNode *typeNode = 0;
00065         if (!modelNode->getNamedParameter("type", typeNode)) return false;
00066 
00067         // Store the model type
00068         type_ = typeNode->getContent();
00069 
00070         // Create the correct model depending on the model type
00071         if (strcmp(typeNode->getContent(), "ase") == 0)
00072         {
00073                 // 3DS Studio ASCII Files
00074                 XMLNode *meshNode, *skinNode;
00075                 if (!modelNode->getNamedChild("mesh", meshNode)) return false;
00076                 const char *meshNameContent = meshNode->getContent();
00077                 static char meshName[1024];
00078                 snprintf(meshName, 1024, "%s/%s", directory, meshNameContent);
00079                 if (!S3D::fileExists(S3D::getDataFile(meshName)))
00080                 {
00081                         return modelNode->returnError(
00082                                 S3D::formatStringBuffer(
00083                                         "Mesg file \"%s\" does not exist",
00084                                         meshName));
00085                         return false;
00086                 }
00087 
00088                 if (!modelNode->getNamedChild("skin", skinNode)) return false;
00089                 const char *skinNameContent = skinNode->getContent();
00090                 static char skinName[1024];
00091                 if (strcmp(skinNameContent, "none") != 0)
00092                 {
00093                         snprintf(skinName, 1024, "%s/%s", directory, skinNameContent);
00094                         if (!S3D::fileExists(S3D::getDataFile(skinName)))
00095                         {
00096                                 return modelNode->returnError(
00097                                         S3D::formatStringBuffer(
00098                                                 "Skin file \"%s\" does not exist",
00099                                                 skinName));
00100                         }
00101                 }
00102                 else
00103                 {
00104                         snprintf(skinName, 1024, "%s", skinNameContent);
00105                 }
00106 
00107                 meshName_ = meshName;
00108                 skinName_ = skinName;
00109         }
00110         else if (strcmp(typeNode->getContent(), "MilkShape") == 0)
00111         {
00112                 const char *meshNameContent = modelNode->getContent();
00113                 static char meshName[1024];
00114                 snprintf(meshName, 1024, "%s/%s", directory, meshNameContent);
00115 
00116                 if (!S3D::fileExists(S3D::getDataFile(meshName)))
00117                 {
00118                         return modelNode->returnError(
00119                                 S3D::formatStringBuffer(
00120                                         "Mesh file \"%s\" does not exist",
00121                                         meshName));
00122                 }
00123 
00124                 meshName_ = meshName;
00125         }
00126         else if (strcmp(typeNode->getContent(), "Tree") == 0)
00127         {
00128                 std::string meshName;
00129                 bool burnt, snow;
00130                 if (!modelNode->getNamedChild("type", meshName)) return false;
00131                 if (!modelNode->getNamedChild("snow", snow)) return false;
00132                 if (!modelNode->getNamedChild("burnt", burnt)) return false;
00133 
00134                 TreeModelFactory::TreeType normalType, burntType;
00135                 if (!TreeModelFactory::getTypes(meshName.c_str(), true, 
00136                         normalType, burntType))
00137                 {
00138                         return modelNode->returnError(
00139                                 S3D::formatStringBuffer(
00140                                         "Tree type \"%s\" does not exist",
00141                                         meshName.c_str()));
00142                 }
00143 
00144                 skinName_ = S3D::formatStringBuffer("%s", (snow?"S":"N"));
00145                 meshName_ = S3D::formatStringBuffer("%s:%s", (burnt?"B":"N"), meshName.c_str());
00146         }
00147         else
00148         {
00149                 return modelNode->returnError(
00150                         S3D::formatStringBuffer(
00151                                 "Unknown mesh type \"%s\"",
00152                                 typeNode->getContent()));
00153         }
00154 
00155         return true;
00156 }
00157 
00158 bool ModelID::writeModelID(NetBuffer &buffer)
00159 {
00160         buffer.addToBuffer(type_);
00161         buffer.addToBuffer(meshName_);
00162         buffer.addToBuffer(skinName_);
00163         return true;
00164 }
00165 
00166 bool ModelID::readModelID(NetBufferReader &reader)
00167 {
00168         if (!reader.getFromBuffer(type_)) return false;
00169         if (!reader.getFromBuffer(meshName_)) return false;
00170         if (!reader.getFromBuffer(skinName_)) return false;
00171         return true;    
00172 }
00173 
00174 const char *ModelID::getStringHash()
00175 { 
00176         hash_ = meshName_ + "-" + skinName_;
00177         return hash_.c_str(); 
00178 }

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