0绘图子画面矢量

0绘图子画面矢量

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

问题描述

我想做一个循环,在屏幕上绘制10块,但没有什么显示,我没有错误,所以我认为矢量是没有存储精灵,新的SFML,所以我真的不知道我在做什么错误。如果有人可以帮助我,我会真正感激。

im trying to do a loop to draw 10 blocks on the screen but nothing is showing, i got no error so i think that the vector is no storing the sprites, im new in SFML so i realy don know what am i doing wrong. If someone can help me i will realy appreciate

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


推荐答案

最终答案:如果要使用SFML显示 png 文件,请将它们保存为8位。

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绘图子画面矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:33