C++ 二维向量返回SIGSEGV。SFML游戏
我刚刚编写了一个代码,它从.txt文件加载tilemap。一切都很好,我的“游戏”完成了。在此之后,我编写了代码,负责在地图上添加像房子这样的对象。 现在代码可以编译了,但当我启动它时,出现了SIGSEGV错误。我正在使用SFML2.4.2。怎么了调试器将我发送到780线型stl_vector.h,以及Level.cpp中的行:C++ 二维向量返回SIGSEGV。SFML游戏,c++,vector,sfml,segmentation-fault,C++,Vector,Sfml,Segmentation Fault,我刚刚编写了一个代码,它从.txt文件加载tilemap。一切都很好,我的“游戏”完成了。在此之后,我编写了代码,负责在地图上添加像房子这样的对象。 现在代码可以编译了,但当我启动它时,出现了SIGSEGV错误。我正在使用SFML2.4.2。怎么了调试器将我发送到780线型stl_vector.h,以及Level.cpp中的行: tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type]) h级 类对象; 班级 { 公众: 水平();
tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type])
h级
类对象;
班级
{
公众:
水平();
级别(std::字符串文件名);
void loadFromFile(std::string filename,Object&Object);//加载级别from.txt
~1级(空);
枚举字段类型{
草,
石
水,,
水1,
水(2),,
水(3),,
水(4),,
草地(块);;
结构砖
{
字段类型;
布尔伊斯沃尔;
};
整数宽度;
内部高度;
const static int tile_width=40;//单个tile的宽度和高度
常数静态积分瓦块高度=40;
sf::纹理技术[7];
std::vector>tiles;
std::vector>瓷砖;
};
类对象
{
公众:
int l;
对象();
~Object();
std::矢量对象_纹理;
std::矢量对象\u精灵;
/*std::矢量附加_精灵*/
};
Level.cpp
void级别::loadFromFile(std::字符串文件名、对象和对象)
{
std::fstream文件;
打开(文件名,std::ios::in);
如果(!file.is_open())
{
标准::cout>宽度>>高度;
瓷砖。调整大小(高度);
瓷砖。调整大小(高度);
对于(int i=0;i>tmp;
瓷砖分布[y][x]。类型=字段类型(tmp);
如果(tmp==2 | | tmp==3||
tmp==4 | | tmp==5||
tmp==6)
tiles_atributes[y][x].isWall=true;
其他的
tiles_atributes[y][x].isWall=false;
}
}
int j=0;
对于(int i=0;i x>>y;
m++;
object.objects\u sprites.resize(m);
object.objects_精灵[p].setTexture(object.objects_纹理[0]);
objects\u精灵[p].setPosition(x,y);
int ob_height=object.objects_精灵[p].getGlobalBounds().height;
int ob_width=object.objects_精灵[p].getGlobalBounds().width;
int point_x=object.objects_sprites[p].getGlobalBounds().left;
int point_y=object.objects_sprites[p].getGlobalBounds().top;
对于(int i=点y/平铺高度;i
你的数组中有七个纹理sf::Texture Tekstry[7];
。但是你尝试加载8个图像,因为你有八个类型。当你尝试访问第八个纹理时……好吧
你需要修正。在你的代码中停止使用魔术数字。使用一个知道你有多少种类型的单一常数。有些人用一个<代码>计数>代码>值结束他们的枚举…因为这个值知道有多少,即使你在中间添加了一些。但是一个简单的<代码> const siZixfield字段类型=8;可能起作用。o
class Object;
class Level
{
public:
Level();
Level(std::string filename);
void loadFromFile(std::string filename, Object &object); //load level from .txt
~Level(void);
enum FieldType {
GRASS,
STONE,
WATER,
WATER_1,
WATER_2,
WATER_3,
WATER_4,
GRASS_BLOCK};
struct Tile
{
FieldType type;
bool isWall;
};
int width;
int height;
const static int tile_width = 40; //width and height of single tile
const static int tile_height = 40;
sf::Texture tekstury[7];
std::vector <std::vector < sf::Sprite > > tiles;
std::vector < std::vector < Tile > > tiles_atributes;
};
class Object
{
public:
int l;
Object();
~Object();
std::vector <sf::Texture> objects_textures;
std::vector <sf::Sprite> objects_sprites;
/*std::vector <std::vector<sf::Sprite>> additional_sprites;*/
};
void Level::loadFromFile(std::string filename, Object &object)
{
std::fstream file;
file.open(filename, std::ios::in);
if (!file.is_open())
{
std::cout << "Not found " + filename;
return;
}
file >> width >> height;
tiles.resize(height);
tiles_atributes.resize(height);
for (int i = 0; i < height; i++)
{
tiles[i].resize(width);
tiles_atributes[i].resize(width);
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int tmp = 0;
file >> tmp;
tiles_atributes[y][x].type = FieldType(tmp);
if(tmp==2 || tmp==3 ||
tmp==4 || tmp==5 ||
tmp==6)
tiles_atributes[y][x].isWall = true;
else
tiles_atributes[y][x].isWall = false;
}
}
int j=0;
for (int i = 0; i <8; i++)
{
std::stringstream ss;
ss<< "Img/Kafelki/tekstura" << i <<".png";
std::string zrodlo;
ss>>zrodlo;
tekstury[i].loadFromFile(zrodlo);
j++;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type]);
tiles[i][j].setPosition(j*tile_width, i*tile_height);
}
}
int m=0;
int p=0;
while (!file.eof())
{
std::string name; // nazwa specjalnego Objectu
file >> name;
std::cout<< name;
if (name == "[House]")
{
unsigned short x, y;
file >> x >> y;
m++;
object.objects_sprites.resize(m);
object.objects_sprites[p].setTexture(object.objects_textures[0]);
object.objects_sprites[p].setPosition(x, y);
int ob_height= object.objects_sprites[p].getGlobalBounds().height;
int ob_width= object.objects_sprites[p].getGlobalBounds().width;
int point_x=object.objects_sprites[p].getGlobalBounds().left;
int point_y=object.objects_sprites[p].getGlobalBounds().top;
for (int i = point_y/tile_height; i < ob_height/tile_height; i++)
{
for (int j = point_x/tile_width; j < ob_width/tile_width; j++)
{
tiles_atributes[i][j].type = FieldType(7);
}
}
for (int i = point_y/tile_height; i < ob_height/tile_height; i++)
{
for (int j = point_x/tile_width; j < ob_width/tile_width; j++)
{
tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type]);
}
}
p++;
}
}
file.close();
}