C++ 如何利用单个缓冲区进行创作艺术

C++ 如何利用单个缓冲区进行创作艺术,c++,textures,rendering,sfml,C++,Textures,Rendering,Sfml,我实际上面临着一个我自己无法解决的问题,使用SFML我有一个单一的形状,我在每一帧移动它,然后我画它。问题是,SFML使用两个缓冲区,每个缓冲区(帧)上的轨迹将不相同,并且会产生奇怪的闪烁效果 我需要你的知识,如果我能找到另一种方法,比如我将扩展的一系列形状。还是有办法跳过第二个缓冲区?把这个缓冲器做成一张纸,我的形状变成刷子 sf::CircleShape circle; //Init the circle (radius etc) sf::Vector2f pos(0, 500); whi

我实际上面临着一个我自己无法解决的问题,使用SFML我有一个单一的形状,我在每一帧移动它,然后我画它。问题是,SFML使用两个缓冲区,每个缓冲区(帧)上的轨迹将不相同,并且会产生奇怪的闪烁效果

我需要你的知识,如果我能找到另一种方法,比如我将扩展的一系列形状。还是有办法跳过第二个缓冲区?把这个缓冲器做成一张纸,我的形状变成刷子

sf::CircleShape circle;
//Init the circle (radius etc)
sf::Vector2f pos(0, 500);

while(!done)
{
  pos.x += 1;
  circle.setPosition(pos);
  window.draw(circle);
  window.display();
}

祝您度过愉快的一天。

双缓冲的目的是避免在渲染帧时显示未完全渲染的部分场景。因此,您的问题与双缓冲本身无关。基本上,每次都需要生成渲染特定帧所需的所有基本体。在您的具体情况下,您可以这样实现:

sf::CircleShape circle;
//Init the circle (radius etc)
sf::Vector2f pos(0, 500);

int frameCount = 0;
while(!done)
{
    ++frameCount;
    for (int i = 0; i < frameCount; ++i) {
        circle.setPosition(pos + sf::Vector2f(i, 0));
        window.draw(circle);
    }
    window.display();
}
sf::圆形;
//初始圆(半径等)
矢量2f位置(0500);
int frameCount=0;
而(!完成)
{
++帧数;
对于(int i=0;i
您可以通过使用
sf::RenderTexture
来实现这一点,它充当后台缓冲区(单个缓冲区)来制作图形,然后通过sf::RenderWindow在屏幕上绘制

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML art");
    window.setVerticalSyncEnabled(true);

    sf::RenderTexture target;
    if (!target.create(window.getSize().x, window.getSize().y))
        return -1;

    target.clear(sf::Color::White);
    target.display();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            sf::CircleShape circle(5.f, 32u);
            circle.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
            circle.setFillColor(sf::Color::Red);
            target.draw(circle, sf::BlendNone);
            target.display();
        }

        window.clear();
        window.draw(sf::Sprite(target.getTexture()));
        window.display();
    }
}
#包括
int main()
{
sf::RenderWindow窗口(sf::VideoMode(800600),“SFML艺术”);
window.setVerticalSyncEnabled(真);
sf::渲染器目标;
如果(!target.create(window.getSize().x,window.getSize().y))
返回-1;
目标。清晰(sf::颜色::白色);
target.display();
while(window.isOpen())
{
sf::事件;
while(window.pollEvent(事件))
{
如果(event.type==sf::event::Closed)
window.close();
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sf::圆形(5.f,32u);
circle.setPosition(sf::Vector2f(sf::Mouse::getPosition(窗口));
circle.setFillColor(sf::Color::Red);
绘制(圆形,sf::BlendNone);
target.display();
}
window.clear();
draw(sf::Sprite(target.getTexture());
window.display();
}
}
您必须为每一帧调用
clear()
。这不是可选的。如果要保留轨迹,请使用@Verman建议的渲染纹理。