i am also reading the book and i think it is great.
i've just finished the first part of chapter 8 (texture atlases).
i feel like experimenting something now, and i want to make a TileBased game using the architecture, mechanisms and concepts provided by the book.
What i've done so far:
- created a Tile Entity, with Type corresponding to the tile in the Texture file (a simple tileset with 4 16x16 tiles: Space, Block1, Block2, None).
- initialized the TileData in the DataTable by its Type. They all refer to the same Texture::ID and each has it's own intRect.
- i've managed to create and attach a Tile to a Layer in the SceneGraph using the Table, providing the Tile::Type.
- there is also a SpaceTile Category, corresponding to the Space, Block1, Block2, None Tile Types from the same Texture File.
it works, and it seems to be well adapted to the framework, but i have the feeling it does not scale well.
i think i should build a vertex array to draw the tilemap in the Background Layer, but how am i going to access the tile data after i draw the map in the background layer?
it looks interesting to have a Tile Entity, as it can have attributes like Hitpoints, Effects, etc and interactions like collision detections (i've made a isWalkable() which returns true if mType == Space).
thanks!
-Gabriel
--- edit ---
based on this example
http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.phpi was able to create a TileMap class and draw it in the background with a single draw call:
TileMap.hpp
#ifndef GAME_TILEMAP_HPP
#define GAME_TILEMAP_HPP
#include <Game/SceneNode.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/VertexArray.hpp>
class TileMap : public SceneNode
{
public:
TileMap(Category::Type category, const sf::Texture& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
private:
sf::Texture mTileset;
sf::VertexArray mVertices;
};
#endif // GAME_TILEMAP_HPP
TileMap.cpp
#include <Game/TileMap.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
TileMap::TileMap(Category::Type category, const sf::Texture& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
: SceneNode(category)
, mTileset(tileset)
, mVertices()
{
mVertices.setPrimitiveType(sf::Quads);
mVertices.resize(width * height * 4);
// populate the vertex array, with one quad per tile
for (unsigned int i = 0; i < width; ++i)
for (unsigned int j = 0; j < height; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * width];
// find its position in the tileset texture
int tu = tileNumber % (tileset.getSize().x / tileSize.x);
int tv = tileNumber / (tileset.getSize().x / tileSize.x);
// get a pointer to the current tile's quad
sf::Vertex* quad = &mVertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
}
}
void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &mTileset;
// draw the vertex array
target.draw(mVertices, states);
}
World::buildScene()
const int level[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
std::unique_ptr<TileMap> tileMap(new TileMap(Category::SpaceTile, mTextures.get(Textures::ID::Tiles), sf::Vector2u(16, 16), level, 16, 8 ));
tileMap->setPosition(mWorldBounds.left, mWorldBounds.top);
mSceneLayers[Background]->attachChild(std::move(tileMap));
i'm thinking about removing the Texture from the Tile Entity, since it is not necessary anymore for drawing the tile, and then attach each Tile to the SceneGraph based on the level index (Tile Type).
suggestions?
screenshot attached, don't mind the Eagle x)