SFML 2.0 – 用精灵向量绘图 [英] SFML 2.0 – drawing with vector of sprites

查看:57
本文介绍了SFML 2.0 – 用精灵向量绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a loop to draw 10 blocks on the screen, but nothing is showing. I got no error, so I think that the vector is not storing the sprites. I'm new to SFML, so I don't really know what I'm doing wrong.

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]);
}

推荐答案

2nd edit with final answer : 如果你想用 SFML 显示 png 文件,请将它们保存为 8bit.

2nd edit with final answer : if you want to display png files with SFML, save them 8bit.

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

I had some bad copy/paste in the second code, I fixed it

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

As SFML is made for multi media applications (mostly games), you need to refresh and draw to screen many times by second (that's frames). That being said, the basic approach is to have a main loop doing 3 things : handling inputs, updating your game logic and then drawing.

请参阅 SFML 网站上的经典示例:

See the classic example from SFML's website :

#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 之间你需要绘制所有内容你想显示(你的块).

Your texture loading and filling the vector have to be done before the main loop, and then between window.clear() and window.display you need to draw everything you want to display (your blocks).

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

You may end up with something like this :

#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;
}

这篇关于SFML 2.0 – 用精灵向量绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