void TMXMapImporter::buildImageLayer(Game *game, ImageLayerInfo *ili, int idOffset) { TiledLayer *imageLayerToAdd = new TiledLayer( 1, 1, ili->imagewidth, ili->imageheight, 0, ili->collidable, largestLayerWidth, largestLayerHeight); game->getGSM()->getWorld()->addLayer(imageLayerToAdd); TextureManager *textureManager = game->getGraphics()->getWorldTextureManager(); Tile *imageTile = new Tile(); imageTile->collidable = ili->collidable; wstring imageSourceW(ili->imageSource.begin(), ili->imageSource.end()); imageTile->textureID = textureManager->loadTexture(dir + imageSourceW) + idOffset - 1; imageLayerToAdd->addTile(imageTile); }
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; }