summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-10 11:40:30 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-10 11:40:30 +0100
commit2a59c1b43f0818e0f8e8ab4d11d9046426148338 (patch)
tree9da59a0c707852917c14b64bd519ceb236fb2e45
parent86c1bc8b9b5d8ad5fbfef85f34658c1da73baa1f (diff)
editor, main: add fmtlib dependency
snprintf in imgui code is slow.
-rw-r--r--.gitmodules3
-rw-r--r--compat/format.hpp20
-rw-r--r--editor/CMakeLists.txt2
-rw-r--r--editor/imgui-raii.hpp15
-rw-r--r--editor/imgui.cpp14
-rw-r--r--external/CMakeLists.txt19
m---------external/fmt0
-rw-r--r--main/CMakeLists.txt2
8 files changed, 58 insertions, 17 deletions
diff --git a/.gitmodules b/.gitmodules
index 0dbe42d2..b24cda5b 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -19,3 +19,6 @@
[submodule "sdl2"]
path = external/sdl2
url = https://github.com/libsdl-org/SDL.git
+[submodule "external/fmt"]
+ path = external/fmt
+ url = https://github.com/fmtlib/fmt.git
diff --git a/compat/format.hpp b/compat/format.hpp
new file mode 100644
index 00000000..e126e77b
--- /dev/null
+++ b/compat/format.hpp
@@ -0,0 +1,20 @@
+#pragma once
+#include <fmt/core.h>
+#include <fmt/compile.h>
+
+namespace floormat {
+
+using namespace fmt::literals;
+
+template<std::size_t N, typename Fmt, typename... Xs>
+std::size_t snformat(char(&buf)[N], Fmt&& fmt, Xs&&... args)
+{
+ constexpr std::size_t n = N > 0 ? N - 1 : 0;
+ auto result = fmt::format_to_n(buf, n, std::forward<Fmt>(fmt), std::forward<Xs>(args)...);
+ const auto len = std::min(n, result.size);
+ if constexpr(N > 0)
+ buf[len] = '\0';
+ return len;
+}
+
+} // namespace floormat
diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt
index 0ce1773a..77a2a73e 100644
--- a/editor/CMakeLists.txt
+++ b/editor/CMakeLists.txt
@@ -9,7 +9,7 @@ else()
set_property(SOURCE "${res}" APPEND PROPERTY COMPILE_OPTIONS "-w")
endif()
-link_libraries(MagnumIntegration::ImGui)
+link_libraries(MagnumIntegration::ImGui fmt::fmt)
add_executable(${self} "${sources}" "${res}" "../loader/loader-impl.cpp")
target_link_libraries(${self} ${PROJECT_NAME}-main)
diff --git a/editor/imgui-raii.hpp b/editor/imgui-raii.hpp
index ab317095..5920e7d9 100644
--- a/editor/imgui-raii.hpp
+++ b/editor/imgui-raii.hpp
@@ -2,9 +2,8 @@
#include <Corrade/Containers/StringView.h>
#include <Magnum/Math/Color.h>
-#ifndef __CLION_IDE__zz
#include <imgui.h>
-#endif
+#include <imgui_internal.h>
namespace floormat::imgui {
@@ -82,4 +81,16 @@ struct raii_wrapper final
return {[]{ ImGui::PopStyleColor(); }};
}
+[[maybe_unused]] static inline void text(const char* str, std::size_t len, ImGuiTextFlags_ flags = ImGuiTextFlags_NoWidthForLargeClippedText)
+{
+ ImGui::TextEx(str, str + len, flags);
+}
+
+template<std::size_t N>
+[[maybe_unused]] static inline void text(const char (&buf)[N], ImGuiTextFlags_ flags = ImGuiTextFlags_NoWidthForLargeClippedText)
+{
+ static_assert(N > 0);
+ ImGui::TextEx(buf, buf + N - 1, flags);
+}
+
} // namespace floormat::imgui
diff --git a/editor/imgui.cpp b/editor/imgui.cpp
index 84827ba3..b29c4f6c 100644
--- a/editor/imgui.cpp
+++ b/editor/imgui.cpp
@@ -1,6 +1,7 @@
#include "app.hpp"
#include "floormat/main.hpp"
#include "src/tile-atlas.hpp"
+#include "compat/format.hpp"
#include <Magnum/GL/Renderer.h>
#include "imgui-raii.hpp"
@@ -104,11 +105,11 @@ static void draw_editor_pane_atlas(tile_editor& ed, StringView name, const std::
if (ed.is_atlas_selected(atlas))
{
ImGui::SameLine();
- ImGui::Text(" (selected)");
+ text(" (selected)");
}
- std::snprintf(buf, sizeof(buf), "%zu", N);
+ const auto len = snformat(buf, FMT_COMPILE("{:d}"), N);
ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4);
- ImGui::Text("%s", buf);
+ text(buf, len);
};
if (const auto flags = ImGuiTreeNodeFlags_(ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Framed);
auto b = tree_node(name.data(), flags))
@@ -123,7 +124,6 @@ static void draw_editor_pane_atlas(tile_editor& ed, StringView name, const std::
for (std::size_t i = 0; i < N; i++)
{
const bool selected = ed.is_tile_selected(atlas, i);
-
if (i > 0 && i % per_row == 0)
ImGui::NewLine();
@@ -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{},
};
- std::snprintf(buf, sizeof(buf), "##item_%zu", i);
+ snformat(buf, FMT_COMPILE("##item_{}"), 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;
- snprintf(buf, sizeof(buf), "%.1f FPS", hz);
+ snformat(buf, FMT_COMPILE("{:.1f} FPS"), 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();
- snprintf(buf, sizeof(buf), "%hd:%hd - %hhu:%hhu", chunk.x, chunk.y, local.x, local.y);
+ snformat(buf, FMT_COMPILE("{}:{} - {}:{}"), chunk.x, chunk.y, local.x, local.y);
const auto size = ImGui::CalcTextSize(buf);
const auto window_size = M->window_size();
diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt
index fe774f8b..45e5728b 100644
--- a/external/CMakeLists.txt
+++ b/external/CMakeLists.txt
@@ -42,12 +42,14 @@ if(FLOORMAT_SUBMODULE-DEPENDENCIES)
NLOHMANN_JSON_CONFIG_INSTALL_DIR "share/cmake/nlohmann_json"
)
sets(BOOL
- JSON_Diagnostics ON
- JSON_Install ON
- JSON_MultipleHeaders ON
- JSON_SystemInclude ON
- )
- sets(BOOL
+
+ FMT_SYSTEM_HEADERS ON
+
+ JSON_Diagnostics ON
+ JSON_Install ON
+ JSON_MultipleHeaders ON
+ JSON_SystemInclude ON
+
CORRADE_BUILD_STATIC OFF
CORRADE_BUILD_TESTS OFF
CORRADE_BUILD_DEPRECATED OFF
@@ -154,6 +156,7 @@ if(FLOORMAT_SUBMODULE-DEPENDENCIES)
add_subdirectory(magnum)
add_subdirectory(magnum-plugins)
add_subdirectory(magnum-integration)
+ add_subdirectory(fmt)
endif()
if(FLOORMAT_SUBMODULE-DEPENDENCIES)
@@ -171,4 +174,8 @@ find_package(Magnum QUIET REQUIRED)
find_package(MagnumPlugins QUIET REQUIRED)
find_package(MagnumIntegration QUIET REQUIRED ImGui)
+if(NOT FLOORMAT_SUBMODULE-DEPENDENCIES)
+ find_package(fmt QUIET REQUIRED)
+endif()
+
fm_run_hook(fm-userconfig-external-post)
diff --git a/external/fmt b/external/fmt
new file mode 160000
+Subproject d2e89c8b080394e996d449371267365c223ca76
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 66737793..1ad37734 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -2,7 +2,7 @@ set(self ${PROJECT_NAME}-main)
file(GLOB sources "*.cpp" CONFIGURE_ARGS)
link_libraries(${PROJECT_NAME})
-link_libraries(Magnum::Sdl2Application Corrade::Containers Magnum::GL Magnum::Trade)
+link_libraries(Magnum::Sdl2Application Corrade::Containers Magnum::GL Magnum::Trade fmt::fmt)
add_library(${self} STATIC "${sources}")