Error with multidimensional vector

Started by
3 comments, last by Dante12129 10 years, 10 months ago

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.

Advertisement

I think my code may actually be in when I load the map, as if I output the whole thing immediately after loading it shows the same skewed output.

Here's my map:


01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
02 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

And the output from what it outputs while it is loading:


1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0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ray as a vector, which is


m_map(i * m_map_dimensions.x + j)

.

I'm guessing m_columns is wrong. I don't see where it's being set, but looking at the output there, you've got 15 rows and 20 columns, and it looks like your skewed output is mixing those up and thinking you've got 20 rows and 15 columns.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

I looked at my MultiArray's constructor and it seems I had been setting the rows and columns in it backwards.

This topic is closed to new replies.

Advertisement