【问题标题】:SFML 2.0 – drawing with vector of spritesSFML 2.0 – 使用精灵矢量绘图
【发布时间】:2013-05-31 10:28:06
【问题描述】:

我正在尝试创建一个循环以在屏幕上绘制 10 个块,但没有显示任何内容。我没有错误,所以我认为向量没有存储精灵。我是 SFML 的新手,所以我真的不知道我做错了什么。

sf::Texture bTexture;
sf::Texture bloqueTexture;
sf::Sprite bloqueSprite;

//create vector of blocks
std::vector<sf::Sprite> bricks(10, sf::Sprite(bloqueTexture));

fondo.setTexture(img_mgr.getImage("fondo.jpg"));
personaje.setTexture(img_mgr.getImage("jumper.png"));
personaje.setPosition(100,POSICION_TERRENO_Y);
bloqueSprite.setTexture(img_mgr.getImage("bloque.png"));
bloqueTexture.loadFromFile("Recursos/imagenes/bloque.png");

//Fill the vector with the texture
for (int i = 0; i < bricks.size(); i++)
{
    bricks[i].setTexture(bloqueTexture);
    bricks[i].setPosition(100 + (i * 45) , 320);
    window.draw(bricks[i]);
}

【问题讨论】:

    标签: c++ sfml


    【解决方案1】:

    我认为问题在于加载纹理,尝试检查 loadFromFile 函数是否返回 true。

    【讨论】:

    • 我尝试添加 if (!bloqueTexture.loadFromFile("Recursos/imagenes/bloque.png")) return EXIT_FAILURE;并且没有错误。
    • hmm 尝试从 SFML 网站运行示例并查找代码中的差异
    【解决方案2】:

    第二次编辑最终答案:如果你想用 SFML 显示png 文件,请将它们保存为 8 位。

    编辑:我在第二个代码中有一些错误的复制/粘贴,我已修复它

    由于 SFML 是为多媒体应用程序(主要是游戏)而设计的,因此您需要每秒多次刷新和绘制屏幕(即frames)。话虽如此,基本的方法是让一个主循环做 3 件事:处理输入、更新游戏逻辑,然后绘制。

    参见 SFML 网站上的经典示例:

    #include <SFML/Graphics.hpp>
    
    int main()
    {
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
    
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
            }
    
            window.clear();
            window.draw(shape);
            window.display();
        }
    
        return 0;
    }
    

    你的纹理加载和向量填充必须在主循环之前完成,然后在window.clear()window.display之间你需要绘制你想要显示的所有东西(你的块)。

    你可能会得到这样的结果:

    #include <SFML/Graphics.hpp>
    
    int main()
    {
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    
        sf::Texture bTexture;
        sf::Texture bloqueTexture;
        sf::Sprite bloqueSprite;
    
        //create vector of blocks
        std::vector<sf::Sprite> bricks(10, sf::Sprite(bloqueTexture));
    
        fondo.setTexture(img_mgr.getImage("fondo.jpg"));
        personaje.setTexture(img_mgr.getImage("jumper.png"));
        personaje.setPosition(100,POSICION_TERRENO_Y);
        bloqueSprite.setTexture(img_mgr.getImage("bloque.png"));
        bloqueTexture.loadFromFile("Recursos/imagenes/bloque.png");
    
        for (int i = 0; i < bricks.size(); i++)
        {
            bricks[i].setTexture(bloqueTexture);
            bricks[i].setPosition(100 + (i * 45) , 320);
        }
    
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
            }
    
            window.clear();
            for (int i = 0; i < bricks.size(); i++)
            {
                window.draw(bricks[i];
            }
            // Consider doing this :
            // for(const auto& brick : bricks)
            //    window.draw(brick);
    
            window.display();
    
        }
    
        return 0;
    }
    

    【讨论】:

    • 我的代码像你说的那样放置并且不起作用,没有错误。
    • @Ishidad 你在屏幕上有什么渲染吗?如果您的 .jpg 渲染而不是 .png 我建议您将它们保存在 8 位、16 或 24 位 png 中效果不佳。您也可以尝试直接在 main 中加载纹理,看看问题是否来自您的图像管理器。
    • 完成,我添加了纹理 JPG 文件,这似乎可行。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多