summaryrefslogtreecommitdiff
path: root/ui/svgIcon.cpp
blob: 2c73b5d882719a695c0f42bf9d2f7e9b2710b30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "svgIcon.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();

	texture.storage(1, GL_RGBA8, dim);
	texture.parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
	texture.parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
	texture.parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	texture.parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	texture.image(dim, GL_RGBA, GL_UNSIGNED_BYTE, bitmap.data());
}

ImTextureID
SvgIcon::operator*() const
{
	static_assert(sizeof(glTexture<GL_TEXTURE_2D>) <= sizeof(ImTextureID));
	// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr) This is how ImGui works
	return reinterpret_cast<ImTextureID>(*texture);
}