| 388 | } |
| 389 | |
| 390 | Primitive::Ptr Renderer::CreateSprite( const sf::FloatRect& rect, PrimitiveTexture::Ptr texture, const sf::FloatRect& subrect, int rotation_turns ) { |
| 391 | auto offset = texture->offset; |
| 392 | |
| 393 | auto primitive = std::make_shared<Primitive>( 4 ); |
| 394 | |
| 395 | PrimitiveVertex vertex0; |
| 396 | PrimitiveVertex vertex1; |
| 397 | PrimitiveVertex vertex2; |
| 398 | PrimitiveVertex vertex3; |
| 399 | |
| 400 | vertex0.position = sf::Vector2f( std::floor( rect.position.x + .5f ), std::floor( rect.position.y + .5f ) ); |
| 401 | vertex1.position = sf::Vector2f( std::floor( rect.position.x + .5f ), std::floor( rect.position.y + .5f ) + std::floor( rect.size.y + .5f ) ); |
| 402 | vertex2.position = sf::Vector2f( std::floor( rect.position.x + .5f ) + std::floor( rect.size.x + .5f ), std::floor( rect.position.y + .5f ) ); |
| 403 | vertex3.position = sf::Vector2f( std::floor( rect.position.x + .5f ) + std::floor( rect.size.x + .5f ), std::floor( rect.position.y + .5f ) + std::floor( rect.size.y + .5f ) ); |
| 404 | |
| 405 | vertex0.color = sf::Color( 255, 255, 255, 255 ); |
| 406 | vertex1.color = sf::Color( 255, 255, 255, 255 ); |
| 407 | vertex2.color = sf::Color( 255, 255, 255, 255 ); |
| 408 | vertex3.color = sf::Color( 255, 255, 255, 255 ); |
| 409 | |
| 410 | sf::Vector2f coords[4]; |
| 411 | |
| 412 | if( ( subrect.position.x != 0.f ) || ( subrect.position.y != 0.f ) || ( subrect.size.x != 0.f ) || ( subrect.size.y != 0.f ) ) { |
| 413 | coords[0] = offset + sf::Vector2f( std::floor( subrect.position.x + .5f ), std::floor( subrect.position.y + .5f ) ); |
| 414 | coords[3] = offset + sf::Vector2f( std::floor( subrect.position.x + .5f ), std::floor( subrect.position.y + .5f ) ) + sf::Vector2f( 0.f, std::floor( subrect.size.y + .5f ) ); |
| 415 | coords[1] = offset + sf::Vector2f( std::floor( subrect.position.x + .5f ), std::floor( subrect.position.y + .5f ) ) + sf::Vector2f( std::floor( subrect.size.x + .5f ), 0.f ); |
| 416 | coords[2] = offset + sf::Vector2f( std::floor( subrect.position.x + .5f ), std::floor( subrect.position.y + .5f ) ) + sf::Vector2f( std::floor( subrect.size.x + .5f ), std::floor( subrect.size.y + .5f ) ); |
| 417 | } |
| 418 | else { |
| 419 | coords[0] = offset + sf::Vector2f( 0.f, 0.f ); |
| 420 | coords[3] = offset + sf::Vector2f( 0.f, static_cast<float>( texture->size.y ) ); |
| 421 | coords[1] = offset + sf::Vector2f( static_cast<float>( texture->size.x ), 0.f ); |
| 422 | coords[2] = offset + sf::Vector2f( static_cast<float>( texture->size.x ), static_cast<float>( texture->size.y ) ); |
| 423 | } |
| 424 | |
| 425 | // Get rotation_turns into the range [0;3]. |
| 426 | rotation_turns %= 4; |
| 427 | |
| 428 | if( rotation_turns < 0 ) { |
| 429 | rotation_turns += 4; |
| 430 | } |
| 431 | |
| 432 | // Perform the circular shift. |
| 433 | if( rotation_turns != 0 ) { |
| 434 | std::rotate( coords, coords + rotation_turns, coords + 4 ); |
| 435 | } |
| 436 | |
| 437 | vertex0.texture_coordinate = coords[0]; |
| 438 | vertex1.texture_coordinate = coords[3]; |
| 439 | vertex2.texture_coordinate = coords[1]; |
| 440 | vertex3.texture_coordinate = coords[2]; |
| 441 | |
| 442 | primitive->AddVertex( vertex0 ); |
| 443 | primitive->AddVertex( vertex1 ); |
| 444 | primitive->AddVertex( vertex2 ); |
| 445 | primitive->AddVertex( vertex2 ); |
| 446 | primitive->AddVertex( vertex1 ); |
| 447 | primitive->AddVertex( vertex3 ); |
no test coverage detected