# Read text as a map in c++

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I have a class call map.

#include "Map.h"
#include <fstream>
#include <ostream>
#include <istream>

Log spritelog("spritedebug.txt", false);
Map::Map()
{
spritelog << "Sprites are loaded" << std::endl;
}
{
ifstream resources("resources.txt");
std::string str;
int i = 0;
while (std::getline(resources, str))
{
UsedSprites[i] = new Sprite(0, 0, str);
i++;
}
resources.close();
delete resources;
str = "";
ifstream maptext("map.txt");
i = 0;
int y = 0;
int x = 0;
while(std::getline(maptext, str))
{
stringstream stream(str);
string c = "";
while(std::getline(stream,c, ','))
{
int n = atoi(c.c_str());
MapSprite[x] = UsedSprites[n];
MapSprite[x]->X = i*64;
MapSprite[x]->Y = y*64;
spritelog << n << "Loading into " << x << " at " << i*64 << "," << y*64 << std::endl;
i++;
x++;
}
y++;
i = 0;
}
maptext.close();
spritelog << "Loading " << x << " Sprites Into Map at " << y << " lines" << std::endl;
delete UsedSprites;
delete maptext;
}
void Map::Render()
{
for(int i = 0; i < 34; i++)
{
MapSprite[i]->Render();
spritelog << "Rended: " << i << "X: " << MapSprite[i]->X << " Y: " << MapSprite[i]->Y << std::endl;
}
}



When I load this text file:

0,1,0,0,1
1,0,0,0,1
1,1,1,1,1
0,0,0,0,0
1,1,1,1,1
1,0,0,0,1
1,1,1,1,1

0 = green 1= grey

See attachments for debug info and screen shot.[attachment=19569:helpc++.PNG][attachment=19570:spritedebug.txt]

When is the x and y not the same as what I set it in Load() to what it renders at.
Edited by shawk08033

##### Share on other sites

MapSprite[x] = UsedSprite[n] will always be setting MapSprite[x] to point to UsedSprite[0] or UsedSprite[1]. UsedSprite[0]'s last update was 192,320. UsedSprite[1]'s last update was 256, 384.

You can see step-by-step what is happening by changing "spritelog << n << "Loading into " << x << " at " << i*64 << "," << y*64 << std::endl;" to

"for (int t = 0; t < x; t++) spritelog << n << "Currently in " << t << " is " << MapSprite[t]->X << "," << MapSprite[t]->Y << std::endl;"

Minor quibble: You probably shouldn't call Load() from a ctor, but should use an Init() or such. Very bad practice to do disk I/O in a constructor.

##### Share on other sites

MapSprite[x] = UsedSprite[n] will always be setting MapSprite[x] to point to UsedSprite[0] or UsedSprite[1]. UsedSprite[0]'s last update was 192,320. UsedSprite[1]'s last update was 256, 384.

You can see step-by-step what is happening by changing "spritelog << n << "Loading into " << x << " at " << i*64 << "," << y*64 << std::endl;" to

"for (int t = 0; t < x; t++) spritelog << n << "Currently in " << t << " is " << MapSprite[t]->X << "," << MapSprite[t]->Y << std::endl;"

Minor quibble: You probably shouldn't call Load() from a ctor, but should use an Init() or such. Very bad practice to do disk I/O in a constructor.

So the output it gives me when I do that is:

