void File::open(const std::string& filename, OpenMode mode, bool no_override) { // check that we don't have anything opened! /// \todo maybe we could just close the old file here... but /// then, this could also lead to errors... assert(mHandle == 0); // open depending on mode if( mode == OPEN_WRITE ) { if(no_override && FileSystem::getSingleton().exists(filename)) { BOOST_THROW_EXCEPTION(FileAlreadyExistsException(filename)); } mHandle = PHYSFS_openWrite(filename.c_str()); } else { mHandle = PHYSFS_openRead(filename.c_str()); } if (!mHandle) { BOOST_THROW_EXCEPTION(FileLoadException(filename)); } mFileName = filename; }
void Tileset::setTexture(TilesetLoadData tileset_data) { sf::Image image; if (!image.loadFromFile(tileset_data.path)) throw FileLoadException(tileset_data.path); name = tileset_data.path; image.createMaskFromColor(MASK_COLOR); texture.loadFromImage(image); items_per_row = image.getSize().x / tileset_data.size.x; tile_size = tileset_data.size; countItems(); }
bool UserConfig::loadFile(const std::string& filename) { PHYSFS_file* fileHandle = PHYSFS_openRead(filename.c_str()); if (!fileHandle) { throw FileLoadException(filename); } int fileLength = PHYSFS_fileLength(fileHandle); char* fileBuffer = new char[fileLength + 1]; PHYSFS_read(fileHandle, fileBuffer, 1, fileLength); fileBuffer[fileLength] = 0; TiXmlDocument configDoc; configDoc.Parse(fileBuffer); delete[] fileBuffer; PHYSFS_close(fileHandle); if (configDoc.Error()) { std::cerr << "Warning: Parse error in " << filename; std::cerr << "!" << std::endl; } TiXmlElement* userConfigElem = configDoc.FirstChildElement("userconfig"); if (userConfigElem == NULL) return false; for (TiXmlElement* varElem = userConfigElem->FirstChildElement("var"); varElem != NULL; varElem = varElem->NextSiblingElement("var")) { std::string name, value; const char* c; c = varElem->Attribute("name"); if (c) name = c; c = varElem->Attribute("value"); if (c) value = c; createVar(name, value); } return true; }
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; }*/ }