Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to draw a Drawable and get a bitmap from it?

Tags:

c++

sfml

Is it possible to get a drawn Drawable as a Texture (a bitmap)? How could I do that, please?


My try

I've modified the green circle example. Now it's really drawn as a bitmap...

But it's drawn just like that:

Unsmooth Arc.

I'd like to have anti-aliasing.

With the RenderWindow class I was able to put the anti-aliasing, by passing a ContextSettings. Using @Mario's suggestion I need RenderTexture, and I don't have control over its ContextSettings, unfortunately.

@AlexG's suggestion

I've created a Context, but my compiler says my_test.cc:9:57: error: use of deleted function 'sf::Context::Context(const sf::Context&)'. Err! Any alternative?

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main()
{
    sf::ContextSettings settings =
        sf::ContextSettings(0, 0, 6);

    sf::Context context = sf::Context(settings, 200, 200);
    context.setActive(true);

    sf::RenderWindow window(
        sf::VideoMode(200, 200), "sfml test", sf::Style::Default,
        settings
    );

    sf::RenderTexture cacheTexture;
    if (!cacheTexture.create(200, 200)) return 0;
    cacheTexture.setSmooth(true);

    sf::CircleShape shape(100.f, 75);
    shape.setFillColor(sf::Color::Green);

    cacheTexture.setActive(true);
    cacheTexture.draw(shape);

    cacheTexture.setActive(false);
    context.setActive(false);

    sf::Sprite sprite = sf::Sprite(cacheTexture.getTexture());

    while (window.isOpen())
    {
        sf::Event event;

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

        window.clear();
        window.draw(sprite);
        window.display();
    }

    return 0;
}
like image 591
Klaider Avatar asked Sep 04 '17 12:09

Klaider


2 Answers

You can draw the shape to an sf::RenderTexture just like @Mario says. If you pass your context settings to a sf::Context you can set the antialiasing level just as you did the window (as long as the sf::RenderTexture is the current context).

Hope that helps!

like image 139
Alex G Avatar answered Oct 22 '22 12:10

Alex G


It's pretty trivial to do. Just draw your drawable to a sf::RenderTexture of the desired size and you can use sf::RenderTexture::getTexture() to get a texture you can use or save to a file.

like image 26
Mario Avatar answered Oct 22 '22 12:10

Mario