1Currently in 0 is 0,0
0Currently in 0 is 128,0
0Currently in 1 is 64,0
0Currently in 0 is 192,0
0Currently in 1 is 64,0
0Currently in 2 is 192,0
1Currently in 0 is 192,0
1Currently in 1 is 256,0
1Currently in 2 is 192,0
1Currently in 3 is 192,0
1Currently in 0 is 192,0
1Currently in 1 is 0,64
1Currently in 2 is 192,0
1Currently in 3 is 192,0
1Currently in 4 is 0,64
0Currently in 0 is 64,64
0Currently in 1 is 0,64
0Currently in 2 is 64,64
0Currently in 3 is 64,64
0Currently in 4 is 0,64
0Currently in 5 is 0,64
0Currently in 0 is 128,64
0Currently in 1 is 0,64
0Currently in 2 is 128,64
0Currently in 3 is 128,64
0Currently in 4 is 0,64
0Currently in 5 is 0,64
0Currently in 6 is 128,64
0Currently in 0 is 192,64
0Currently in 1 is 0,64
0Currently in 2 is 192,64
0Currently in 3 is 192,64
0Currently in 4 is 0,64
0Currently in 5 is 0,64
0Currently in 6 is 192,64
0Currently in 7 is 192,64
1Currently in 0 is 192,64
1Currently in 1 is 256,64
1Currently in 2 is 192,64
1Currently in 3 is 192,64
1Currently in 4 is 256,64
1Currently in 5 is 256,64
1Currently in 6 is 192,64
1Currently in 7 is 192,64
1Currently in 8 is 192,64
1Currently in 0 is 192,64
1Currently in 1 is 0,128
1Currently in 2 is 192,64
1Currently in 3 is 192,64
1Currently in 4 is 0,128
1Currently in 5 is 0,128
1Currently in 6 is 192,64
1Currently in 7 is 192,64
1Currently in 8 is 192,64
1Currently in 9 is 0,128
1Currently in 0 is 192,64
1Currently in 1 is 64,128
1Currently in 2 is 192,64
1Currently in 3 is 192,64
1Currently in 4 is 64,128
1Currently in 5 is 64,128
1Currently in 6 is 192,64
1Currently in 7 is 192,64
1Currently in 8 is 192,64
1Currently in 9 is 64,128
1Currently in 10 is 64,128
1Currently in 0 is 192,64
1Currently in 1 is 128,128
1Currently in 2 is 192,64
1Currently in 3 is 192,64
1Currently in 4 is 128,128
1Currently in 5 is 128,128
1Currently in 6 is 192,64
1Currently in 7 is 192,64
1Currently in 8 is 192,64
1Currently in 9 is 128,128
1Currently in 10 is 128,128
1Currently in 11 is 128,128
1Currently in 0 is 192,64
1Currently in 1 is 192,128
1Currently in 2 is 192,64
1Currently in 3 is 192,64
1Currently in 4 is 192,128
1Currently in 5 is 192,128
1Currently in 6 is 192,64
1Currently in 7 is 192,64
1Currently in 8 is 192,64
1Currently in 9 is 192,128
1Currently in 10 is 192,128
1Currently in 11 is 192,128
1Currently in 12 is 192,128
1Currently in 0 is 192,64
1Currently in 1 is 256,128
1Currently in 2 is 192,64
1Currently in 3 is 192,64
1Currently in 4 is 256,128
1Currently in 5 is 256,128
1Currently in 6 is 192,64
1Currently in 7 is 192,64
1Currently in 8 is 192,64
1Currently in 9 is 256,128
1Currently in 10 is 256,128
1Currently in 11 is 256,128
1Currently in 12 is 256,128
1Currently in 13 is 256,128
0Currently in 0 is 0,192
0Currently in 1 is 256,128
0Currently in 2 is 0,192
0Currently in 3 is 0,192
0Currently in 4 is 256,128
0Currently in 5 is 256,128
0Currently in 6 is 0,192
0Currently in 7 is 0,192
0Currently in 8 is 0,192
0Currently in 9 is 256,128
0Currently in 10 is 256,128
0Currently in 11 is 256,128
0Currently in 12 is 256,128
0Currently in 13 is 256,128
0Currently in 14 is 256,128
0Currently in 0 is 64,192
0Currently in 1 is 256,128
0Currently in 2 is 64,192
0Currently in 3 is 64,192
0Currently in 4 is 256,128
0Currently in 5 is 256,128
0Currently in 6 is 64,192
0Currently in 7 is 64,192
0Currently in 8 is 64,192
0Currently in 9 is 256,128
0Currently in 10 is 256,128
0Currently in 11 is 256,128
0Currently in 12 is 256,128
0Currently in 13 is 256,128
0Currently in 14 is 256,128
0Currently in 15 is 64,192
0Currently in 0 is 128,192
0Currently in 1 is 256,128
0Currently in 2 is 128,192
0Currently in 3 is 128,192
0Currently in 4 is 256,128
0Currently in 5 is 256,128
0Currently in 6 is 128,192
0Currently in 7 is 128,192
0Currently in 8 is 128,192
0Currently in 9 is 256,128
0Currently in 10 is 256,128
0Currently in 11 is 256,128
0Currently in 12 is 256,128
0Currently in 13 is 256,128
0Currently in 14 is 256,128
0Currently in 15 is 128,192
0Currently in 16 is 128,192
0Currently in 0 is 192,192
0Currently in 1 is 256,128
0Currently in 2 is 192,192
0Currently in 3 is 192,192
0Currently in 4 is 256,128
0Currently in 5 is 256,128
0Currently in 6 is 192,192
0Currently in 7 is 192,192
0Currently in 8 is 192,192
0Currently in 9 is 256,128
0Currently in 10 is 256,128
0Currently in 11 is 256,128
0Currently in 12 is 256,128
0Currently in 13 is 256,128
0Currently in 14 is 256,128
0Currently in 15 is 192,192
0Currently in 16 is 192,192
0Currently in 17 is 192,192
0Currently in 0 is 256,192
0Currently in 1 is 256,128
0Currently in 2 is 256,192
0Currently in 3 is 256,192
0Currently in 4 is 256,128
0Currently in 5 is 256,128
0Currently in 6 is 256,192
0Currently in 7 is 256,192
0Currently in 8 is 256,192
0Currently in 9 is 256,128
0Currently in 10 is 256,128
0Currently in 11 is 256,128
0Currently in 12 is 256,128
0Currently in 13 is 256,128
0Currently in 14 is 256,128
0Currently in 15 is 256,192
0Currently in 16 is 256,192
0Currently in 17 is 256,192
0Currently in 18 is 256,192
what does this mean?

##### Share on other sites

MapSprite[x] = UsedSprite[n] will always be setting MapSprite[x] to point to UsedSprite[0] or UsedSprite[1]. UsedSprite[0]'s last update was 192,320. UsedSprite[1]'s last update was 256, 384.

You can see step-by-step what is happening by changing "spritelog << n << "Loading into " << x << " at " << i*64 << "," << y*64 << std::endl;" to

"for (int t = 0; t < x; t++) spritelog << n << "Currently in " << t << " is " << MapSprite[t]->X << "," << MapSprite[t]->Y << std::endl;"

Minor quibble: You probably shouldn't call Load() from a ctor, but should use an Init() or such. Very bad practice to do disk I/O in a constructor.

So the output it gives me when I do that is:

<snip massively unnecessary spam>
what does this mean?

The output is telling you that you're overwriting existing values because your current MapSprite[x] points to the same things as previous MapSprite[x]. You have 34 MapSprites pointing to only 2 UsedSprites. You can't fit 34 sets of X and Y data into only 2 Sprites.

You might be incorrectly thinking MapSprite[x] = UsedSprite[n] means the same thing as MapSprite[x] = new Sprite(UsedSprite[n]). It doesn't. Your version causes MapSprite[x] to point to the existing UsedSprite[n] variable, and the version with new copies (using copy constructor) the data from UsedSprite[n] into a newly allocated Sprite variable.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 34
• 16
• 18
• 25