// load keyframes from a brlan file
u32 Animator::LoadAnimators(const RLAN_Header *header, Layout& layout )
{

	gprintf( "don't call this  %s %u\n", __FILE__, __LINE__ );
	u32 frame_count = 0;

	if (header->magic != MAGIC_ANIMATION || header->endian != 0xFEFF || header->version != 0x0008)
		return 0;	// bad header

	// first section
	const u8 *position = ((const u8 *) header) + header->offset;

	for(u32 i = 0; i < header->section_count; ++i)
	{
		section_t *section = (section_t *) position;
		position += section->size;

		if (section->magic == MAGIC_PANE_ANIMATION_INFO)
		{
			const PAI1_Header *pai = (const PAI1_Header *) section;
			u16 animator_count = pai->animator_count;
			frame_count += pai->frame_count;

			// read animation file names
			/*const u32 *nameOffsets = (const u32 *)(pai + 1);
			for(u32 i = 0; i < pai->file_count; i++)
			{
				const char* name = (((const char *) nameOffsets) + nameOffsets[i]);
				layout.AddPalette(name );
			}*/

			const u32 *offsets = (const u32 *) (((const u8 *)section) + pai->entry_offset);
			// read each animator
			for(u32 n = 0; n < animator_count; n++)
			{
				const AnimatorHeader *animHdr = (const AnimatorHeader *) (((const u8 *)section) + offsets[n]);
				std::string anim_name(animHdr->name, 0, 20);

				Animator* animator = animHdr->is_material ?
						(Animator*) layout.FindMaterial(anim_name) :
						(Animator*) layout.FindPane(anim_name);

				if (animator)
					animator->LoadKeyFrames((const u8 *) animHdr, animHdr->tag_count, sizeof(AnimatorHeader) );
			}
		}
		else
		{
			gprintf("Unknown: %c%c%c%c\n", position[0], position[1], position[2], position[3]);
		}
	}

	return frame_count;
}
void EntityFactory::LoadUnits(const char* filename)
{
	TiXmlDocument doc;
	if (!doc.LoadFile(filename))
	{
		DIE("can't open units definition %s\n%s", filename, doc.ErrorDesc());
	}
	TiXmlHandle handle(&doc);

	const char* p = NULL;
	MediaManager& media = MediaManager::GetInstance();

	TiXmlElement* elem = handle.FirstChildElement().FirstChildElement().Element();
	while (elem != NULL)
	{
		// unit id
		int id;
		if (elem->QueryIntAttribute("id", &id) != TIXML_SUCCESS)
		{
			DIE("unit id is missing");
		}
		UnitPattern* unit = &units_[id];

		// name
		p = elem->Attribute("name");
		if (p == NULL)
		{
			printf("warning: unit '%d' doesn't have 'name' attribute\n", id);
			p = DEFAULT_NAME;
		}
		unit->name = p;

		// health points
		int hp;
		if (elem->QueryIntAttribute("hp", &hp) != TIXML_SUCCESS)
		{
			printf("warning: unit '%d' doesn't have 'hp' attribute\n", id);
			hp = DEFAULT_HP;
		}
		unit->hp = hp;

		// speed
		int speed;
		if (elem->QueryIntAttribute("speed", &speed) != TIXML_SUCCESS)
		{
			printf("warning: unit '%d' doesn't have 'speed' attribute\n", id);
			speed = DEFAULT_SPEED;
		}
		unit->speed = speed;

		// animations
		p = elem->Attribute("animation");
		if (p == NULL)
		{
			printf("warning: unit '%d' doesn't have 'animation' attribute\n", id);
			p = DEFAULT_ANIMATION;
		}
		unit->image = &media.GetImage(p);

		std::string anim_name(p);
		anim_name += "_walk_up";
		unit->anim[Entity::UP] = &media.GetAnimation(anim_name.c_str());

		anim_name = p;
		anim_name += "_walk_down";
		unit->anim[Entity::DOWN] = &media.GetAnimation(anim_name.c_str());

		anim_name = p;
		anim_name += "_walk_left";
		unit->anim[Entity::LEFT] = &media.GetAnimation(anim_name.c_str());

		anim_name = p;
		anim_name += "_walk_right";
		unit->anim[Entity::RIGHT] = &media.GetAnimation(anim_name.c_str());

		// weapon (optional)
		p = elem->Attribute("weapon");
		// TODO: utiliser des strings pour identifier les objets, vérifier que la string est valide, ou NULL
		if (p != NULL)
		{
			unit->item = p;
		}
		else
		{
			unit->item.clear();
		}

		elem = elem->NextSiblingElement();
	}
}