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?