Exemple #1
0
void XML_Helpers::handle_min_max_tag(XML *xml, int32_t &min, int32_t &max)
{
	min = 0;
	max = 0;

	XML *min_xml = xml->find("min");
	XML *max_xml = xml->find("max");

	if (min_xml == 0 || max_xml == 0) {
		return;
	}

	XML *x = min_xml->find("rand");
	if (x == 0) {
		min = handle_numeric_tag(min_xml);
	}
	else {
		x = XML_Helpers::handle_rand_tag(x);
		min = atoi(x->get_value().c_str());
	}

	x = max_xml->find("rand");
	if (x == 0) {
		max = handle_numeric_tag(max_xml);
	}
	else {
		x = XML_Helpers::handle_rand_tag(x);
		max = atoi(x->get_value().c_str());
	}
}
void Sprite::load(std::string xml_filename, std::string image_directory, bool absolute_path)
{
	if (absolute_path == false) {
		xml_filename = "sprites/" + xml_filename;
		image_directory = "sprites/" + image_directory;
	}

	this->xml_filename = xml_filename;
	this->image_directory = image_directory;

	XML *xml = new XML(xml_filename);

	std::list<XML *> nodes = xml->get_nodes();
	std::list<XML *>::iterator it;

	bool first = true;

	for (it = nodes.begin(); it != nodes.end(); it++) {
		XML *anim = *it;
		int count;
		std::vector<Image *> images;
		for (count = 0; count < 1024 /* NOTE: hardcoded max frames */; count++) {
			std::string filename = image_directory + "/" + anim->get_name() + itos(count) + ".tga";
			Image *image;
			try {
				image = new Image(filename, true);
			}
			catch (Error e) {
				break;
			}
			images.push_back(image);
		}
		XML *loop_xml = anim->find("loop");
		bool looping;
		if (loop_xml && loop_xml->get_value() == "false") {
			looping = false;
		}
		else {
			looping = true;
		}
		XML *delays = anim->find("delays");
		std::vector<Uint32> delays_vector;
		if (delays == 0) {
			XML *delay = anim->find("delay");
			if  (delay == 0) {
				for (int i = 0; i < count; i++) {
					delays_vector.push_back(100);
				}				
			}
			else {
				Uint32 delay_int = atoi(delay->get_value().c_str());
				for (int i = 0; i < count; i++) {
					delays_vector.push_back(delay_int);
				}
			}
		}
		else {
			for (int i = 0; i < count; i++) {
				XML *delay = delays->find(itos(i));
				if (delay == 0) {
					delays_vector.push_back(100);
				}
				else {
					delays_vector.push_back(atoi(delay->get_value().c_str()));
				}
			}
		}
		Uint32 total_delays = 0;
		for (size_t i = 0; i < delays_vector.size(); i++) {
			total_delays += delays_vector[i];
		}
		Animation *a;
		if (animations.find(anim->get_name()) == animations.end()) {
			a = new Animation();
			a->images = images;
			a->delays = delays_vector;
			a->total_delays = total_delays;
			a->rand_start = anim->find("rand_start") != 0;
			a->looping = looping;
			animations[anim->get_name()] = a;
		}
		else {
			throw Error("Duplicate animation!");
		}
		if (first) {
			first = false;
			current_animation = anim->get_name();
		}
	}

	delete xml;
}