0

Hi I am trying to learn about SFML and although it is great fun I am stumbling with Level creation and tilemaps. The material is getting more clearer everytime I work on some issue, but now I have stumbled upon something I dont get. I have been watching this tutorial online and came up with the following code:

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;
using namespace sf;


int main()
{
ifstream openfile("levels/level1.txt");
Texture tileTexture;
Sprite tiles;

Vector2i map[100][100];
Vector2i loadCounter = Vector2i(0, 0);

if (openfile.is_open())
{
    string tileLocation;
    openfile >> tileLocation;
    tileTexture.loadFromFile(tileLocation);
    tiles.setTexture(tileTexture);
    while (!openfile.eof())
    {
        string str;
        char x = str[0];
        char y = str[2];

        if (!isdigit(x) || !isdigit(y))
        {
            map[loadCounter.x][loadCounter.y] = Vector2i(-1, -1);

        }
        else
        {
            map[loadCounter.x][loadCounter.y] = Vector2i(x - '0', y - '0');
        }
        if (openfile.peek() == '\n')
        {
            loadCounter.x = 0;
            loadCounter.y++;
        }
        else
        {
            loadCounter.x++;
        }
    }

    loadCounter.y++;
}

RenderWindow Window(VideoMode(640, 480, 32), "MAPS");

while (Window.isOpen())
{
    Event Event;
    while (Window.pollEvent(Event))
    {
        switch (Event.type)
        {
        case Event::Closed:
            Window.close();
            break;
        }
    }

    Window.clear();

    for (int i = 0; i < loadCounter.x; i++)
    {
        for (int j = 0; j < loadCounter.y; j++)
        {
            if (map[i][j].x != -1 && map[i][j].y != -1)
            {
                tiles.setPosition(i * 32, j * 32);
                tiles.setTextureRect(IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
                Window.draw(tiles);
            }
        }
    }
    Window.display();
}

return 0;

}

This is the is what my textfile looks like:

graphics/tiles.png
0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 x,x x,x x,x x,x x,x x,x x,x x,x 0,0
0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0

When I start debugging I get an error stating:

Debug Assertion Failed! Program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll File c:program files[x86]\microsoft visual studio\2017\community\vc\tools\msvc\14.11.25503\include\xstring Line 2969 Expression string subscript out of range

Could someone please tell me what is going wrong?

arbtrader
  • 11
  • 2
  • This code snippet string str; char x = str[0]; char y = str[2]; does not make sense. str is empty. So what you do is what you get.:) – Vlad from Moscow Mar 20 '18 at 15:54
  • 1
    And once you fixed that, read about [why `while(!eof)` is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Quentin Mar 20 '18 at 15:55
  • Please [edit] your question to show us what kind of debugging you've done. I expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Mar 20 '18 at 18:40

0 Answers0