绘图时出现分段错误

时间:2017-04-12 17:59:31

标签: c++ drawing sfml

当我画一个精灵时,我的窗口关闭。

这绝对是它的绘图部分,因为当我把它排除在外时 我的代码,它工作正常,但它当然不会绘制我的精灵。

我在运行时遇到此错误: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;
}

谢谢!

1 个答案:

答案 0 :(得分:1)

您正在尝试访问不存在的向量上的元素。

更改此

window.draw(tiles[1]);

到这个

window.draw(tiles[0]);
相关问题