Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

/Users/blackie/Documents/myRepository/phobosengine-vc2005/phobosengine/phobosengine/TextureResourceFile.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include "TextureResourceFile.h"
00004 
00005 #include "Configuration.h"
00006 #include "Renderer.h"
00007 #include "Utils.h"
00008 #include "XMLDocument.h"
00009 #include "XMLNode.h"
00010 
00011 
00012 namespace pge {
00013 
00014         //****************************************************************************
00015         //
00016         //
00017         //
00018         //****************************************************************************
00019         TextureResourceFile::TextureResourceFile(const std::string &filename) {
00020                 m_resources = std::vector<TextureResource>();
00021                 m_document = new xml::CXMLDocument(filename);
00022 
00023                 if(m_document != NULL) {
00024                         if(parseContent()) {
00025                                 printf("INFO: [%s] TextureResourceFile loaded.\n", __FILE__);
00026                         } else {
00027                                 printf("ERROR: [%s] TextureResourceFile could not be loaded.\n", __FILE__);
00028                         }
00029                 }
00030         }
00031 
00032 
00033         //****************************************************************************
00034         //
00035         //
00036         //
00037         //****************************************************************************
00038         TextureResourceFile::~TextureResourceFile(void) {
00039                 if(m_document != NULL) {
00040                         delete m_document;
00041                         m_document = NULL;
00042                 }
00043         }
00044 
00045 
00046         //****************************************************************************
00047         //
00048         //
00049         //
00050         //****************************************************************************
00051         bool TextureResourceFile::hasResource(const std::string &filename) {
00052                 std::vector<TextureResource>::iterator it;
00053 
00054 
00055                 for(it = m_resources.begin(); it != m_resources.end(); it++) {
00056                         TextureResource res = *it;
00057                         if(res.m_filename == filename) {
00058                                 return true;
00059                         }
00060                 }
00061                 return false;
00062         }
00063 
00064 
00065         //****************************************************************************
00066         //
00067         //
00068         //
00069         //****************************************************************************
00070         TextureResourceFile::TextureResource TextureResourceFile::getResource(const std::string &filename) {
00071                 std::vector<TextureResource>::iterator it;
00072 
00073 
00074                 for(it = m_resources.begin(); it != m_resources.end(); it++) {
00075                         TextureResource res =*it;
00076                         if(res.m_filename == filename) {
00077                                 return res;
00078                         }
00079                 }
00080                 return TextureResource();
00081         }
00082 
00083 
00084         //****************************************************************************
00085         //
00086         //
00087         //
00088         //****************************************************************************
00089         bool TextureResourceFile::parseContent(void) {
00090                 xml::CXMLNodeSet *set = NULL;
00091                 int i;
00092                 bool success = true;
00093 
00094 
00095                 if(m_document->isFileOpen()) {
00096                         set = m_document->evaluateXPathExpression("/textures/texture", NULL);
00097                         if(set != NULL) {
00098 
00099                                 for(i = 0; i < set->getNodeNum() && success; i++) {
00100                                         xml::CXMLNode node;
00101 
00102                                         if(set->getNodeAt(i, &node)) {
00103                                                 if(!parseTexture(&node)) {
00104                                                         success = false;
00105                                                 }
00106                                         } else {
00107                                                 success = false;
00108                                         }
00109                                 }
00110 
00111                                 delete set;
00112                                 set = NULL;
00113                         }
00114                         delete m_document;
00115                         m_document = NULL;
00116                 }
00117 
00118                 return success;
00119         }
00120 
00121 
00122         //****************************************************************************
00123         //
00124         //
00125         //
00126         //****************************************************************************
00127         bool TextureResourceFile::parseTexture(xml::CXMLNode *textureNode) {
00128                 TextureResource res;
00129                 xml::CXMLNode firstChild;
00130                 xml::CXMLNode sibling;
00131                 xml::CXMLNode temp;
00132                 std::string name;
00133                 std::string str;
00134 
00135 
00136                 // The first child must be the filename node, else return failure.
00137                 if(textureNode->getFirstChildNode(&firstChild)) {
00138                         if(firstChild.getName(&name)) {
00139                                 if(name != "filename") {
00140                                         return false;
00141                                 } else {
00142                                         if(firstChild.getText(&str)) {
00143                                                 res.m_filename = std::string(str);
00144                                         } else {
00145                                                 return false;
00146                                         }
00147                                 }
00148                         }
00149 
00150                         // Now go through all the siblings.
00151                         while(firstChild.getNextSiblingNode(&sibling)) {
00152 
00153                                 // Try to get the name of the node.
00154                                 if(sibling.getName(&name)) {
00155 
00156                                         // Now parse the specific nodes.
00157                                         // Alpha node.
00158                                         if(name == "alpha") {
00159                                                 xml::CXMLAttribute attr;
00160 
00161                                                 // Get the attribute of the node.
00162                                                 if(sibling.getAttributeByName("enabled", &attr)) {
00163                                                         // Set alpha enabled value.
00164                                                         if(attr.m_value == "true") {
00165                                                                 res.m_alphaEnabled = true;
00166                                                         } else {
00167                                                                 res.m_alphaEnabled = false;
00168                                                         }
00169                                                 }
00170                                                 // Parse alpha value node.
00171                                                 if(sibling.getFirstChildNode(&temp)) {
00172                                                         if(temp.getName(&str)) {
00173                                                                 if(str == "alphablend") {
00174 
00175                                                                         // Now parse the alpha values.
00176                                                                         if(temp.getAttributeByName("red", &attr)) {
00177                                                                                 res.m_alphaRed = utils::stringToInt(attr.m_value);
00178                                                                         }
00179                                                                         if(temp.getAttributeByName("green", &attr)) {
00180                                                                                 res.m_alphaGreen = utils::stringToInt(attr.m_value);
00181                                                                         }
00182                                                                         if(temp.getAttributeByName("blue", &attr)) {
00183                                                                                 res.m_alphaBlue = utils::stringToInt(attr.m_value);
00184                                                                         }
00185                                                                 }
00186                                                         }
00187                                                 }
00188                                         }
00189 
00190                                         // The filter node.
00191                                         if(name == "filter") {
00192                                                 xml::CXMLAttribute attr;
00193 
00194                                                 // Set standard values at first, the overwrite them if more information is given.
00195                                                 res.m_minFilter = TEXTURE_MINFILTER_MIPMAP;
00196                                                 res.m_magFilter = TEXTURE_MAGFILTER;
00197                                                 res.m_doMipmap = true;
00198 
00199                                                 // Min filter.
00200                                                 if(sibling.getAttributeByName("minfilter", &attr)) {
00201                                                         // Check the filter settings.
00202                                                         if(attr.m_value == "linear") {
00203                                                                 res.m_minFilter = GL_LINEAR;
00204                                                                 res.m_doMipmap = false;
00205                                                         }
00206                                                         if(attr.m_value == "nearest") {
00207                                                                 res.m_minFilter = GL_NEAREST;
00208                                                                 res.m_doMipmap = false;
00209                                                         }
00210                                                         if(attr.m_value == "linear_mipmap_linear") {
00211                                                                 res.m_minFilter = GL_LINEAR_MIPMAP_LINEAR;
00212                                                                 res.m_doMipmap = true;
00213                                                         }
00214                                                         if(attr.m_value == "linear_mipmap_nearest") {
00215                                                                 res.m_minFilter = GL_LINEAR_MIPMAP_NEAREST;
00216                                                                 res.m_doMipmap = true;
00217                                                         }
00218                                                         if(attr.m_value == "nearest_mipmap_linear") {
00219                                                                 res.m_minFilter = GL_NEAREST_MIPMAP_LINEAR;
00220                                                                 res.m_doMipmap = true;
00221                                                         }
00222                                                         if(attr.m_value == "nearest_mipmap_nearest") {
00223                                                                 res.m_minFilter = GL_NEAREST_MIPMAP_NEAREST;
00224                                                                 res.m_doMipmap = true;
00225                                                         }
00226                                                 }
00227 
00228                                                 // Mag filter.
00229                                                 if(sibling.getAttributeByName("magfilter", &attr)) {
00230                                                         // Check the filter settings
00231                                                         if(attr.m_value == "linear") {
00232                                                                 res.m_magFilter = GL_LINEAR;
00233                                                         }
00234                                                         if(attr.m_value == "nearest") {
00235                                                                 res.m_magFilter = GL_NEAREST;
00236                                                         }
00237                                                 }
00238                                         }
00239 
00240                                         // The wrap node.
00241                                         if(name == "wrap") {
00242                                                 xml::CXMLAttribute attr;
00243 
00244                                                 // Default wrap settings.
00245                                                 res.m_wrapS = TEXTURE_WRAPS;
00246                                                 res.m_wrapT = TEXTURE_WRAPT;
00247 
00248                                                 if(sibling.getAttributeByName("s", &attr)) {
00249                                                         if(attr.m_value == "repeat") {
00250                                                                 res.m_wrapS = GL_REPEAT;
00251                                                         }
00252                                                         if(attr.m_value == "clamp") {
00253                                                                 res.m_wrapS = GL_CLAMP;
00254                                                         }
00255                                                 }
00256                                                 if(sibling.getAttributeByName("t", &attr)) {
00257                                                         if(attr.m_value == "repeat") {
00258                                                                 res.m_wrapT = GL_REPEAT;
00259                                                         }
00260                                                         if(attr.m_value == "clamp") {
00261                                                                 res.m_wrapT = GL_CLAMP;
00262                                                         }
00263                                                 }
00264                                         }
00265                                 }
00266                                 firstChild = sibling;
00267                         }
00268                         m_resources.push_back(res);
00269                         return true;
00270                 }
00271                 return false;
00272         }
00273 };

Generated on Mon Oct 16 12:08:11 2006 for Phobosengine by doxygen 1.3.4