Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SF-SVG, simple SVG-XML library  (Read 6167 times)

0 Members and 1 Guest are viewing this topic.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
SF-SVG, simple SVG-XML library
« on: December 10, 2016, 08:17:17 pm »
Hi!

Some people asked about SVG support in SFML and i thought it's pretty nice idea. Well, this library actually provides some other features, but it's primary purpose is to draw / rasterize SVG graphics.

Here's what it can do:
  • SVG drawing / rasterization;
  • Bezier square / cubic curves;
  • Some debug features for Bezier curves;
  • Bezier curves inherit sf::Shape, so they can be stored in containers like std::vector<sf::Shape*> etc. to manage multiple shapes.

Example code
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <SFC/Svg.hpp>

int main() {
        /* Create image */
        sfc::SVGImage img;

        /* Don't show debug lines */
        img.setMode(sfc::DrawMode::NORMAL);

        /* Load SVG image from file */
        img.loadFromFile("media/car.svg");

        /* Rasterize image & save it to file */
        img.rasterize().saveToFile("rasterized.png");

        /* Move it by [10, 10] to make it more visible */
        img.move({10, 10});

        /* Create window */
        sf::RenderWindow app(sf::VideoMode((img.getSize().x + 20) * 1, (img.getSize().y + 20) * 1), "sf-svg");

        while(app.isOpen()) {
                /* Handle events */
                for(sf::Event ev; app.pollEvent(ev);) {
                        if(ev.type == sf::Event::Closed) {
                                app.close();
                        }
                }

                /* Clear window */
                app.clear({20, 20, 20});

                /* Draw SVG file */
                app.draw(img);

                /* Display */
                app.display();
        }
        return 0;
}
 
Result:
http://imgur.com/a/fqUJo

And here is documentation: https://koczurekk.github.io/sf-svg/

//edit: To keep it clear, I use my fork of nanosvg to manage SVG images.
« Last Edit: December 10, 2016, 08:27:46 pm by korczurekk »

eugustus

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SF-SVG, simple SVG-XML library
« Reply #1 on: December 29, 2016, 01:04:29 am »
Nice. If I can load this as texture to OpenGL? I can try on future.

Sory for bad english...

roccio

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: SF-SVG, simple SVG-XML library
« Reply #2 on: December 29, 2016, 04:17:26 pm »
I have tryied this and it seems a good implementation. Just one question. The rasterizer works very well, I can see the image in ull colors, but if I draw the svg I can only see lines. Is there a way to draw the svg with all it's colours?

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: SF-SVG, simple SVG-XML library
« Reply #3 on: January 05, 2017, 03:43:16 pm »
Nice. If I can load this as texture to OpenGL? I can try on future.
I don't really know, I'm not 'that OpenGL guy'. But you can convert it to image and then to sf::Texture, is it good?

I have tryied this and it seems a good implementation. Just one question. The rasterizer works very well, I can see the image in ull colors, but if I draw the svg I can only see lines. Is there a way to draw the svg with all it's colours?
You have to create texture from rasterized image and then attach to sprite etc.

eugustus

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: SF-SVG, simple SVG-XML library
« Reply #4 on: January 07, 2017, 09:24:32 am »
I don't really know, I'm not 'that OpenGL guy'. But you can convert it to image and then to sf::Texture, is it good?

Very :-)