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

Chapter 3

The update function controls world scrolling and entity movement. First, the view is scrolled according to the passed time. Next, we check if the player's aircraft reaches a certain distance (150) from the world's borders, and flip its x velocity in this case.

This results in the plane moving back, until it reaches the other border, and the procedure repeats. Eventually, we forward the update to the scene graph, which actually applies the velocities.

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

{

mWorldView.move(0.f, mScrollSpeed * dt.asSeconds());

sf::Vector2f position = mPlayerAircraft->getPosition(); sf::Vector2f velocity = mPlayerAircraft->getVelocity();

if (position.x <= mWorldBounds.left + 150

|| position.x >= mWorldBounds.left + mWorldBounds.width - 150)

{

velocity.x = -velocity.x; mPlayerAircraft->setVelocity(velocity);

}

mSceneGraph.update(dt);

}

Integrating the Game class

By now we already know how the Game class works, what a game loop is for, and how to take advantage of it. For this chapter, we take the previously used Game class, and plug into it our newcomer World class.

Because we obeyed a few principles, this integration is very easy and it's just a matter of having a World object inside the Game class, and then letting it update and draw itself in the appropriate times.

The run() method

Our application's main() function has a simple job. It allocates a Game object, and lets it run itself through the run() method. When the run() method exits, the program releases its resources and closes.

Therefore, it is within the run() method that the magic happens! It is responsible for managing the famous game loop, fetching input from the window system, updating the world, and ordering the rendering of the game.

[ 81 ]

www.it-ebooks.info

Forge of the Gods – Shaping Our World

In the next chapter, events and input will be covered in depth. For now, it is

only important to understand how the drawing sequence happens inside the run() method:

mWindow.clear();

mWorld.draw();

mWindow.setView(mWindow.getDefaultView());

mWindow.draw(mStatisticsText);

mWindow.display();

This code closely resembles the high-level drawing of a frame for any game. The function call mWindow.clear() ensures that our frame buffer, the canvas where the frame will be drawn, is cleaned up before we start drawing. Then, we let the world draw itself, where it defines its own view and orders the scene graph to render itself. Finally, because we want to display some text in the corner of the screen for statistical purposes, we activate the default view, which is always the same so the text always appears fixed on the screen.

On the mWindow.display() call, we tell SFML we are done drawing the frame and it proceeds to upload it to our screen right away.

From this moment, you should be able to better dissect a game's mechanics and visualize its structure in your mind, mapping it directly to a data structure in C++. We can use the mental toolbox learned in this chapter to help us lay out our thoughts into actual computational data. This hypothetical toolbox contains many tools: managing entities and their transformations, scene graphs, views as cameras, graphical tricks, and others. But more importantly, we have started to understand how to properly glue all these elements together in a harmonious simulation. Furthermore, you have also seen many smaller techniques, which you may or may

not have known, for example, practical use cases of C++11 features. So far, our result looks as shown in the following screenshot:

[ 82 ]

www.it-ebooks.info

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