summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-10 13:08:28 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-10 13:08:39 +0100
commita60021403a35802d3e0a404ffea969f1492f91b2 (patch)
tree868e2c987e22255976990cbbbe3c11869f5f9fd3
parent4e22a206f3106ef0ff7cc518d7ee2933481e125a (diff)
add a working fmtlib operator""_cf()
-rw-r--r--compat/format.hpp22
-rw-r--r--editor/imgui.cpp8
2 files changed, 26 insertions, 4 deletions
diff --git a/compat/format.hpp b/compat/format.hpp
index d3b06521..0ebb3e18 100644
--- a/compat/format.hpp
+++ b/compat/format.hpp
@@ -2,8 +2,30 @@
#include <fmt/core.h>
#include <fmt/compile.h>
+namespace floormat::detail::fmt {
+
+template<std::size_t N>
+struct fmt_string final {
+ static constexpr std::size_t size = N;
+ char data[N];
+
+ template <std::size_t... Is>
+ consteval fmt_string(const char (&arr)[N]) noexcept {
+ for (std::size_t i = 0; i < N; i++)
+ data[i] = arr[i];
+ }
+};
+
+} // namespace floormat::detail::fmt
+
namespace floormat {
+template<detail::fmt::fmt_string s>
+consteval auto operator""_cf() noexcept
+{
+ return FMT_COMPILE(s.data);
+}
+
template<std::size_t N, typename Fmt, typename... Xs>
std::size_t snformat(char(&buf)[N], Fmt&& fmt, Xs&&... args)
{
diff --git a/editor/imgui.cpp b/editor/imgui.cpp
index b29c4f6c..df1f07b3 100644
--- a/editor/imgui.cpp
+++ b/editor/imgui.cpp
@@ -107,7 +107,7 @@ static void draw_editor_pane_atlas(tile_editor& ed, StringView name, const std::
ImGui::SameLine();
text(" (selected)");
}
- const auto len = snformat(buf, FMT_COMPILE("{:d}"), N);
+ const auto len = snformat(buf, "{:d}"_cf, N);
ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4);
text(buf, len);
};
@@ -134,7 +134,7 @@ static void draw_editor_pane_atlas(tile_editor& ed, StringView name, const std::
perm_selected ? push_style_color(ImGuiCol_ButtonHovered, color_perm_selected) : raii_wrapper{},
};
- snformat(buf, FMT_COMPILE("##item_{}"), i);
+ snformat(buf, "##item_{}"_cf, i);
const auto uv = atlas->texcoords_for_id(i);
constexpr ImVec2 size_2 = { TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f };
ImGui::ImageButton(buf, (void*)&atlas->texture(), size_2,
@@ -193,7 +193,7 @@ void app::draw_fps()
const auto frame_time = M->smoothed_dt();
char buf[16];
const double hz = frame_time > 1e-6f ? (int)std::round(10./(double)frame_time + .05) * .1 : 9999;
- snformat(buf, FMT_COMPILE("{:.1f} FPS"), hz);
+ snformat(buf, "{:.1f} FPS"_cf, hz);
const ImVec2 size = ImGui::CalcTextSize(buf);
ImDrawList& draw = *ImGui::GetForegroundDrawList();
draw.AddText({M->window_size()[0] - size.x - 4, 3}, ImGui::ColorConvertFloat4ToU32({0, 1, 0, 1}), buf);
@@ -208,7 +208,7 @@ void app::draw_tile_under_cursor()
const auto coord = *cursor.tile;
const auto chunk = coord.chunk();
const auto local = coord.local();
- snformat(buf, FMT_COMPILE("{}:{} - {}:{}"), chunk.x, chunk.y, local.x, local.y);
+ snformat(buf, "{}:{} - {}:{}"_cf, chunk.x, chunk.y, local.x, local.y);
const auto size = ImGui::CalcTextSize(buf);
const auto window_size = M->window_size();