From dbc200043493d69d76e7c3e3177d8d7f64e9ae62 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 8 Jul 2025 00:55:45 -0400 Subject: [PATCH] Started a map icon gen tool that will load the fonts and create tile sprites for everything I use. --- meson.build | 7 ++++++ tools/icongen.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 tools/icongen.cpp diff --git a/meson.build b/meson.build index bbd938c..a2ee37e 100644 --- a/meson.build +++ b/meson.build @@ -169,6 +169,13 @@ executable('zedcaster', override_options: exe_defaults, dependencies: dependencies) +executable('icongen', + [ 'dbc.cpp', 'tools/icongen.cpp' ], + cpp_args: cpp_args, + link_args: link_args, + override_options: exe_defaults, + dependencies: dependencies) + executable('fragviewer', [ 'textures.cpp', 'config.cpp', 'dbc.cpp', 'tools/fragviewer.cpp' ], diff --git a/tools/icongen.cpp b/tools/icongen.cpp new file mode 100644 index 0000000..0070f67 --- /dev/null +++ b/tools/icongen.cpp @@ -0,0 +1,62 @@ +#include +#include "dbc.hpp" +#include +#include +#include "constants.hpp" + +int main() { + unsigned int font_size = 30; + sf::Vector2u size{32, 32}; + + sf::RenderTexture render{size}; + render.setSmooth(false); + + sf::Font font{FONT_FILE_NAME}; + font.setSmooth(false); + + wchar_t display_char = 3848; + std::wstring content{display_char}; + sf::Text icon{font, content, font_size}; + + icon.setFillColor({0, 0, 0, 255}); + render.draw(icon); + render.clear({0,0,0,0}); + + // fit the glyph in our box height + auto glyph = font.getGlyph(content[0], font_size, false); + + while(glyph.textureRect.size.y < int(size.y)-1) { + font_size++; + glyph = font.getGlyph(content[0], font_size, false); + } + + auto font_texture = font.getTexture(font_size); + sf::Sprite sprite{font_texture, glyph.textureRect}; + auto t_size = glyph.textureRect.size; + + dbc::check(int(size.x - t_size.x) > 0, "font too big on x"); + dbc::check(int(size.y - t_size.y) > 0, "font too big on y"); + + sf::Vector2f center{ + (float(size.x) - float(t_size.x)) / 2.0f, + (float(size.y) - float(t_size.y)) / 2.0f}; + + sf::Vector2f scale{float(size.x) / float(t_size.x), float(size.y) / float(t_size.y)}; + + fmt::println("scale={},{}; t_size={},{}; size={},{}", + scale.x, scale.y, t_size.x, t_size.y, size.x, size.y); + + sprite.setScale(scale); + + sprite.setPosition({0,0}); + sprite.setColor({0, 0, 0, 255}); + + render.draw(sprite); + render.display(); + sf::Image out_img = render.getTexture().copyToImage(); + + bool worked = out_img.saveToFile("./screenshot.png"); + dbc::check(worked, "Failed to write screenshot.png"); + + return 0; +}