Sign in to follow this  
Dante12129

Error with multidimensional vector

Recommended Posts

Dante12129    2038

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.

 

Share this post


Link to post
Share on other sites
Dante12129    2038

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 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0 0

Share this post


Link to post
Share on other sites
Dante12129    2038

I tried it with a normal vector and it works fine. It also works if I access my MultiArray as a vector, which is 

m_map(i * m_map_dimensions.x + j)

.

Edited by Dante12129

Share this post


Link to post
Share on other sites
Cornstalks    7030

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this