Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - SimonMate

Pages: [1]
1
Hello again,

Is this what you are talking about with using vertex arrays instead of a sprite, Hapax?

        m_vertexArray = sf::VertexArray(sf::Quads, 4);
       
        //Position setting
        m_vertexArray[0].position = sf::Vector2f(posX, posY);
        m_vertexArray[1].position = sf::Vector2f(posX + 10, posY);
        m_vertexArray[2].position = sf::Vector2f(posX + 10, posY + 10);
        m_vertexArray[3].position = sf::Vector2f(posX, posY + 10);
       
       
        if (tileType == "floor")
        {
                m_vertexArray[0].texCoords = sf::Vector2f(56, 85);
                m_vertexArray[1].texCoords = sf::Vector2f(64, 85);
                m_vertexArray[2].texCoords = sf::Vector2f(64, 93);
                m_vertexArray[3].texCoords = sf::Vector2f(56, 93);
        }

        else if (tileType == "wall")
        {
                m_vertexArray[0].texCoords = sf::Vector2f(47, 76);
                m_vertexArray[1].texCoords = sf::Vector2f(55, 76);
                m_vertexArray[2].texCoords = sf::Vector2f(55, 84);
                m_vertexArray[3].texCoords = sf::Vector2f(47, 84);
        }

        else if (tileType == "closedDoor")
        {
                m_vertexArray[0].texCoords = sf::Vector2f(56, 85);
                m_vertexArray[1].texCoords = sf::Vector2f(64, 85);
                m_vertexArray[2].texCoords = sf::Vector2f(64, 93);
                m_vertexArray[3].texCoords = sf::Vector2f(56, 93);
        }

        else if (tileType == "stairsUp")
        {
                m_vertexArray[0].texCoords = sf::Vector2f(137, 76);
                m_vertexArray[1].texCoords = sf::Vector2f(145, 76);
                m_vertexArray[2].texCoords = sf::Vector2f(145, 84);
                m_vertexArray[3].texCoords = sf::Vector2f(137, 84);
        }

        else if (tileType == "stairsDown")
        {
                //m_sprite.setTextureRect(sf::IntRect(146, 76, 8, 8));
                m_vertexArray[0].texCoords = sf::Vector2f(146, 76);
                m_vertexArray[1].texCoords = sf::Vector2f(154, 76);
                m_vertexArray[2].texCoords = sf::Vector2f(154, 84);
                m_vertexArray[3].texCoords = sf::Vector2f(146, 84);
        }

2
I made some adjustments:

Firstly, I got a tileset that I actually wanted to use, so every texture is loaded from 1 image now.

Second, each Tile now only stores a pointer to a texture (in this case the tileset texture) which I get by returning it from a GameData class (for json and such).

Finally the actual sprite that is used from the tileset is decided based on the string I pass in the Tile constructor, and then I use setTextureRect to actually get the tile I want.

Cheers,
Simon

3
Hi,

I solved my problem, and I'm writing this post in case anyone else would like to use my solution.
Basically it was to create a tile class, cycle through the vector of characters stored in the Dungeon class to get the characters and their x and y positions, and to place a tile there based on its name via a string.

Tile.h
class Tile
{
public:
        Tile();
        Tile(const std::string tileType);
        Tile(const std::string tileType, int x, int y);
        void update();
        void draw(sf::RenderWindow &window);
private:
        sf::Sprite m_sprite;
        sf::Texture m_texture;
        sf::Vector2f m_pos;
        std::string m_id;
};
 

Tile.cpp
Tile::Tile()
{

}

Tile::Tile(const std::string tileType)
{

}

Tile::Tile(const std::string tileType, int x, int y)
{
        int posX = x * 16;
        int posY = y * 16;
        m_texture.loadFromFile(tileType + ".png");
        m_sprite.setTexture(m_texture);
        m_sprite.setPosition(sf::Vector2f(posX, posY));
        m_id = tileType;
}

void Tile::update()
{

}

void Tile::draw(sf::RenderWindow &window)
{
        window.draw(m_sprite);
}
 

Then back in main() I use this code to populate a tile vector with the correct tiles, and draw them.
        std::vector<Tile*> tileVector;



        //enum Tile
        //{
        //      Unused = ' ',
        //      Floor = '.',
        //      Corridor = ',',
        //      Wall = '#',
        //      ClosedDoor = '+',
        //      OpenDoor = '-',
        //      UpStairs = '<',
        //      DownStairs = '>'
        //};

        for (int x = 0; x <= 79; x++)
        {
                for (int y = 0; y <= 24; y++)
                {
                        if (d.getTile(x, y) == ' ')
                        {
                                tileVector.push_back(new Tile("floor", x, y));
                        }

                        if (d.getTile(x, y) == '#')
                        {
                                tileVector.push_back(new Tile("wall", x, y));
                        }

                        if (d.getTile(x, y) == '.')
                        {
                                tileVector.push_back(new Tile("empty", x, y));
                        }

                        if (d.getTile(x, y) == '+')
                        {
                                tileVector.push_back(new Tile("closedDoor", x, y));
                        }
                }
        }

        while (window->isOpen())
        {
                sf::Event event;

                while (window->pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window->close();
                }

                window->clear();
                int size = tileVector.size();
                for (int i = 0; i < size; i++)
                {
                        tileVector.at(i)->draw(*window);
                }
                window->display();
        }
 

Thanks for the suggestions everyone.
edit: here's a picture for you to see what it turned out like if you're interested in these algorithms



4
Hi all,

I was wondering if any of you have experience with adapting the algorithms here:
http://www.roguebasin.com/index.php?title=C%2B%2B_Example_of_Dungeon-Building_Algorithm
from outputting text to drawing tiles (specifically the 2nd and 3rd algorithms)

Thanks in advance for any help.

Simon

Pages: [1]
anything