Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SFML Game Development.pdf
Скачиваний:
194
Добавлен:
28.03.2016
Размер:
4.19 Mб
Скачать

Chapter 3

case Aircraft::Raptor: return Textures::Raptor;

}

}

We define this global function at the beginning of the Aircraft.cpp file, so it does not appear in the Aircraft interface. In our constructor, we then initialize our sprite with the texture:

Aircraft::Aircraft(Type type, const TextureHolder& textures): mType(type), mSprite(textures.get(toTextureID(type)))

{

}

Instead of the sf::Sprite constructor which takes const sf::Texture&, it would also have been possible to call the method sf::Sprite::setTexture().

Aligning the origin

By default, the origin of sprites is located in their upper-left corner. When we call setPosition(), we therefore always set the position of the sprite's upper-left corner. For various tasks such as object alignment or rotation, it is however more comfortable to work with the center. Therefore, we have to move the sprite's origin to its center.

For the center, we need the half size of the sprite. With getLocalBounds(), we get the local bounding rectangle (local means not taking any sf::Sprite transforms into account—as opposed to getGlobalBounds()). The rectangle is of type sf::FloatRect, which stores four float variables called left, top, width, and height. SFML also provides sf::IntRect, which stores four int varibles. Our local bounding rectangle's left and top coordinates are zero, while its width and height correspond to the sprite's texture size.

sf::FloatRect bounds = mSprite.getLocalBounds(); mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f);

After all this work, our aircraft is finally drawable! When it is added to a scene graph, and somebody draws it, our plane will appear on the screen.

[ 63 ]

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]