绘图时出现分段错误
Segmentation fault when drawing
我的 window 在我绘制精灵时关闭。
这绝对是它的绘图部分,因为当我把它放在外面时
我的代码,它工作正常,当然它不绘制我的精灵。
我在 运行 时也收到此错误:Segmentation fault (core dumped)
我不知道那是什么意思:/ .
这是我的代码:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
//create vars
sf::Color bgColour(20, 175, 215);
vector<sf::Sprite> tiles;
void CreateTile(string Texture, int x, int y)
{
sf::Vector2f Pos(x, y);
sf::Texture Ftexture;
Ftexture.loadFromFile(Texture);
sf::Sprite Tile;
Tile.setTexture(Ftexture);
Tile.setPosition(Pos);
tiles.push_back(Tile);
}
int main()
{
//create window
sf::RenderWindow window(sf::VideoMode(800, 600), "-\\-(Game)-//-");
CreateTile("Recources/grass.png", 40, 40);
//main loop
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(bgColour);
window.draw(tiles[1]);
window.display();
}
return 0;
}
谢谢!
您正在尝试访问矢量上不存在的元素。
改变这个
window.draw(tiles[1]);
至此
window.draw(tiles[0]);
我的 window 在我绘制精灵时关闭。
这绝对是它的绘图部分,因为当我把它放在外面时 我的代码,它工作正常,当然它不绘制我的精灵。
我在 运行 时也收到此错误:Segmentation fault (core dumped)
我不知道那是什么意思:/ .
这是我的代码:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
//create vars
sf::Color bgColour(20, 175, 215);
vector<sf::Sprite> tiles;
void CreateTile(string Texture, int x, int y)
{
sf::Vector2f Pos(x, y);
sf::Texture Ftexture;
Ftexture.loadFromFile(Texture);
sf::Sprite Tile;
Tile.setTexture(Ftexture);
Tile.setPosition(Pos);
tiles.push_back(Tile);
}
int main()
{
//create window
sf::RenderWindow window(sf::VideoMode(800, 600), "-\\-(Game)-//-");
CreateTile("Recources/grass.png", 40, 40);
//main loop
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(bgColour);
window.draw(tiles[1]);
window.display();
}
return 0;
}
谢谢!
您正在尝试访问矢量上不存在的元素。
改变这个
window.draw(tiles[1]);
至此
window.draw(tiles[0]);