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

Author Topic: sf::Drawable Draw only visible area  (Read 2002 times)

0 Members and 1 Guest are viewing this topic.

GamDev

  • Newbie
  • *
  • Posts: 29
    • View Profile
sf::Drawable Draw only visible area
« on: July 22, 2018, 04:37:32 pm »
Hi, i have sf::Drawable class:
class DrawTileMap : public sf::Drawable
{
    public:
        void            load                (const Texture tileset, int ( &TileMap )[500][500]);
        void            update              (int ( &TileMap )[500][500]);
    private:
        virtual void    draw                (sf::RenderTarget& target, sf::RenderStates states) const;
        VertexArray     m_vertices;
        Texture         m_tileset;
        Vector2u        tileSize            = {64, 64};
};
//==================================================================================================
void DrawTileMap::load(const Texture tileset, int ( &TileMap )[500][500])
{
    m_tileset = tileset;

    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(500 * 500 * 4);
}
//==================================================================================================
void DrawTileMap::update(int ( &TileMap )[500][500])
{
    int tu = 0;
    int tv = 0;

    for(unsigned int x = 0; x < 500; ++x)
        for(unsigned int y = 0; y < 500; ++y)
            {
                switch(TileMap[ x ] [ y ] )
                    {
                        case 1:     tu = 0;tv = 2;break;
                        case 2:     tu = 1;tv = 2;break;
                        case 3:     tu = 2;tv = 2;break;
                        case 4:     tu = 3;tv = 2;break;
                        case 5:     tu = 4;tv = 2;break;
                        case 6:     tu = 0;tv = 3;break;
                        case 7:     tu = 1;tv = 3;break;
                        case 8:     tu = 2;tv = 3;break;
                        case 9:     tu = 3;tv = 3;break;
                        case 10:    tu = 4;tv = 3;break;

                        default:tu = 0;tv = 0;break;
                    }

                sf::Vertex* tile64 = &m_vertices[(x + y * 500) * 4];

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

                tile64[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                tile64[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                tile64[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                tile64[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }
}
//==================================================================================================
void DrawTileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.texture = &m_tileset;
    target.draw(m_vertices, states);
}
//==================================================================================================
 
my game map size is 500x500
this class draws a game map in full size (500x500)
how to draw only a visible area of the map, for example 100x100
« Last Edit: July 22, 2018, 04:58:26 pm by Laurent »

c0d3r9

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: sf::Drawable Draw only visible area
« Reply #1 on: July 23, 2018, 04:36:11 am »
I.e. if your view starts on start_x= 0, start_y = 0  you only draws the tiles from start_x/tile_size to (start_x+view_width)/tile_size+1 and from start_y/tile_size to (start_y+view_height)/tile_size +1 in your for loop.
If you scroll you only have to shift the start x/y  values depend on your movement value.The results are the array values for your tiles.


Sorry no code...its to late.Hope it helps a little bit.

 

anything