Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Error with multidimensional vector


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Dante12129   Members   -  Reputation: 1156

Like
0Likes
Like

Posted 23 June 2013 - 08:19 PM

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.

 


Sponsor:

#2 Dante12129   Members   -  Reputation: 1156

Like
0Likes
Like

Posted 24 June 2013 - 05:17 PM

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


#3 Dante12129   Members   -  Reputation: 1156

Like
0Likes
Like

Posted 25 June 2013 - 08:45 PM

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, 25 June 2013 - 08:47 PM.


#4 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 25 June 2013 - 11:00 PM

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.


[ 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 ]

#5 Dante12129   Members   -  Reputation: 1156

Like
0Likes
Like

Posted 26 June 2013 - 03:27 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS