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

Chapter 3

Zoom and rotation

The sf::View utility gives us another two precious features: zooming and rotating our view.

We can use the sf::View::zoom(float factor) function to easily approach or move away from the center of the view. The factor parameter means that the current view's rectangle will be multiplied by it. It is really that simple. A factor of one will have no effects on the zoom function. A factor bigger than one will grow the view's rectangle, which makes objects appear smaller and gives us the impression to watch from a more distant point of view. The opposite applies when using a factor less than one, which will make the world appear to be closer, just like when we zoom in a real camera.

About rotation, sf::View allows us to turn our view orientation to another angle than the default one, zero, in degrees. The easiest way to visualize the results of this operation is to picture how our world is seen under the default view orientation, and then imagine the whole content rotating relative to the center of the view.

This is a concept that is better understood by experimenting until the desired effect is reached. You can use sf::View::rotate(float degrees) to add a rotation angle

to the current one, or sf::View::setRotation(float degrees) to set the rotation of the view to an absolute value.

Landscape rendering

As you can observe in the C++ sample for this chapter, our aircraft travels continuously over a desert. This continuity can be achieved in many ways and with many different levels of detail and complexity. However, we chose to go in a very simple and yet effective way of doing it, using a feature that SFML provides out of the box.

SpriteNode

In order to display our background sprite through the scene graph, we created a new SceneNode type, the SpriteNode, which acts as a simple sf::Sprite that can be plugged into our tree structure. Conveniently, this is all we need to make our landscape. We only have to create SpriteNode and attach it to our background layer of the scene graph.

[ 71 ]

www.it-ebooks.info

Forge of the Gods – Shaping Our World

To demonstrate the implementation of a new node type, there follows a small snippet of the SpriteNode declaration:

class SpriteNode : public SceneNode

{

public:

 

 

explicit

SpriteNode(const sf::Texture&

texture);

 

SpriteNode(const sf::Texture&

texture,

 

const sf::IntRect&

rect);

private:

 

 

virtual void

drawCurrent(sf::RenderTarget&

target,

sf::RenderStates states) const;

private:

sf::Sprite mSprite;

};

The sf::Sprite class is constructed and prepared at startup and not touched again in the future. This is a proof of the scene graph's power, showing us that the relative transforms work very well because we can manipulate the positioning, rotation, and scale of SpriteNode, and these transforms are inherently applied to the sf::Sprite object as well.

With the sprite node, we have also introduced the last piece in our scene graph for the moment. The following diagram should give you an impression of the current inheritance hierarchy. The grey classes at the top are part of SFML, the black ones are ours.

sf::Drawable sf::Transformable sf::NonCopyable

SceneNode

SpriteNode

 

Entity

 

 

 

 

 

 

 

 

Aircraft

[ 72 ]

www.it-ebooks.info

Chapter 3

Landscape texture

In order to have a good landscape, without wasting too much memory having multiple images to represent it along the whole level, we used a tileable texture.

A tileable texture is no more than an image that can be put together continuously, without letting the player notice that we actually have one single image repeating itself. This is possible because the seam between every two instances of the image is not noticeable. The image's beginning fits perfectly into its ending, creating an illusion of infinity, with just one texture. As you can see in the following figure, multiple desert images can be put together without creating a hard seam, giving the illusion that it's only one image.

That is exactly what we did. We made a desert "tile", which is only big enough to fill one screen. Using the following technique, we made it look infinite, repeating itself along the whole level.

Texture repeating

The key to our tiling effect is exactly the texture repeating feature that SFML provides us. Every sf::Texture comes along with the option to enable repeating along both axis with the sf::Texture::setRepeated(bool) function.

When repeating is enabled for a texture, it means that it will be theoretically infinite, tiling itself continuously as much as required. When a sf::Sprite object links to

a texture in this mode, it will behave normally until the sprite requests a texture rectangle that is larger than the texture's real dimensions. In that moment, the sprite will display the texture along its whole size.

[ 73 ]

www.it-ebooks.info

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