c++ - 绘图时出现段错误

标签 c++ drawing sfml

当我绘制 Sprite 时,我的窗口会关闭。

这绝对是它的绘图部分,因为当我把它放在外面时 我的代码,它工作正常,当然它不会绘制我的 Sprite 。

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

谢谢!

最佳答案

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

改变这个

window.draw(tiles[1]);

对此

window.draw(tiles[0]);

关于c++ - 绘图时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376541/

相关文章:

c++ - 应用程序未能正确初始化 (0xc0150002)

c++ - 如何在其他线程等待时阻塞线程

html - 当头部有 CSS 链接时,Firefox 不会阻止渲染 HTML

c# - 对于绘图应用程序,什么是好的 2D 图形框架?

c# - 'System.Drawing.Graphics.FromImage(System.Drawing.Image )' is a ' 方法'但像 'type' 一样使用

c++ - 如何在 while 循环中使用时钟

c++ - 如何自定义 QTabWidget 选项卡?

删除变量时出现 C++ 奇怪的编译错误

c++ - 如何检测是否有func。是一个常量?并标记其他功能。 constexpr 取决于它?

c++ - 如何识别输入组合?