0

I'm trying to load a tile map from a text file, but when I render it on the screen, it goes sideways, so the left block goes to the right of the screen, the right one goes to the left. am I doing anything wrong here please help.

void blocks::genBlocks(blocks getBlocks) {
ifstream mapFile("Data/block_map.txt");
if (mapFile.is_open()) {
    while (!mapFile.eof()) {
        blockData = 0;
        mapFile >> blockData;
        if (mapFile.peek() == '\n') {
            loadCounterY++;
            loadCounterX = 1;
        }
        else {
            loadCounterX++;
        }

        if (blockData == 1) {
            blockRect.setPosition(loadCounterX * 110, loadCounterY * 40);
            blocksVec.push_back(getBlocks);
        }
    }
}
}


void blocks::renderBlocks(RenderTarget &window) {
for (int i = 0; i < blocksVec.size(); i++) {
    window.draw(blocksVec[i].blockRect);
}
}

it should look like this: (1 means draw something, 0 means draw nothing)

0 0 0 0 0 1

0 0 0 0 0 1

0 0 0 0 0 1

0 0 0 0 0 1

0 0 0 0 0 1

0 0 0 0 0 1

but turns out it looks like this:

1 0 0 0 0 0

1 0 0 0 0 0

1 0 0 0 0 0

1 0 0 0 0 0

1 0 0 0 0 0

1 0 0 0 0 0
James Adkison
  • 9,412
  • 2
  • 29
  • 43
SSLukeY
  • 21
  • 5
  • Unrelated to your (current) problem, but you should read ["Why is iostream::eof inside a loop condition considered wrong?"](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Jul 03 '16 at 12:29
  • As for your problem, how are `loadCounterY` and `loadCounterX` initialized? And why aren't those local variables inside the loading function? Are they used anywhere else? And what is `getBlocks` and how is it initialized and used otherwise? And what is `blockRect` and where is it declared, initialized and used? – Some programmer dude Jul 03 '16 at 12:33
  • And finally, a little note about your reading and parsing: There's no need for you to explicitly check for newlines, instead just [read a whole line](http://en.cppreference.com/w/cpp/string/basic_string/getline) into a [string](http://en.cppreference.com/w/cpp/string/basic_string), and use an [input string stream](http://en.cppreference.com/w/cpp/io/basic_istringstream) to get the numbers one by one (and that can by done with a single call to [`std::copy`](http://en.cppreference.com/w/cpp/algorithm/copy) or [`std::transform`](http://en.cppreference.com/w/cpp/algorithm/transform)). – Some programmer dude Jul 03 '16 at 12:35

0 Answers0