Example #1
0
void TexturePacker::process_resource(Canvas &canvas, Resource &item_resource, std::vector<Subtexture> &packed_sub_textures, std::map<Texture, std::string> &generated_texture_filenames, int &generated_texture_index, const std::string &image_pathname )
{
	// Found a sprite resource, lets modify its content!
	Resource resource = item_resource;

	// Iterate through all nodes, and remove all previous image tags
	DomElement &element = resource.get_element();
	DomNode cur = element.get_first_child();
	while (!cur.is_null())
	{
		DomNode next = cur.get_next_sibling();
		DomNode::NodeType nodeType = (DomNode::NodeType)cur.get_node_type();

		// Only remove the <image> tag, as we want to keep the other sprite attributes
		if (cur.get_node_name() == "image")
			element.remove_child(cur);

		cur = next;
	}

	// Add new image tag to resource DOM
	std::vector<Subtexture>::size_type index, size;
	size = packed_sub_textures.size();
	for(index = 0; index < size; ++index)
	{
		Subtexture subtexture = packed_sub_textures[index];

		// Try to find out if we already have created a texture-on-disk for this subtexture
		std::string texture_filename;
		Texture2D texture = subtexture.get_texture();
		std::map<Texture, std::string>::iterator it;
		it = generated_texture_filenames.find(texture);
		if(it == generated_texture_filenames.end())
		{
			// Texture not found, generate a filename and dump texture to disk
			texture_filename = string_format("texture%1.png", ++generated_texture_index);
			PNGProvider::save(texture.get_pixeldata(canvas), image_pathname + texture_filename);
			generated_texture_filenames[texture] = texture_filename;
		}
		else
		{
			// Previously dumped textures, lets reuse the filename
			texture_filename = (*it).second;
		}

		// Add <grid> DOM element
		DomElement new_grid_element = element.get_owner_document().create_element("grid");
		new_grid_element.set_attribute("pos", string_format("%1,%2", subtexture.get_geometry().left + last_border_size, subtexture.get_geometry().top + last_border_size));
		new_grid_element.set_attribute("size", string_format("%1,%2", subtexture.get_geometry().get_width()- last_border_size*2, subtexture.get_geometry().get_height()- last_border_size*2));

		// Add <image> DOM element
		DomElement new_image_element = element.get_owner_document().create_element("image");
		new_image_element.set_attribute("file", texture_filename);
		new_image_element.append_child(new_grid_element);

		// Add <image> element under <sprite> element
		element.append_child(new_image_element);
	}
}
Example #2
0
	void GlyphCache::insert_glyph(Canvas &canvas, unsigned int glyph, Subtexture &sub_texture, const Pointf &offset, const Sizef &size, const GlyphMetrics &glyph_metrics)
	{
		auto font_glyph = std::unique_ptr<Font_TextureGlyph>(new Font_TextureGlyph());

		font_glyph->glyph = glyph;
		font_glyph->offset = offset;
		font_glyph->metrics = glyph_metrics;
		font_glyph->size = size;

		if ((sub_texture.get_geometry().get_width() > 0) && (sub_texture.get_geometry().get_height() > 0))
		{
			font_glyph->texture = sub_texture.get_texture();
			font_glyph->geometry = sub_texture.get_geometry();
		}

		glyph_list.push_back(std::move(font_glyph));
	}
Example #3
0
HSVSprite::HSVSprite(Canvas &canvas, HSVSpriteBatch *batcher, const std::string &image_filename)
: batcher(batcher)
{
	PixelBuffer image = ImageProviderFactory::load(image_filename);
	Subtexture subtexture = batcher->alloc_sprite(canvas, image.get_size());
	geometry = subtexture.get_geometry();
	texture = subtexture.get_texture();
	texture.set_subimage(canvas, geometry.get_top_left(), image, image.get_size());
}
Example #4
0
void GlyphCache::insert_glyph(GraphicContext &gc, unsigned int glyph, Subtexture &sub_texture, const Point &offset, const Point &increment)
{
	// Search for duplicated glyph's, if found silently ignore them
	std::vector< Font_TextureGlyph * >::size_type size = glyph_list.size();
	for (int cnt=0; cnt<size; cnt++)
	{
		if (glyph_list[cnt]->glyph == glyph)
			return;
	}

	Font_TextureGlyph *font_glyph = new Font_TextureGlyph();
	
	glyph_list.push_back(font_glyph);
	font_glyph->glyph = glyph;
	font_glyph->offset = offset;
	font_glyph->increment = increment;

	if ( (sub_texture.get_geometry().get_width() > 0 ) && (sub_texture.get_geometry().get_height() > 0) )
	{
		font_glyph->texture = sub_texture.get_texture();
		font_glyph->geometry = sub_texture.get_geometry();
	}

}