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

Every Pixel Counts – Adding Visual Effects

Defining texture atlases

A texture atlas describes the concept of a single texture that contains multiple objects. You may also encounter other terms, such as sprite sheet, or tile set in the case of squared tiles put together. Texture atlases allow having fewer image files, which decreases the amount of switches between different textures at runtime. Since texture switching is a rather slow operation on the graphics card, using texture atlases may result in notable speedups. Till now, we used separate textures for each object: one for each aircraft, pick-up, projectile, button, and background. Every texture was stored in its own PNG file. The code design looked as follows:

Textures were stored inside TextureHolder, our container storing sf::Texture objects.

We had an enum Textures::ID to identify the different textures in a TextureHolder. By that, we could easily refer to different textures without knowing the actual sf::Texture object or the filename.

The textures used in the scene were loaded in World::loadTextures().

They were bound to sprites in the specific entity classes such as Aircraft. For a given entity, data tables stored the texture ID it used.

The SFML sprite class sf::Sprite offers the possibility to set a texture rectangle

(or texture rect for short), containing the pixel coordinates of a specific object inside the texture. You already came across this functionality when we implemented the tiling background for our world in Chapter 3, Forge of the Gods – Shaping Our World. The rectangle is of type sf::IntRect and stores four integral values: the x and

y coordinates of the left-upper pixel (members left and top) as well as the size (members width and height).

For example, the following texture rectangle rect begins at (0, 15) and has a width of 30 and height of 20. The size excludes the last pixel; the pixel with coordinates (30, 35) is outside the rectangle.

sf::IntRect rect(0, 15, 30, 20);

Given a texture and a rectangle, you can initialize a sprite using the constructor, or you can set the attributes later with the corresponding methods.

sf::Texture texture = ...; sf::IntRect rect = ...;

[ 186 ]

www.it-ebooks.info

Chapter 8

sf::Sprite sprite(texture, rect);

sf::Sprite sprite2; sprite2.setTexture(texture); sprite2.setTextureRect(rect);

If no rectangle is specified, the sprite will assume that the whole texture is used. This is what we have always done so far.

Adapting the game code

We need to extend a few parts of our code to work with texture rects instead of whole textures. First, we must remove many of our resource identifiers. All the aircraft, projectile and pickup textures will be merged to one texture, with an ID of Entities. The texture containing the three buttons is accessible via Buttons.

Eventually, we only have the following identifiers:

namespace Textures

{

enum ID

{

Entities,

Jungle,

TitleScreen,

Buttons,

Explosion,

Particle,

FinishLine,

};

}

In case you wonder, Jungle is the new background we will paint. It is much bigger and far more interesting than the desert we had before. FinishLine is a texture used to mark the end of the level, instead of the black void. It is embedded to the scene graph using SpriteNode. Explosion and Particle are going to be introduced soon.

[ 187 ]

www.it-ebooks.info

Every Pixel Counts – Adding Visual Effects

With the new image files in our Media folder, the method World::loadTextures() can be adapted accordingly. We also modify our data tables to store a texture rectangle in addition to the texture ID. The rectangle coordinates are hardcoded in the initialization functions.

struct AircraftData

 

{

 

Textures::ID

texture;

sf::IntRect

textureRect;

...

 

};

 

std::vector<AircraftData> initializeAircraftData()

{

std::vector<AircraftData> data(Aircraft::TypeCount);

data[Aircraft::Eagle].texture = Textures::Entities; data[Aircraft::Eagle].textureRect = sf::IntRect(0, 0, 48, 64);

...

return data;

}

The last part to extend is the entities that use the textures. Now, we initialize the sprite with both texture and texture rect:

namespace

{

const std::vector<AircraftData> Table = initializeAircraftData();

}

Aircraft::Aircraft(Type type, const TextureHolder& textures, const FontHolder& fonts)

: mSprite(

textures.get(Table[type].texture), // sf::Texture

Table[type].textureRect)

// sf::IntRect

, ...

 

{

 

centerOrigin(mSprite);

 

...

 

}

 

Analogous steps have been applied for textures other than the one for the Eagle aircraft. That's pretty much it; the new code should now directly work with texture atlases! Visually, there will be no difference to what we had before.

[ 188 ]

www.it-ebooks.info

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