diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-31 01:18:03 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-31 18:18:23 +0100 |
commit | c30eda289b5815b6b62799ef986eaf3c462fd72d (patch) | |
tree | 186861496bda98ac2d97bdec819125162a090ab0 /ui/svgIcon.cpp | |
parent | Add lunasvg (and plutovg) (diff) | |
download | ilt-c30eda289b5815b6b62799ef986eaf3c462fd72d.tar.bz2 ilt-c30eda289b5815b6b62799ef986eaf3c462fd72d.tar.xz ilt-c30eda289b5815b6b62799ef986eaf3c462fd72d.zip |
Add SvgIcon class
Based on Icon class, but constructor replaced with calls to lunasvg.
Diffstat (limited to 'ui/svgIcon.cpp')
-rw-r--r-- | ui/svgIcon.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/ui/svgIcon.cpp b/ui/svgIcon.cpp new file mode 100644 index 0000000..499d9cc --- /dev/null +++ b/ui/svgIcon.cpp @@ -0,0 +1,34 @@ +#include "svgIcon.h" +#include "gl_traits.h" +#include <resource.h> + +SvgIcon::SvgIcon(ImageDimensions dim, const std::filesystem::path & path) +{ + const auto svgDoc = lunasvg::Document::loadFromFile(Resource::mapPath(path).native()); + if (!svgDoc) { + throw std::runtime_error("Failed to load SVG from " + path.string()); + } + + auto bitmap = svgDoc->renderToBitmap(dim.x, dim.y); + if (bitmap.isNull()) { + throw std::runtime_error("Failed to render SVG " + path.string()); + } + bitmap.convertToRGBA(); + + glBindTexture(GL_TEXTURE_2D, texture); + + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dim.x, dim.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap.data()); +} + +ImTextureID +SvgIcon::operator*() const +{ + static_assert(sizeof(glTexture) <= sizeof(ImTextureID)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr) This is how ImGui works + return reinterpret_cast<ImTextureID>(*texture); +} |