在 C++ 中渲染矢量图形 (.svg)
我和一个朋友正在开发一款 2D 游戏,其中图形将是 .svg 文件,我们将通过首先光栅化它们或直接将它们渲染在表面上(在某些时候仍然需要光栅化)来适当缩放它们。
问题是,我一整天都在寻找一个库,它允许我获取 .svg 文件并最终让它在 allegro 中渲染。据我所知,这将涉及将光栅化为 allegro 可以读取的某种格式,然后 allegro 可以渲染“扁平化”图像。
那么我可以使用哪些 C++ 库来获取 .SVG 文件并“展平”它以便我可以渲染它?该库显然也需要支持缩放,以便我可以缩放矢量图形然后对其进行光栅化。
我使用的是 Windows 和 Visual C++ Express 2010。
我尝试过 Cairo,但它只允许写入 .svg 文件,不允许读取 .svg 文件。我还研究了与 Cario 一起使用的 librsvg,但要让它在 Windows 上正常工作遇到了很多麻烦(因为它有大量的 GNOME 依赖项)。如果您有任何使这些工作(在 Windows 上)的指南,那就太好了。
My and a friend are working on a 2D game where the graphics will be .svg files and we will scale them appropriately either by rasterizing them first, or rendering them directly on a surface (which still would require rasterization at some point).
The problem is, I've been looking all day to find a library that will allow me to take an .svg file and eventually get it to render in allegro. As far as I know, it would involve rasterization into some sort of format that allegro can read and then allegro could render the "flattened" image.
So what are some C++ libraries I could use for taking an .SVG file and "flattening" it so I can render it? The library obviously needs to support scaling too so I can scale the vector graphic then rasterize it.
I'm using Windows and Visual C++ Express 2010.
I've tried Cairo, but it only allows writing of .svg files and doesn't allow you to read the .svg file. I've also looked into librsvg which works with Cario, but I was having a lot of trouble getting it to work properly on Windows (because it has loads of GNOME dependencies). If you have any guides for getting these to work (on Windows) that would be great too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
wxsvg 库 允许加载和操作 SVG 文件。 Qt 还有一个 SVG 模块。
The wxsvg library allows loading and manipulating SVG files. Qt also has an SVG module.
我来的有点晚了,但我建议你看看 Nano SVG,非常轻量级的 svg 渲染器,不需要 cairo/libsvg。我在几个小时内编译并运行了 nanosvg。这是非常基本的,但它可以完成工作。
I'm coming a little late to the conversation, but I would suggest you to look at Nano SVG, an extremely lightweight svg renderer that doesn't need cairo/libsvg. I got nanosvg compiled and working in a couple of hours. It's very basic, but it gets the job done.
https://github.com/sammycage/lunasvg 是一个很好的 svg 解析、渲染和操作库。它是用纯c++编写的
https://github.com/sammycage/lunasvg is a nice svg parsing, rendering and manipulating library. It is written with pure c++
SVG++ 库 为 SVG 读取提供高级支持,以便可以在合理的时间内实现使用 allegro 渲染 SVG。
SVG++ library provides advanced support for SVG reading, so that rendering SVG with allegro can be implemented in reasonable time.
我最近用 C++ 编写了一个 SVG 渲染器库:
https://github.com/igagis/svgren
它使用 AGG 渲染到离屏表面。
支持渐变和各种形状。
I have recently put together an SVG renderer library in C++:
https://github.com/igagis/svgren
It uses AGG for rendering to off-screen surface.
Supports gradients and all kind of shapes.
就我个人而言,我在 Simple Viewer GL 中使用 NanoSVG。它使我能够通过几行代码轻松加载和光栅化 SVG 图像。但这个库对 SVG 支持很弱。
Personally, I using NanoSVG in my Simple Viewer GL. It's allowing me easy to load and rasterize SVG images in few lines of code. But this library has weak SVG support.
在
nanosvg
和许多其他 c++ svg 解析器的帮助下,添加 svg 渲染您的应用程序的功能应该是微不足道的。配方如下:svg 解析器 + 矢量渲染库 = 简单的 svg 渲染。矢量渲染库可以是cairo
和许多其他库(nanovg
以及许多其他vg< /代码> 库)。下面是一个示例,说明如何使用 cairo + fltk + nanosvg 组合支持 svg 渲染。现在,所有 svg 解析器以及 cairo 本身以及其他渲染器都存在错误/缺点,但基本的 svg 支持永远不会出现问题。
With the help of
nanosvg
and many other c++ svg parsers, adding svg rendering capability to your application should be trivial. The recipe is as follows: svg parser + vector rendering library = trivial svg rendering. The vector rendering library can becairo
and a number of other libraries (nanovg
comes to mind, as well as a number of othervg
libraries). Here's an example of how to support svg rendering with cairo + fltk + nanosvg combo. Now, all the svg parsers, as well as cairo itself, along with other renderers, have bugs/shortcomings, but basic svg support should never present a problem.我一直在寻找一种真正快速的方法来在基于 Windows 操作系统的 MFC 项目中渲染 SVG 文件。
在这种情况下,Microsoft 浏览器 Web 浏览器 ActiveX 控件被认为是理想的解决方案。
这是使用浏览器控件加载 SVG 文件的结果。
I was looking for a real quick way to render SVG file in a Windows OS based MFC project.
In that case, Microsoft Browser Web Browser ActiveX Control is found to be an ideal solution.
And here is the result of loading SVG file using the Browser control.