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

Author Topic: Book code - Custom draw  (Read 872 times)

0 Members and 1 Guest are viewing this topic.

Urefeu

  • Newbie
  • *
  • Posts: 4
    • View Profile
Book code - Custom draw
« on: July 11, 2015, 09:27:08 pm »
Hi guys!

With the code from the book, I tried to create a tiled map with vertex arrays (like there : http://www.sfml-dev.org/tutorials/2.3/graphics-vertex-array.php).

So I did this :

void World::buildScene()
{
        // Initialize the different layers
        for (std::size_t i = 0; i < LayerCount; ++i)
        {
                Category::Type category = (i == LowerAir) ? Category::SceneBackgroundLayer : Category::None;

                SceneNode::Ptr layer(new SceneNode(category));
                mSceneLayers[i] = layer.get();

                mSceneGraph.attachChild(std::move(layer));
        }

        // Prepare the tiled background
        sf::Texture& tilesetTexture = mTextures.get(Textures::Tileset);

    int roomHeight = 30;
    int roomWidth = 30;

    sf::VertexArray _vertices;
    _vertices.setPrimitiveType(sf::Quads);
    _vertices.resize(30 * 30 * 4);

        for(unsigned int y = 0; y < roomHeight; ++y)
        for(unsigned int x = 0; x < roomWidth; ++x)
        {
            unsigned int tileNumber = 5;
            unsigned int textureColumn, textureLine;

            textureColumn = (tileNumber % 30 == 0) ? 30 : tileNumber % 30;
            textureLine = (tileNumber % 30 == 0) ? tileNumber / 30 : tileNumber / 30 + 1;

            sf::Vector2u coordTexture;

            coordTexture.x = (textureColumn - 1) * (128  + 128);
            coordTexture.y = (textureLine   - 1) * (128 + 128);

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

            quad[0].position = sf::Vector2f(x * 128, y * 128);
            quad[1].position = sf::Vector2f((x + 1) * 128, y * 128);
            quad[2].position = sf::Vector2f((x + 1) * 128 , (y + 1) * 128);
            quad[3].position = sf::Vector2f(x * 128, (y + 1) * 128);

            quad[0].texCoords = sf::Vector2f(coordTexture.x, coordTexture.y);
            quad[1].texCoords = sf::Vector2f(coordTexture.x + 128, coordTexture.y);
            quad[2].texCoords = sf::Vector2f(coordTexture.x + 128, coordTexture.y + 128);
            quad[3].texCoords = sf::Vector2f(coordTexture.x, coordTexture.y + 128);
        }

    std::unique_ptr<SpriteNode> tiledBackground(new SpriteNode(tilesetTexture, _vertices));
    mSceneLayers[Background]->attachChild(std::move(tiledBackground));
}

And then I modified SpriteNode like that :

class SpriteNode : public SceneNode
{
        public:
                explicit                        SpriteNode(const sf::Texture& texture);
                                                        SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect);
                                                        SpriteNode(const sf::Texture& texture, sf::VertexArray& vertices);


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


        private:
                sf::Sprite                      mSprite;
                sf::VertexArray     mVertices;
                sf::Texture         mTexture;
                bool                mTiled;
};

SpriteNode::SpriteNode(const sf::Texture& texture)
: mSprite(texture)
, mTiled(false)
{
}

SpriteNode::SpriteNode(const sf::Texture& texture, const sf::IntRect& textureRect)
: mSprite(texture, textureRect)
, mTiled(false)
{
}

SpriteNode::SpriteNode(const sf::Texture& texture, sf::VertexArray& vertices)
: mTexture(texture)
, mVertices(vertices)
, mTiled(true)
{
}

void SpriteNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
        if(!mTiled)
        target.draw(mSprite, states);
    else
    {
        states.transform *= getTransform();
        states.texture = &mTexture;
        target.draw(mVertices, states);
    }
}
 

My VertexArray and my Texture are okay, the algorithm (with the 2 for) works in an other code but I'd like to integrate it into this one.

There, when I switch to GameState, I just see a black screen. What is wrong ?
I did put a std::cout in the else of the draw and it is displayed.

Thanks for your help!

 

anything