SurajSharma90 / SFML_RPG

RPG game made in SFML using C++
191 stars 36 forks source link

Improper initializing loop in TileMap.cpp #2

Open Sahil12S opened 5 years ago

Sahil12S commented 5 years ago

While writing constructor of TileMap to initialize vector, there is one extra loop of layers that can be removed and resize statements should go outside the for loop. So, we resize all x coordinates by one statement, then for every x, we resize y, then for every y, we resize z. While in present code, we are resizing y for size of y, which will throw error if we go for rectangular map. Resolution snippet:

// Initialize map
m_Map.resize( m_MapSize.x, std::vector< std::vector< Tile* > >() );

for ( int x = 0; x < m_MapSize.x; x++ )  {
m_Map[x].resize( m_MapSize.y, std::vector< Tile* >() );

    for ( int y = 0; y < m_MapSize.y; y++ )  {
        m_Map[x][y].resize( m_Layers, nullptr );
    }
}