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

Forge of the Gods – Shaping Our World

Scene layers

In the game, we often have different scene nodes that must be rendered in a certain order. Nodes with objects that are located "above" others (closer to the sky) must be drawn after them. For example, we might first draw a desert background, then an oasis and some buildings, above which we draw the planes, and eventually some health bars located in front of them. This is rather cumbersome to handle when we insert node by node to the scene graph, because we have to ensure the order manually.

Luckily, we can easily automate the ordering, even using the scene graph's current capabilities. We call a group of scene nodes that are rendered together a layer. Inside a layer, the rendering order is irrelevant—we just make sure we render the different layers in the right order. We represent a layer with an empty scene node, directly under the graph's root node. A layer node itself contains no graphics; it is only supposed to render its children. Since the scene graph is traversed node by node, we know that all children of layer one will be rendered before any children of layer two. We can assign a node to a certain layer by attaching it as a child to the corresponding layer node. As a result, we have an automatic ordering of different layers, without the need to manually sort objects.

Let us introduce two layers for now: one for the background, and one for entities in the air. We use an enum to have an appropriate type. The last enumerator

LayerCount is not used to refer to a layer; instead it stores the total amount of layers.

enum Layer

{

Background,

Air, LayerCount

};

Updating the scene

In each frame, we update our world with all the entities inside. During an update, the whole game logic is computed: entities move and interact with each other, collisions are checked, and missiles are launched. Updating changes the state of our world and makes it progress over time, while rendering can be imagined as a snapshot of a part of the world at a given time point.

[ 64 ]

www.it-ebooks.info

Chapter 3

We can reuse our scene graph to reach all entities with our world updates. To achieve this, we implement a public update() member function in the SceneNode class. Analogous to the way we have proceeded for the draw() function, we split up update() into two parts: an update for the current node, and one for the child nodes. We thus write two private methods updateCurrent() and updateChildren(), of which the former is virtual.

All update functions take the frame time dt as a parameter of type sf::Time (the SFML class for time spans). This is the frame time we computed for the Game class in Chapter 1, Making a Game Tick. In our game, we work with fixed time steps, so dt will be constant. Nevertheless, we pass the frame time every time to the scene nodes and make entity behavior dependent on it. This leaves us the flexibility to change the frame rate or to experiment with other approaches to compute the frame time.

We add the following methods to our SceneNode class:

public:

 

void

update(sf::Time dt);

private:

 

virtual void

updateCurrent(sf::Time dt);

void

updateChildren(sf::Time dt);

The implementation is the same as for rendering. The definition of updateCurrent() remains empty, by default we do nothing for a scene node.

void SceneNode::update(sf::Time dt)

{

updateCurrent(dt);

updateChildren(dt);

}

void SceneNode::updateCurrent(sf::Time)

{

}

void SceneNode::updateChildren(sf::Time dt)

{

FOREACH(Ptr& child, mChildren) child->update(dt);

}

[ 65 ]

www.it-ebooks.info

Forge of the Gods – Shaping Our World

In derived classes, we can now implement specific update functionality, such as movement of each entity. In the Entity class, we override the virtual

updateCurrent() method, in order to apply the current velocity. The class definition of Entity is expanded by the following lines of code:

private:

 

virtual void

updateCurrent(sf::Time dt);

We offset the position by the velocity depending on the time step. A longer time step leads to a bigger offset, meaning that our entity is moved further over longer time.

void Entity::updateCurrent(sf::Time dt)

{

move(mVelocity * dt.asSeconds());

}

Here, move() is a function of the indirect base class sf::Transformable. The expression move(offset) is a shortcut for setPosition(getPosition() + offset).

Since Aircraft inherits Entity, the update functionality for it is also inherited. We thus do not need to re-define updateCurrent() in the Aircraft class, unless we want to execute further actions specifically for aircraft.

One step back – absolute transforms

Relative coordinates are nice and useful, but there are cases where we still want to access the absolute position of an object in the world. For example, to find out whether two entities collide, relative positions won't help us—we need to know

where in the world the entities are located, not where in the local coordinate system.

To compute the absolute transforms, we can step upwards in the class hierarchy, and accumulate all relative transforms until we reach the root. This was also the reason why we introduced the parent pointer in the SceneNode class. In addition to the function getPosition(), which is inherited from sf::Transformable and returns the relative position, we add a new method getWorldPosition() to SceneNode, which returns the absolute position. First, we add a function getWorldTransform() that takes into account all the parent transforms. It multiplies all the sf::Transform objects from the root to the current node, its iterative implementation looks as follows. The position can be computed by transforming the origin sf::Vector2f() using the absolute transform.

sf::Transform SceneNode::getWorldTransform() const

{

sf::Transform transform = sf::Transform::Identity;

[ 66 ]

www.it-ebooks.info

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