示例#1
0
文件: TileSet.cpp 项目: lukadriel/STP
TileSet::TileSet(unsigned int firstgid, const std::string& name, unsigned int tilewidth,
                 unsigned int tileheight, tmx::Image image, unsigned int spacing,
                 unsigned int margin, sf::Vector2i tileoffset) :
        firstgid_(firstgid),
        name_(name),
        tilewidth_(tilewidth),
        tileheight_(tileheight),
        spacing_(spacing),
        margin_(margin),
        width_no_spacing_(0),
        height_no_spacing_(0),
        image_(image),
        tileoffset_(tileoffset) {
    unsigned int width_no_margin = image_.GetWidth() - (margin_ * 2);
    unsigned int height_no_margin =  image_.GetHeight() - (margin_ * 2);
    if (spacing != 0) {
        for (unsigned int i = 0; i <= width_no_margin;) {
            i += tilewidth + spacing_;
            width_no_spacing_ += tilewidth;
        }
        for (unsigned int i = 0; i <= height_no_margin;) {
            i += tileheight + spacing_;
            height_no_spacing_ += tileheight;
        }
    } else {
        width_no_spacing_ = width_no_margin;
        height_no_spacing_ = height_no_margin;
    }
    lastgid_ = firstgid + (width_no_spacing_ / tilewidth) * (height_no_spacing_ / tileheight) - 1;

    // Add each tile to the TileSet
    unsigned int tile_amount = lastgid_ - firstgid_ + 1;
    sf::Vector2u cont;
    sf::Vector2u tile_pos;
    tiles_.reserve(tile_amount);
    for (unsigned int i = 0; i < tile_amount; ++i) {
        tile_pos.x = (cont.x * tilewidth_) + (spacing_ * cont.x) + margin_;
        tile_pos.y = (cont.y * tileheight_) + (spacing_ * cont.y) + margin_;
        sf::IntRect texture_rect(tile_pos.x, tile_pos.y, tilewidth_, tileheight_);

        tiles_.push_back(tmx::TileSet::Tile(i, texture_rect, this));

        cont.x = (cont.x + 1) % (width_no_spacing_ / tilewidth_);
        if (cont.x == 0) cont.y += 1;
    }
}
示例#2
0
Primitive::Ptr Renderer::CreateText( const sf::Text& text, sf::Color background_color_hint ) {
	Primitive::Ptr primitive( new Primitive );

	const sf::Font& font = text.getFont();
	unsigned int character_size = text.getCharacterSize();
	sf::Color color = text.getColor();

	if( m_preblend ) {
		color = sf::Color::White;
	}

	sf::Vector2f atlas_offset = LoadFont( font, character_size, background_color_hint, text.getColor() );

	const sf::String& str = text.getString();
	std::size_t length = str.getSize();

	float horizontal_spacing = static_cast<float>( font.getGlyph( L' ', character_size, false ).advance );
	float vertical_spacing = static_cast<float>( font.getLineSpacing( character_size ) );
	sf::Vector2f position( std::floor( text.getPosition().x + .5f ), std::floor( text.getPosition().y + static_cast<float>( character_size ) + .5f ) );

	const static float tab_spaces = 2.f;

	sf::Uint32 previous_character = 0;

	for( std::size_t index = 0; index < length; ++index ) {
		sf::Uint32 current_character = str[index];

		position.x += static_cast<float>( font.getKerning( previous_character, current_character, character_size ) );

		switch( current_character ) {
			case L' ':
				position.x += horizontal_spacing;
				continue;
			case L'\t':
				position.x += horizontal_spacing * tab_spaces;
				continue;
			case L'\n':
				position.y += vertical_spacing;
				position.x = 0.f;
				continue;
			case L'\v':
				position.y += vertical_spacing * tab_spaces;
				continue;
			default:
				break;
		}

		const sf::Glyph& glyph = font.getGlyph( current_character, character_size, false );

		Primitive::Vertex vertex0;
		Primitive::Vertex vertex1;
		Primitive::Vertex vertex2;
		Primitive::Vertex vertex3;

		vertex0.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top ) );
		vertex1.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) );
		vertex2.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top ) );
		vertex3.position = position + sf::Vector2f( static_cast<float>( glyph.bounds.left + glyph.bounds.width ), static_cast<float>( glyph.bounds.top + glyph.bounds.height ) );

		vertex0.color = color;
		vertex1.color = color;
		vertex2.color = color;
		vertex3.color = color;

		// Let SFML cast the Rect for us.
		sf::FloatRect texture_rect( glyph.textureRect );

		vertex0.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top );
		vertex1.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left, texture_rect.top + texture_rect.height );
		vertex2.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top );
		vertex3.texture_coordinate = atlas_offset + sf::Vector2f( texture_rect.left + texture_rect.width, texture_rect.top + texture_rect.height );

		primitive->AddVertex( vertex0 );
		primitive->AddVertex( vertex1 );
		primitive->AddVertex( vertex2 );
		primitive->AddVertex( vertex2 );
		primitive->AddVertex( vertex1 );
		primitive->AddVertex( vertex3 );

		position.x += static_cast<float>( glyph.advance );

		previous_character = current_character;
	}

	AddPrimitive( primitive );

	return primitive;
}