c++ - 如何为生成艺术目的在单个缓冲区上绘制

标签 c++ textures rendering sfml

我实际上面临着一个我自己无法解决的问题,使用 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::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();
    }
}

关于c++ - 如何为生成艺术目的在单个缓冲区上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33336272/

相关文章:

c++ - QML textInput 元素中的自动完成和建议

时间:2019-03-09 标签:c++iomaniptableformatting

css - Firefox 渲染错误,但达到了预期的效果

javascript - Firefox 中的 SVG ClipPath 渲染错误

c++ - C++ 中单冒号 ":"的重要性

c++ - 关于 C++ 重载运算符

c++ - 创建纹理后,OpenGL 抗锯齿形状

ios - ios 中的 three.js 视频纹理在单独启动的播放器中播放 - 想法?

c++ - Direct3D 11:如何访问 CPU 端内存中的立方体贴图面(ID3D11DeviceContext::Map 应该与子资源一起使用吗?)?

optimization - 浏览器渲染的最佳图像尺寸