ErrorCode LoadedMap::loadFile() { Logger.logMessage(LOG_STATE, LOG_MAP, "LoadedMap::loadFile start\n"); int ret = m_doc.LoadFile(m_filename.c_str()); if(ret != 0) { return ERROR_OPENING_FILE; } XMLElement *root_map = m_doc.FirstChildElement(XML_MAP.c_str()); assert(root_map); loadMap(root_map); XMLElement *child = root_map->FirstChildElement(); assert(child); while(child != NULL) { if(child->Name() == XML_TILESET) { loadTileset(child); } else if(child->Name() == XML_LAYER) { loadLayer(child); } else if(child->Name() == XML_OBJECTGROUP) { loadObjectGroup(child); } child = child->NextSiblingElement(); } Logger.logMessage(LOG_STATE, LOG_MAP, "LoadedMap::loadFile end\n"); printMapInformation(); return OK; }
// An old map may be passed in to avoid reloading the images for the tilesets. struct Map *loadMap(const char *path, SDL_Renderer *renderer, struct Map *reuse_images){ // Load the int size; struct Turbo_Value mapValue; struct Map *map = NULL; char *const data = BufferFile(path, &size); if(!data){ fprintf(stderr, "Could not open map file %s\n", path); return NULL; } assert(path); Turbo_Parse(&mapValue, data, data + size); if(mapValue.type == TJ_Error){ Turbo_WriteError(&mapValue, stderr, 0); goto ending; } if(mapValue.type != TJ_Object){ fprintf(stderr, "Invalid map object\n"); goto ending; } // Load the tilesets first. { unsigned i; const struct Turbo_Value *const tilesetArray = jsonFindObjectValue(&mapValue, "tilesets"); if(!tilesetArray){ fprintf(stderr, "Invalid map object: no tilesets\n"); goto ending; } else if(tilesetArray->type == TJ_Error){ Turbo_WriteError(&mapValue, stderr, 0); goto ending; } else if(tilesetArray->type != TJ_Array){ fprintf(stderr, "Invalid tileset array\n"); goto ending; } map = malloc(sizeof(struct Map)); map->numTilesets = tilesetArray->length; map->tilesets = calloc(map->numTilesets, sizeof(struct Tileset)); // Load all tilesets. for(i = 0; i < map->numTilesets; i++){ if(!loadTileset(map->tilesets + i, renderer, tilesetArray->value.array + i, reuse_images)){ // Free what we've loaded so far. unsigned e; for(e = 0; e != i; e++){ unsigned j; // If we reused an existing image, don't touch it. for(j = 0; j < reuse_images->numTilesets; j++){ if(reuse_images->tilesets[j].image == map->tilesets[e].image){ map->tilesets[e].image = NULL; map->tilesets[e].imagePath[0] = '\0'; break; } } destroyTileset(map->tilesets + e); } fprintf(stderr, "Error loading tileset %i\n", i); goto error_free_tilesets_only; } } // Change all reused surfaces to avoid any double-frees or use-after-frees. if(reuse_images){ for(i = 0; i < map->numTilesets; i++){ unsigned e; for(e = 0; e < reuse_images->numTilesets; e++){ if(reuse_images->tilesets[e].image == map->tilesets[i].image){ reuse_images->tilesets[e].image = NULL; reuse_images->tilesets[e].imagePath[0] = '\0'; } } } } } // Load layers { const struct Turbo_Value *const layers = jsonFindObjectValue(&mapValue, "layers"); if(!layers || layers->type != TJ_Array){ // Huh. This is at least a warning. fprintf(stderr, "Warning: No layers in map."); map->numLayers = 0; map->layers = NULL; } else{ unsigned i; map->numLayers = layers->length; map->layers = calloc(layers->length, sizeof(struct Layer)); for(i = 0; i < map->numLayers; i++){ if(!loadLayer(map->layers + i, layers->value.array + i)){ unsigned e; for(e = 0; e < i; e++){ destroyLayer(map->layers + e); } free(map->layers); goto error_free_tilesets; } } } } // All OK! goto ending; error_free_tilesets: { unsigned i; for(i = 0; i < map->numTilesets; i++) destroyTileset(map->tilesets + i); } error_free_tilesets_only: free(map->tilesets); error_free_map: free(map); map = NULL; ending: Turbo_FreeParse(&mapValue); FreeBufferFile(data, size); return map; }
TileMap::TileMap() { loadTileset(); }
void Area::loadTileModels() { loadTileset(); loadTiles(); }
sf::Texture* RenderUtils::getTileset(int tileset) { if (tiles.count(tileset) < 1) { return loadTileset(tileset); } return &tiles[tileset]; }
bool Map::loadMap(const std::string &filename) { std::string file = resourceManager->getDataPath(filename); logger->logDebug("Loading map " + file); TiXmlDocument doc(file.c_str()); bool loaded = doc.LoadFile(); if (!loaded) { logger->logError("Error loading map"); return false; } TiXmlHandle hDoc(&doc); TiXmlElement *e; TiXmlHandle map = hDoc.FirstChild("map"); if (!loadMapInfo(map.ToElement())) { return false; } int numTilesets = 0; while(1) { e = map.Child("tileset", numTilesets).ToElement(); if (!loadTileset(e)) { break; } ++numTilesets; } if (numTilesets == 0) { logger->logError("No tilesets found."); return false; } int numLayers = 0; while(1) { e = map.Child("layer", numLayers).ToElement(); if (!loadLayer(e)) { break; } ++numLayers; } if (numLayers == 0) { logger->logError("No layers found!"); return false; } logger->logDebug("Finished loading map"); mLoaded = true; return true; }