Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So i'm building a 2d game but i have never really done any procedural generation i followed the SFML tutorial for tilemaps and i have the maps loading what i know want to know is how can i convert the SFML code to use arrays where i can just the array to an instance of a tile.

So for instance i have the following array:

Terrain* tiles[MAP_WIDTH][MAP_HEIGHT]

and to assign tiles i can do this

tiles[x][y] = &_grassTerrain 

this is essentially what i want to be able to do and draw from this array but instead im stuck using SFML vertexarray.

Here is my current code which is a mix and match of the SFML tutorial and my own code.

Here is my current code for generating the tile map

void Map::GenerateTerrain()
{

    _vertices.setPrimitiveType(sf::Quads);
    _vertices.resize(MAP_WIDTH * MAP_HEIGHT * 4);

    for(int x = 0; x < MAP_WIDTH; x++)
    {
        for(int y = 0; y < MAP_HEIGHT; y++)
        {

            int tu = 1 % (text.getSize().x / TILE_WIDTH);
            int tv = 1 / (text.getSize().x / TILE_WIDTH);

            tiles[x][y] = &_grassTerrain;

            sf::Vertex* quad = &_vertices[(x + y * MAP_WIDTH) * 4];

            // define its 4 corners
            quad[0].position = sf::Vector2f(x * TILE_WIDTH, y * TILE_HEIGHT);
            quad[1].position = sf::Vector2f((x + 1) * TILE_WIDTH, y * TILE_HEIGHT);
            quad[2].position = sf::Vector2f((x + 1) * TILE_WIDTH, (y + 1) * TILE_HEIGHT);
            quad[3].position = sf::Vector2f(x * TILE_WIDTH, (y + 1) * TILE_HEIGHT);

            // define its 4 texture coordinates
            quad[0].texCoords = sf::Vector2f(tu * TILE_WIDTH, tv * TILE_HEIGHT);
            quad[1].texCoords = sf::Vector2f((tu + 1) * TILE_WIDTH, tv * TILE_HEIGHT);
            quad[2].texCoords = sf::Vector2f((tu + 1) * TILE_WIDTH, (tv + 1) * TILE_HEIGHT);
            quad[3].texCoords = sf::Vector2f(tu * TILE_WIDTH, (tv + 1) * TILE_HEIGHT);

        }
    }
}

How can i integrate the SFML vertex for drawing into what the style i am working with, i am new to SFML so i really don't know much.

Here is the Header for the Map class

class Terrain;

class Map: public sf::Drawable, public sf::Transformable
{
public:
    Map();
    ~Map(void);

    sf::Texture text;

    static const int MAP_HEIGHT = 32;
    static const int MAP_WIDTH = 32;

    static const int TILE_HEIGHT = 32;
    static const int TILE_WIDTH = 32;

    void GenerateTerrain();

    const Terrain& GetTile(int x, int y) const;

private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

    sf::VertexArray _vertices;

    Terrain* tiles[MAP_WIDTH][MAP_HEIGHT];

    Terrain _grassTerrain;
};
share|improve this question
    
You could put a Texture object in the Terrain class that contains the part of the image representing that terrain and put a Sprite object in the Map class. That way in your draw method (doesn't it need to be public?) you can just iterate your map array by setting the sprite's texture then drawing it with the target and states. –  fastinvsqrt Aug 1 at 14:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.