bool TMXMapImporter::buildWorldFromInfo(Game *game) { TextureManager *worldTextureManager = game->getGraphics()->getWorldTextureManager(); if (mapType == MapType::ORTHOGONAL_MAP) { World *world = game->getGSM()->getWorld(); int largestLayerWidth = 0; int largestLayerHeight = 0; // LET'S FIRST FIGURE OUT THE WORLD WIDTH AND HEIGHT // FIRST THE IMAGE LAYERS map<string, ImageLayerInfo>::const_iterator iliIt = imageLayerInfos.begin(); while (iliIt != imageLayerInfos.end()) { string key = iliIt->first; ImageLayerInfo ili = imageLayerInfos[key]; if (ili.imagewidth > largestLayerWidth) largestLayerWidth = ili.imagewidth; if (ili.imageheight > largestLayerHeight) largestLayerHeight = ili.imageheight; iliIt++; } // AND THE TILED LAYERS map<string, TiledLayerInfo>::const_iterator tliIt = tiledLayerInfos.begin(); while (tliIt != tiledLayerInfos.end()) { string key = tliIt->first; TiledLayerInfo tli = tiledLayerInfos[key]; int layerWidth = tli.width * tli.tileSetInfo->tilewidth; if (layerWidth > largestLayerWidth) largestLayerWidth = layerWidth; int layerHeight = tli.height * tli.tileSetInfo->tileheight; if (layerHeight > largestLayerHeight) largestLayerHeight = layerHeight; tliIt++; } unsigned int idOffset = worldTextureManager->getWStringTable()->getNumWStringsInTable(); // FIRST LOAD ALL THE TILE SETS map<string, TileSetInfo>::const_iterator tsiIt = tileSetInfos.begin(); while (tsiIt != tileSetInfos.end()) { string key = tsiIt->first; TileSetInfo tsi = tileSetInfos[key]; wstring sourceImageW(tsi.sourceImage.begin(), tsi.sourceImage.end()); bool success = worldTextureManager->loadTileSetFromTexture(game, dir, sourceImageW, tsi.tilewidth, tsi.tileheight); if (!success) return false; tsiIt++; } // NOW LOAD THE IMAGE LAYERS, IF THERE ARE ANY iliIt = imageLayerInfos.begin(); while (iliIt != imageLayerInfos.end()) { string key = iliIt->first; ImageLayerInfo ili = imageLayerInfos[key]; TiledLayer *imageLayerToAdd = new TiledLayer( 1, 1, ili.imagewidth, ili.imageheight, 0, ili.collidable, largestLayerWidth, largestLayerHeight); world->addLayer(imageLayerToAdd); Tile *imageTile = new Tile(); imageTile->collidable = ili.collidable; wstring imageSourceW(ili.imageSource.begin(), ili.imageSource.end()); imageTile->textureID = worldTextureManager->loadTexture(dir + imageSourceW); imageLayerToAdd->addTile(imageTile); iliIt++; } // AND NOW LOAD THE TILED LAYERS, WHICH REFERENCE THE TILE SETS tliIt = tiledLayerInfos.begin(); while (tliIt != tiledLayerInfos.end()) { // @TODO WE'LL NEED TO CUSTOMIZE THIS bool collidableLayer = false; string key = tliIt->first; TiledLayerInfo tli = tiledLayerInfos[key]; TiledLayer *tiledLayerToAdd = new TiledLayer( tli.width, tli.height, tli.tileSetInfo->tilewidth, tli.tileSetInfo->tileheight, 0, tli.collidable, largestLayerWidth, largestLayerHeight); world->addLayer(tiledLayerToAdd); // WE HAVE TO ADD ALL THE TILES int row = 0; int col = 0; int uncollidableIndex = tli.tileSetInfo->firstgid; for (unsigned int i = 0; i < tli.gids.size(); i++) { Tile *tileToAdd = new Tile(); tileToAdd->textureID = tli.gids[i] + idOffset - 1; if (tli.gids[i] == uncollidableIndex) tileToAdd->collidable = false; else { tileToAdd->collidable = tli.collidable; } tiledLayerToAdd->addTile(tileToAdd); } tliIt++; } // AND MAKE THE WORLD DIMENSIONS THE // SIZE OF THE LARGEST LAYER world->setWorldWidth(largestLayerWidth); world->setWorldHeight(largestLayerHeight); } return true; }
bool TMXMapImporter::buildWorldFromInfo(Game *game) { TextureManager *worldTextureManager = game->getGraphics()->getWorldTextureManager(); if (mapType == MapType::ORTHOGONAL_MAP) { World *world = game->getGSM()->getWorld(); // LET'S FIRST FIGURE OUT THE WORLD WIDTH AND HEIGHT calculateWorldDimensions(); // AND MAKE THE WORLD DIMENSIONS THE // SIZE OF THE LARGEST LAYER world->setWorldWidth(largestLayerWidth); world->setWorldHeight(largestLayerHeight); // IT'S POSSIBLE THE TEXTURE MANAGER ALREADY HAS SOME // IMAGES, SO WE NEED TO KNOW HOW MANY IMAGES ARE ALREADY // THER TO OFFSET THE IDS OF ALL IMAGE REFERENCES unsigned int idOffset = worldTextureManager->getWStringTable()->getNumWStringsInTable(); // NOW LOAD THE TILE SETS IN THE ORDER THEY WERE LISTED // INSIDE TMX FILE list<TileSetInfo>::iterator tsiIt = tileSetInfos.begin(); while (tsiIt != tileSetInfos.end()) { TileSetInfo tsi = (*tsiIt); wstring sourceImageW(tsi.sourceImage.begin(), tsi.sourceImage.end()); bool success = worldTextureManager->loadTileSetFromTexture(game, dir, sourceImageW, tsi.tilewidth, tsi.tileheight, tsi.spacing, tsi.margin); if (!success) return false; tsiIt++; } // NOW LOAD ALL THE LAYERS list<LayerInfo*>::iterator it = layerInfos.begin(); while (it != layerInfos.end()) { LayerInfo *layer = (*it); if (layer->type == LayerType::IMAGE) { ImageLayerInfo *ili = (ImageLayerInfo*)layer; buildImageLayer(game, ili, idOffset); } else if (layer->type == LayerType::TILED) { TiledLayerInfo *tli = (TiledLayerInfo*)layer; buildTiledLayer(game, tli, idOffset); } else if (layer->type == LayerType::SPARSE) { SparseLayerInfo *sli = (SparseLayerInfo*)layer; buildSparseLayer(game, sli, idOffset); } // ON TO THE NEXT LAYER it++; } } if (mapType==MapType::ISOMETRIC_MAP){ World *world = game->getGSM()->getWorld(); // LET'S FIRST FIGURE OUT THE WORLD WIDTH AND HEIGHT calculateWorldDimensions(); // AND MAKE THE WORLD DIMENSIONS THE // SIZE OF THE LARGEST LAYER world->setWorldWidth(largestLayerWidth); world->setWorldHeight(largestLayerHeight); // IT'S POSSIBLE THE TEXTURE MANAGER ALREADY HAS SOME // IMAGES, SO WE NEED TO KNOW HOW MANY IMAGES ARE ALREADY // THER TO OFFSET THE IDS OF ALL IMAGE REFERENCES unsigned int idOffset = worldTextureManager->getWStringTable()->getNumWStringsInTable(); // NOW LOAD THE TILE SETS IN THE ORDER THEY WERE LISTED // INSIDE TMX FILE list<TileSetInfo>::iterator tsiIt = tileSetInfos.begin(); while (tsiIt != tileSetInfos.end()) { TileSetInfo tsi = (*tsiIt); wstring sourceImageW(tsi.sourceImage.begin(), tsi.sourceImage.end()); bool success = worldTextureManager->loadTileSetFromTexture(game, dir, sourceImageW, tsi.tilewidth, tsi.tileheight, tsi.spacing, tsi.margin); if (!success) return false; tsiIt++; } // NOW LOAD ALL THE LAYERS list<LayerInfo*>::iterator it = layerInfos.begin(); while (it != layerInfos.end()) { LayerInfo *layer = (*it); if (layer->type == LayerType::IMAGE) { ImageLayerInfo *ili = (ImageLayerInfo*)layer; buildImageLayer(game, ili, idOffset); } else if (layer->type == LayerType::TILED) { TiledLayerInfo *tli = (TiledLayerInfo*)layer; buildTiledLayer(game, tli, idOffset); } else if (layer->type == LayerType::SPARSE) { SparseLayerInfo *sli = (SparseLayerInfo*)layer; buildSparseLayer(game, sli, idOffset); } // ON TO THE NEXT LAYER it++; } } return true; }