bool Sprite::load(const std::string& filename) { TiXmlDocument doc; if(!doc.PHYSFS_LoadFile(filename)) { std::cout << "Couldn't load sprite \"" << filename << doc.Error() << std::endl; return false; } if(!doc.RootElement() || doc.RootElement()->ValueStr() != "sprite") { std::cout << "Couldn't load sprite \"" << filename << "\": Invalid XML-Format" << std::endl; return false; } TiXmlElement* animation = doc.RootElement()->FirstChildElement("animation"); while(animation) { if(std::string(animation->Attribute("name")).size()) { m_animations[animation->Attribute("name")] = new SpriteAnimation(animation); } else { std::cout << "\"" << filename << "\": <animation> missing name attribute" << std::endl; } animation = animation->NextSiblingElement("animation"); } if(m_animations.size()) { m_currentAnimation = m_animations.begin()->second; } return true; }
void NewGameDialog::showCampaign(const std::string& file) { TiXmlDocument xmldata; if (xmldata.PHYSFS_LoadFile(file)) if (xmldata.RootElement()->ValueStr() == "mission") { TiXmlElement* element = xmldata.RootElement()->FirstChildElement("info"); if (element) { element = element->FirstChildElement("description"); if (element) m_missionViewer->setText(element->FirstChild()->ValueStr()); } } }
Island::Island(const std::string& filename, int pos_x, int pos_y, std::vector<bool>& waterTiles) : m_x(pos_x), m_y(pos_y), m_tiles(NULL), m_defaultTile(0) { m_defaultTile = Tile::getDefaultTile(); std::cout << "Trying to load island \"" << filename << "\"" << std::endl; TiXmlDocument document; if (!document.PHYSFS_LoadFile(filename)) { throw FileLoadException(filename, document.ErrorDesc()); } if(!document.RootElement() || document.RootElement()->ValueStr() != "island") throw XMLException(filename, -1, "This is no valid island XML file (missing island root element)"); TiXmlElement *island = document.RootElement(); if (!island->Attribute("cols")) throw XMLException(filename, island->Row(), "Missing attribute cols"); if (!island->Attribute("rows")) throw XMLException(filename, island->Row(), "Missing attribute rows"); if (!island->Attribute("clime")) throw XMLException(filename, island->Row(), "Missing attribute clime"); std::stringstream attr; attr << island->Attribute("cols") << " " << island->Attribute("rows"); attr >> m_cols >> m_rows; m_clime = island->Attribute("clime"); std::cout << "Creating " << (m_rows * m_cols) << " tiles" << std::endl; m_tiles = new Tile* [m_rows * m_cols]; memset(m_tiles, 0, sizeof(Tile*) * m_rows * m_cols); TiXmlElement *terrain = island->FirstChildElement("terrain"); if (!terrain) throw XMLException(filename, island->Row(), "Missing toplevel element terrain"); TiXmlElement *row = terrain->FirstChildElement("row"); if (!terrain) throw XMLException(filename, terrain->Row(), "Missing row subelements"); while (row) { if (!row->Attribute("value")) throw XMLException(filename, row->Row(), "Missing attribute value"); std::stringstream rowStr(row->Attribute("value")); int rowInt; rowStr >> rowInt; rowInt--; TiXmlElement *col = row->FirstChildElement("col"); if (!col) throw XMLException(filename, row->Row(), "Missing col subelements"); while (col) { if (!col->Attribute("value")) throw XMLException(filename, col->Row(), "Missing attribute value"); std::stringstream colStr(col->Attribute("value")); int colInt; colStr >> colInt; colInt--; TiXmlElement *tile = col->FirstChildElement("tile"); if (!tile) throw XMLException(filename, col->Row(), "Missing tile subelement"); if (((rowInt * m_cols) + colInt) >= m_cols * m_rows) std::cout << "WARNING! Index out of bounds. Row: " << rowInt << ", column: " << colInt << std::endl; else { m_tiles[(rowInt * m_cols) + colInt] = new Tile(tile); if (m_tiles[(rowInt * m_cols) + colInt] == NULL) { std::cout << "TILE CREATION FAILED" << std::endl; } waterTiles[(rowInt * m_cols) + colInt] = false; } col = col->NextSiblingElement("col"); } row = row->NextSiblingElement("row"); } std::cout << "Succesfully loaded island \"" << filename << "\"" << std::endl; std::cout << "\tColums: " << m_cols << std::endl; std::cout << "\tRows: " << m_rows << std::endl; /* std::cout << "debug-listing 0,0 to 9,9" << std::endl; for (int y = 0; y < 10; y++) { for (int x = 0; x < 10; x++) { std::cout << m_tiles[(y * m_cols) + x]->getName(); std::cout << ","; } std::cout << std::endl; }*/ }