Example #1
0
World* World::CreateWorldFromXml(std::string xmlPath)
{
	XmlReader worldFileReader = XmlReader();
	World* ret = new World();

	worldFileReader.LoadFile(PathLoader::GetPath(xmlPath));

	// Read the <tiles> node
	auto mapNodes = worldFileReader.GetNode("maps")->GetNodes("map");

	auto it = ITBEGIN(mapNodes);
	while (it != ITEND(mapNodes))
	{
		XmlNode* element = *it;

		std::string mapName = element->GetAttribute("name").AttributeValue;
		std::string mapFilepath = element->GetAttribute("source").AttributeValue;

		Map* newMap = Map::CreateFromXml(mapFilepath);

		ret->Maps->Add(newMap);

		it++;
	}

	// Read the <tile_description> node
	auto descriptionNode = worldFileReader.FindNode("tile_description")->GetNodes("tile");
	TileDescriptionList* descList = new TileDescriptionList();
	ret->TileMapping = descList;

	descList->Entries = new PointerList<TileDescriptionEntry*>();

	auto descIt = ITBEGIN(descriptionNode);
	while (descIt != ITEND(descriptionNode))
	{
		XmlNode* element = *descIt;

		TileDescriptionEntry* newEntry = new TileDescriptionEntry();
		newEntry->id = atoi(element->GetAttribute("id").AttributeValue);
		newEntry->TextureName = element->GetAttribute("texture").AttributeValue;

		descList->Entries->Add(newEntry);

		descIt++;
	}

	return ret;
}
PointerList<BaseSprite*>* Spritesheet::ExtractSprites()
{
    assert(this->ConfigFilePath != "");
    assert(this->SpritesheetFilePath != "");
    assert(this->Graphics != NULL);

    PointerList<BaseSprite*>* spriteList = new PointerList<BaseSprite*>();

    XmlReader configReader = XmlReader();
    configReader.LoadFile(this->ConfigFilePath);
    PointerList<XmlNode*>* nodes = configReader.FindNodes("sprite");

    auto it = nodes->GetContainer()->begin();
    while (it != nodes->GetContainer()->end())
    {
        XmlNode* node = (*it);

        std::string spriteName = node->GetAttribute("n").AttributeValue;
        int spriteSubTextureX = std::atoi(node->GetAttribute("x").AttributeValue);
        int spriteSubTextureY = std::atoi(node->GetAttribute("y").AttributeValue);
        int spriteSubTextureW = std::atoi(node->GetAttribute("w").AttributeValue);
        int spriteSubTextureH = std::atoi(node->GetAttribute("h").AttributeValue);

        FRectangle subRec = FRectangle(spriteSubTextureX, spriteSubTextureY, spriteSubTextureW, spriteSubTextureH);

        BaseSprite* newSprite = Graphics->CreateSpriteInstance();
        newSprite->Ident = spriteName;

        BaseTexture* newSpriteTexture = spritesheetTexture->GetSubTexture(subRec);
        newSprite->SetTexture(newSpriteTexture);
        newSpriteTexture->TextureName = spriteName;
        newSpriteTexture->TexturePath = this->SpritesheetFilePath;

        spriteList->Add(newSprite);

        it++;
    }

    nodes->Release();
    delete(nodes);

    return spriteList;
}
Spritesheet::Spritesheet(std::string configFilePath, BaseGraphicEngine* engine)
{
    assert(configFilePath != "");
    assert(engine != NULL);
    
    // Open config file to get the spritesheet image location
    XmlReader configReader = XmlReader();
    configReader.LoadFile(configFilePath);

    XmlNode* node = configReader.FindNode("TextureAtlas");

    std::string path = node->GetAttribute("imagePath").AttributeValue;

    this->spritesheetHeight = std::atoi(node->GetAttribute("height").AttributeValue);
    this->spritesheetWidth = std::atoi(node->GetAttribute("width").AttributeValue);

    this->spritesheetTexture = engine->TextureRepo->LoadFromDisk("assets//" + path); // TODO : Asset root is not prepended in the texture packed config file

    this->Graphics = engine;
    this->SpritesheetFilePath = path;
    this->ConfigFilePath = configFilePath;
}