/*
	This method loads all the sprite types found in the provided sprite type list file
	into the game state manager, including their images.
*/
bool PoseurSpriteTypesImporter::loadSpriteTypes(Game *game, wstring spriteTypesListFileName)
{
	int slashIndex = spriteTypesListFileName.rfind('/');
	dir = string(spriteTypesListFileName.begin(), spriteTypesListFileName.end()).substr(0, slashIndex+1);
	const char *spriteTypesListFile = newCharArrayFromWstring(spriteTypesListFileName);
	bool success = loadSpriteTypesListInfo(spriteTypesListFile);
	if (!success) return false;
	for (unsigned int i = 0; i < spriteTypes.size(); i++)
	{
		success = loadSpriteTypeInfo(spriteTypes[i]);
		if (!success) return false;
	}

	TextureManager *tm = game->getGraphics()->getWorldTextureManager();
	WStringTable *wStringTable = tm->getWStringTable();

	// NOW LET'S USE ALL THE INFO WE'VE LOADED 
	// LET'S START BY LOADING THE TEXTURES INTO THE WORLD TEXTURE MANAGER
	for (unsigned int i = 0; i < spriteTypes.size(); i++)
	{
		string spriteType = spriteTypes[i];
		unsigned int offset = wStringTable->getNumWStringsInTable();
		map<int, string> images = spriteTypesImages[spriteType];
		for (int j = 0; j < images.size(); j++)
		{
			string imageToLoad = images[j];
			wstring wImageToLoad(imageToLoad.begin(), imageToLoad.end());
			tm->loadTexture(wImageToLoad);
		}

		AnimatedSpriteType *ast = new AnimatedSpriteType();
		unsigned int spriteTypeId = game->getGSM()->getSpriteManager()->addSpriteType(ast);
		ast->setSpriteTypeID(spriteTypeId);
		Dimensions dim = spriteTypesDimensions[spriteType];
		ast->setTextureSize(dim.width, dim.height);
		
		map<string, vector<Pose>> animations = spriteTypesAnimationsLists[spriteType];
		map<string, vector<Pose>>::iterator it = animations.begin();
		while (it != animations.end())
		{
			string key = it->first;
			wstring wKey(key.begin(), key.end());
			ast->addAnimationSequence(wKey);
			vector<Pose> poseList = it->second;
			vector<Pose>::iterator poseIt = poseList.begin();
			while (poseIt != poseList.end())
			{
				Pose pose = *poseIt;
				ast->addAnimationFrame(wKey, pose.imageId + offset - 1, pose.duration);
				poseIt++;
			}				
			it++;
		}
	}


	return true;
}
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;
}
Exemple #3
0
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;
}