I am making a tile map for my game. I am using my own MultiArray class to represent a 2D vector while using a one-dimensional vector internally. I can store things in it fine, bu when I try to access data in it the data is repeated in some places.
Loading code (outputs the map correctly in debug info):
void Map::LoadFromFile(const std::string& filename)
{
std::ifstream map_file(filename);
std::cout << m_map.GetHeight() << ", " << m_map.GetWidth() << std::endl;
std::cout << m_map_dimensions.x << ", " << m_map_dimensions.y << std::endl;
if(map_file.is_open())
{
int current_tile;
for(unsigned int i = 0; i < m_map_dimensions.y; i++)
{
for(unsigned int j = 0; j < m_map_dimensions.x; j++)
{
if(map_file >> current_tile)
{
try
{
m_map(i, j) = current_tile;
std::cout << m_map(i, j) << " ";
}
catch(std::exception& e)
{
std::cerr << "Vector overflow in map loading. Error: " << e.what() << std::endl;
}
}
else
std::cerr << "Error getting tile from map." << std::endl;
}
std::cout << std::endl;
}
std::cout << std::endl;
}
else
std::cerr << "Couldn't load map file: " << filename << "." << std::endl;
}
Part of drawing code (the part that outputs the map to the console):
if(!once_flag)
{
std::cout << m_map.GetHeight() << ", " << m_map.GetWidth() << std::endl;
std::cout << m_map_dimensions.x << ", " << m_map_dimensions.y << std::endl;
for(unsigned int i = 0; i < m_map_dimensions.y; i++)
{
for(unsigned int j = 0; j < m_map_dimensions.x; j++)
{
int temp = m_map(i, j);
std::cout << m_map(i, j) << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
once_flag = 1;
}
THe m_map is a MultiArray<int> and the m_map_dimensions is an sf::Vector2u. It will draw the wrong map in the correct way, so I know it is with it storing the data. Here are my overloads for operator() in my MultiArray class:
T& operator() (unsigned int row, unsigned int col)
{
#ifdef DEBUGBUILD
#ifdef DOUTPUT
std::cout << "Accessing element " << row * m_columns + col << "in multimap" << std::endl;
#endif // DOUTPUT
#endif // DEBUGBUILD
return m_data.at(row * m_columns + col);
}
T const& operator() (unsigned int row, unsigned int col) const
{
#ifdef DEBUGBUILD
#ifdef DOUTPUT
std::cout << "Accessing element " << row * m_columns + col << "in multimap" << std::endl;
#endif // DOUTPUT
#endif // DEBUGBUILD
return m_data.at(row * m_columns + col);
}
In the drawing code, the fifteenth through twentieth spots are being replaced with the first through fifth parts of the next line, which stay also. I'm not doing anything to the map between loading and drawing it.