From 6e94848aec5d42eed5f9e1f5548ade26915ce90b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 4 Jun 2024 21:14:45 +0100 Subject: Tidy many stringy messes with std::format --- ui/font.cpp | 79 +++++++++++++++++++++++++++++++---------------------------- ui/window.cpp | 3 ++- 2 files changed, 43 insertions(+), 39 deletions(-) (limited to 'ui') diff --git a/ui/font.cpp b/ui/font.cpp index b6669b0..ebd29d0 100644 --- a/ui/font.cpp +++ b/ui/font.cpp @@ -1,6 +1,7 @@ #include "font.h" #include #include +#include #include #include FT_FREETYPE_H #include "gl_traits.h" @@ -15,49 +16,51 @@ // IWYU pragma: no_forward_declare FT_LibraryRec_ -std::string -FT_Error_StringSafe(FT_Error err) -{ - if (const auto errstr = FT_Error_String(err)) { - return {errstr}; +namespace { + std::string + FT_Error_StringSafe(FT_Error err) + { + if (const auto errstr = FT_Error_String(err)) { + return {errstr}; + } + return std::format("Unknown FT error: {}", err); } - return std::to_string(err); -} -template -void -FT_Check(Args &&... args) -{ - if (const auto err = Func(std::forward(args)...)) { - throw std::runtime_error {std::string {"FreeType error: "} + FT_Error_StringSafe(err)}; + template + void + FT_Check(Args &&... args) + { + if (const auto err = Func(std::forward(args)...)) { + throw std::runtime_error {std::format("FreeType error: {}", FT_Error_StringSafe(err))}; + } } -} -const std::string BASIC_CHARS = []() { - std::string chars; - for (char c {}; c >= 0; c++) { - if (isgraph(c)) { - chars += c; + const std::string BASIC_CHARS = []() { + std::string chars {"£€²³"}; + for (char c {}; c >= 0; c++) { + if (isgraph(c)) { + chars += c; + } } - } - return chars + "£€²³"; -}(); - -using FT = glRef(&ft); - return ft; - }, - FT_Done_FreeType>; - -using Face = glRef(ft, name, 0, &face); - return face; - }, - FT_Done_Face>; + return chars; + }(); + + using FT = glRef(&ft); + return ft; + }, + FT_Done_FreeType>; + + using Face = glRef(ft, name, 0, &face); + return face; + }, + FT_Done_Face>; +} Font::Font(std::filesystem::path p, unsigned s) : path {std::move(p)}, size {getTextureSize(s)} { diff --git a/ui/window.cpp b/ui/window.cpp index 6855aa0..dd488d7 100644 --- a/ui/window.cpp +++ b/ui/window.cpp @@ -1,5 +1,6 @@ #include "window.h" #include "uiComponent.h" +#include #include #include #include @@ -9,7 +10,7 @@ Window::GLInitHelper::GLInitHelper() [[maybe_unused]] static auto init = []() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) if (const auto version = gladLoadGL(reinterpret_cast(SDL_GL_GetProcAddress)); version < 30003) { - throw std::runtime_error {"Insufficient OpenGL version: " + std::to_string(version)}; + throw std::runtime_error {std::format("Insufficient OpenGL version: {}", version)}; } else { return version; -- cgit v1.2.3