断言失败字符串下标超出范围sfml

时间:2018-03-20 15:46:22

标签: c++ string sfml

您好我正在尝试了解SFML,虽然它非常有趣,但我对Level创建和tilemaps感到磕磕绊绊。每次我处理某些问题时,材料越来越清晰,但现在我偶然发现了一些我没有得到的东西。我一直在线观看本教程,并提出以下代码:

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

}

这就是我的文本文件:

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

当我开始调试时,我收到一条错误说明:

调试断言失败! 程序:C:\ WINDOWS \ SYSTEM32 \ MSVCP140D.dll 文件c:程序文件[x86] \ microsoft visual studio \ 2017 \ community \ vc \ tools \ msvc \ 14.11.25503 \ include \ xstring 2969行 表达式字符串下标超出范围

有人可以告诉我出了什么问题吗?

0 个答案:

没有答案