From 9da19582b5feb93258cc4e71fea05cd8f93d7588 Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 20:51:59 +0300 Subject: [PATCH 1/8] added files --- include/omath/hud/canvas_box.hpp | 23 ++++++ include/omath/hud/entity_overlay.hpp | 72 +++++++++++++++++++ include/omath/hud/hud_renderer_interface.hpp | 27 +++++++ .../renderer_realizations/imgui_renderer.hpp | 20 ++++++ source/hud/canvas_box.cpp | 27 +++++++ source/hud/entity_overlay.cpp | 9 +++ .../renderer_realizations/imgui_renderer.cpp | 9 +++ 7 files changed, 187 insertions(+) create mode 100644 include/omath/hud/canvas_box.hpp create mode 100644 include/omath/hud/entity_overlay.hpp create mode 100644 include/omath/hud/hud_renderer_interface.hpp create mode 100644 include/omath/hud/renderer_realizations/imgui_renderer.hpp create mode 100644 source/hud/canvas_box.cpp create mode 100644 source/hud/entity_overlay.cpp create mode 100644 source/hud/renderer_realizations/imgui_renderer.cpp diff --git a/include/omath/hud/canvas_box.hpp b/include/omath/hud/canvas_box.hpp new file mode 100644 index 0000000..021a775 --- /dev/null +++ b/include/omath/hud/canvas_box.hpp @@ -0,0 +1,23 @@ +// +// Created by orange on 13.03.2026. +// +#pragma once +#include "omath/linear_algebra/vector2.hpp" + +namespace omath::hud +{ + class CanvasBox final + { + public: + CanvasBox(Vector2 top, Vector2 bottom, float ratio = 4.f); + + [[nodiscard]] + std::array, 4> as_array() const; + + Vector2 top_left_corner; + Vector2 top_right_corner; + + Vector2 bottom_left_corner; + Vector2 bottom_right_corner; + }; +} // namespace omath::hud \ No newline at end of file diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp new file mode 100644 index 0000000..fd120e1 --- /dev/null +++ b/include/omath/hud/entity_overlay.hpp @@ -0,0 +1,72 @@ +// +// Created by orange on 13.03.2026. +// +#pragma once +#include "canvas_box.hpp" +#include "hud_renderer_interface.hpp" +#include "omath/linear_algebra/vector2.hpp" +#include "omath/utility/color.hpp" +#include +#include +namespace omath::hud +{ + enum class SnapLineStartPosition + { + SCREEN_BOTTOM, + SCREEN_TOP, + SCREEN_CENTER, + }; + class EntityOverlay + { + public: + EntityOverlay(const Vector2& top, const Vector2& bottom, + const std::shared_ptr& renderer); + + void add_2d_box(const Color& box_color, const Color& fill_color = Color{0.f, 0.f, 0.f, 0.f}, + float thickness = 1.f) const; + + void add_cornered_2d_box(const Color& box_color, const Color& fill_color = Color{0.f, 0.f, 0.f, 0.f}, + float corner_ratio_len = 0.2f, float thickness = 1.f) const; + + void add_right_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width, + float ratio, float offset = 5.f); + + void add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, float width, + float ratio, float offset = 5.f) const; + + template + void add_right_label(const Color& color, const float offset, const bool outlined, + std::format_string fmt, Args&&... args) + { + const std::string label = std::vformat(fmt.get(), std::make_format_args(args...)); + + add_right_label(color, offset, outlined, std::string_view{label}); + } + + void add_right_label(const Color& color, float offset, bool outlined, const std::string_view& text); + + template + void add_top_label(const Color& color, const float offset, const bool outlined, std::format_string fmt, + Args&&... args) + { + const std::string label = std::vformat(fmt.get(), std::make_format_args(args...)); + + add_top_label(color, offset, outlined, std::string_view{label}); + } + + void add_top_label(const Color& color, float offset, bool outlined, std::string_view text); + + void add_top_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height, + float ratio, float offset = 5.f); + + void add_snap_line(const SnapLineStartPosition& start_pos, const Color& color, float width); + + private: + static void draw_outlined_text(const Vector2& position, const Color& color, + const std::string_view& text); + CanvasBox m_canvas; + Vector2 m_text_cursor_right; + Vector2 m_text_cursor_top; + std::shared_ptr renderer; + }; +} // namespace omath::hud \ No newline at end of file diff --git a/include/omath/hud/hud_renderer_interface.hpp b/include/omath/hud/hud_renderer_interface.hpp new file mode 100644 index 0000000..4891e1e --- /dev/null +++ b/include/omath/hud/hud_renderer_interface.hpp @@ -0,0 +1,27 @@ +// +// Created by orange on 13.03.2026. +// +#pragma once +#include "omath/linear_algebra/vector2.hpp" +#include "omath/utility/color.hpp" + +namespace omath::hud +{ + class HudRendererInterface + { + public: + virtual ~HudRendererInterface() = default; + virtual void add_line(const Vector2& line_start, const Vector2& line_end, const Color& color, + float thickness) = 0; + + virtual void add_polyline(const std::span>& vertexes, float thickness) = 0; + + virtual void add_filled_polyline(const std::span>& vertexes, float thickness) = 0; + + virtual void add_rectangle(const Vector2& min, const Vector2& max, const Color& color) = 0; + + virtual void add_filled_rectangle(const Vector2& min, const Vector2& max, const Color& color) = 0; + + virtual void add_text(const Vector2& position, const Color& color, const std::string_view& text) = 0; + }; +} // namespace omath::hud diff --git a/include/omath/hud/renderer_realizations/imgui_renderer.hpp b/include/omath/hud/renderer_realizations/imgui_renderer.hpp new file mode 100644 index 0000000..3e0d608 --- /dev/null +++ b/include/omath/hud/renderer_realizations/imgui_renderer.hpp @@ -0,0 +1,20 @@ +// +// Created by orange on 13.03.2026. +// +#pragma once +#include +namespace omath::hud +{ + class ImguiHudRenderer final : public HudRendererInterface + { + public: + ~ImguiHudRenderer() override; + void add_line(const Vector2& line_start, const Vector2& line_end, const Color& color, + float thickness) override; + void add_polyline(const std::span>& vertexes, float thickness) override; + void add_filled_polyline(const std::span>& vertexes, float thickness) override; + void add_rectangle(const Vector2& min, const Vector2& max, const Color& color) override; + void add_filled_rectangle(const Vector2& min, const Vector2& max, const Color& color) override; + void add_text(const Vector2& position, const Color& color, const std::string_view& text) override; + }; +} \ No newline at end of file diff --git a/source/hud/canvas_box.cpp b/source/hud/canvas_box.cpp new file mode 100644 index 0000000..ac3b38f --- /dev/null +++ b/source/hud/canvas_box.cpp @@ -0,0 +1,27 @@ +// +// Created by orange on 13.03.2026. +// +// +// Created by Vlad on 6/17/2025. +// +#include "omath/hud/canvas_box.hpp" + +namespace omath::hud +{ + + CanvasBox::CanvasBox(const Vector2 top, Vector2 bottom, const float ratio) + { + bottom.x = top.x; + const auto height = std::abs(top.y - bottom.y); + + top_left_corner = top - Vector2{height / ratio, 0}; + top_right_corner = top + Vector2{height / ratio, 0}; + + bottom_left_corner = bottom - Vector2{height / ratio, 0}; + bottom_right_corner = bottom + Vector2{height / ratio, 0}; + } + std::array, 4> CanvasBox::as_array() const + { + return {top_left_corner, top_right_corner, bottom_right_corner, bottom_left_corner}; + } +} // namespace ohud \ No newline at end of file diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp new file mode 100644 index 0000000..11f6c95 --- /dev/null +++ b/source/hud/entity_overlay.cpp @@ -0,0 +1,9 @@ +// +// Created by orange on 13.03.2026. +// +#include "omath/hud/entity_overlay.hpp" + +namespace omath::hud +{ + +} \ No newline at end of file diff --git a/source/hud/renderer_realizations/imgui_renderer.cpp b/source/hud/renderer_realizations/imgui_renderer.cpp new file mode 100644 index 0000000..525a014 --- /dev/null +++ b/source/hud/renderer_realizations/imgui_renderer.cpp @@ -0,0 +1,9 @@ +// +// Created by orange on 13.03.2026. +// +#include "omath/hud/renderer_realizations/imgui_renderer.hpp" + +namespace omath::hud +{ + +} \ No newline at end of file From 87966c82b9ec11348fd5fe63e8e4ae41ee1b04d9 Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:09:12 +0300 Subject: [PATCH 2/8] added realization --- include/omath/hud/entity_overlay.hpp | 14 +- include/omath/hud/hud_renderer_interface.hpp | 4 +- .../renderer_realizations/imgui_renderer.hpp | 4 +- source/hud/entity_overlay.cpp | 133 +++++++++++++++++- .../renderer_realizations/imgui_renderer.cpp | 46 +++++- 5 files changed, 185 insertions(+), 16 deletions(-) diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index fd120e1..c64e523 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -10,13 +10,7 @@ #include namespace omath::hud { - enum class SnapLineStartPosition - { - SCREEN_BOTTOM, - SCREEN_TOP, - SCREEN_CENTER, - }; - class EntityOverlay + class EntityOverlay final { public: EntityOverlay(const Vector2& top, const Vector2& bottom, @@ -59,14 +53,14 @@ namespace omath::hud void add_top_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height, float ratio, float offset = 5.f); - void add_snap_line(const SnapLineStartPosition& start_pos, const Color& color, float width); + void add_snap_line(const Vector3& start_pos, const Color& color, float width); private: - static void draw_outlined_text(const Vector2& position, const Color& color, + void draw_outlined_text(const Vector2& position, const Color& color, const std::string_view& text); CanvasBox m_canvas; Vector2 m_text_cursor_right; Vector2 m_text_cursor_top; - std::shared_ptr renderer; + std::shared_ptr m_renderer; }; } // namespace omath::hud \ No newline at end of file diff --git a/include/omath/hud/hud_renderer_interface.hpp b/include/omath/hud/hud_renderer_interface.hpp index 4891e1e..c563154 100644 --- a/include/omath/hud/hud_renderer_interface.hpp +++ b/include/omath/hud/hud_renderer_interface.hpp @@ -14,9 +14,9 @@ namespace omath::hud virtual void add_line(const Vector2& line_start, const Vector2& line_end, const Color& color, float thickness) = 0; - virtual void add_polyline(const std::span>& vertexes, float thickness) = 0; + virtual void add_polyline(const std::span>& vertexes, const Color& color, float thickness) = 0; - virtual void add_filled_polyline(const std::span>& vertexes, float thickness) = 0; + virtual void add_filled_polyline(const std::span>& vertexes, const Color& color, float thickness) = 0; virtual void add_rectangle(const Vector2& min, const Vector2& max, const Color& color) = 0; diff --git a/include/omath/hud/renderer_realizations/imgui_renderer.hpp b/include/omath/hud/renderer_realizations/imgui_renderer.hpp index 3e0d608..be7f637 100644 --- a/include/omath/hud/renderer_realizations/imgui_renderer.hpp +++ b/include/omath/hud/renderer_realizations/imgui_renderer.hpp @@ -11,8 +11,8 @@ namespace omath::hud ~ImguiHudRenderer() override; void add_line(const Vector2& line_start, const Vector2& line_end, const Color& color, float thickness) override; - void add_polyline(const std::span>& vertexes, float thickness) override; - void add_filled_polyline(const std::span>& vertexes, float thickness) override; + void add_polyline(const std::span>& vertexes, const Color& color, float thickness) override; + void add_filled_polyline(const std::span>& vertexes, const Color& color, float thickness) override; void add_rectangle(const Vector2& min, const Vector2& max, const Color& color) override; void add_filled_rectangle(const Vector2& min, const Vector2& max, const Color& color) override; void add_text(const Vector2& position, const Color& color, const std::string_view& text) override; diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index 11f6c95..d342364 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -5,5 +5,136 @@ namespace omath::hud { + void EntityOverlay::add_2d_box(const Color& box_color, const Color& fill_color, const float thickness) const + { + const auto points = m_canvas.as_array(); -} \ No newline at end of file + m_renderer->add_polyline({points.data(), points.size()}, box_color, thickness); + m_renderer->add_filled_polyline({points.data(), points.size()}, fill_color, thickness); + } + void EntityOverlay::add_cornered_2d_box(const Color& box_color, const Color& fill_color, + const float corner_ratio_len, const float thickness) const + { + const auto corner_line_length = + std::abs((m_canvas.top_left_corner - m_canvas.top_right_corner).x * corner_ratio_len); + + if (fill_color.value().w > 0.f) + add_2d_box(fill_color, fill_color); + // Left Side + m_renderer->add_line(m_canvas.top_left_corner, + m_canvas.top_left_corner + Vector2{corner_line_length, 0.f}, box_color, thickness); + + m_renderer->add_line(m_canvas.top_left_corner, + m_canvas.top_left_corner + Vector2{0.f, corner_line_length}, box_color, thickness); + + m_renderer->add_line(m_canvas.bottom_left_corner, + m_canvas.bottom_left_corner - Vector2{0.f, corner_line_length}, box_color, + thickness); + + m_renderer->add_line(m_canvas.bottom_left_corner, + m_canvas.bottom_left_corner + Vector2{corner_line_length, 0.f}, box_color, + thickness); + // Right Side + m_renderer->add_line(m_canvas.top_right_corner, + m_canvas.top_right_corner - Vector2{corner_line_length, 0.f}, box_color, thickness); + + m_renderer->add_line(m_canvas.top_right_corner, + m_canvas.top_right_corner + Vector2{0.f, corner_line_length}, box_color, thickness); + + m_renderer->add_line(m_canvas.bottom_right_corner, + m_canvas.bottom_right_corner - Vector2{0.f, corner_line_length}, box_color, + thickness); + + m_renderer->add_line(m_canvas.bottom_right_corner, + m_canvas.bottom_right_corner - Vector2{corner_line_length, 0.f}, box_color, + thickness); + } + void EntityOverlay::add_right_bar(const Color& color, const Color& outline_color, const Color& bg_color, + const float width, float ratio, const float offset) + { + ratio = std::clamp(ratio, 0.f, 1.f); + const auto max_bar_height = std::abs(m_canvas.top_right_corner.y - m_canvas.bottom_right_corner.y); + + const auto bar_start = m_canvas.bottom_right_corner + Vector2{offset, 0.f}; + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(width, -max_bar_height), bg_color); + + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(width, -max_bar_height * ratio), color); + m_renderer->add_rectangle(bar_start - Vector2(1.f, 0.f), + bar_start + Vector2(width, -max_bar_height), outline_color); + + // NOTE: Move text 1 pixel horizontali due to imgui logic of rendering rectangle + m_text_cursor_right.x += offset + width + 1.f; + } + void EntityOverlay::add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, + const float width, float ratio, const float offset) const + { + ratio = std::clamp(ratio, 0.f, 1.f); + const auto max_bar_height = std::abs(m_canvas.top_left_corner.y - m_canvas.bottom_right_corner.y); + + const auto bar_start = m_canvas.bottom_left_corner + Vector2{-(offset + width), 0.f}; + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(width, -max_bar_height), bg_color); + + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(width, -max_bar_height * ratio), color); + m_renderer->add_rectangle(bar_start - Vector2(1.f, 0.f), + bar_start + Vector2(width, -max_bar_height), outline_color); + } + void EntityOverlay::add_right_label(const Color& color, const float offset, const bool outlined, + const std::string_view& text) + { + if (outlined) + draw_outlined_text(m_text_cursor_right + Vector2{offset, 0.f}, color, text); + else + m_renderer->add_text(m_text_cursor_right + Vector2{offset, 0.f}, color, text.data()); + + m_text_cursor_right.y += ImGui::CalcTextSize(text.data()).y; + } + void EntityOverlay::add_top_label(const Color& color, const float offset, const bool outlined, + const std::string_view text) + { + + m_text_cursor_top.y -= ImGui::CalcTextSize(text.data()).y; + + if (outlined) + draw_outlined_text(m_text_cursor_top + Vector2{0.f, -offset}, color, text); + else + m_renderer->add_text(m_text_cursor_top + Vector2{0.f, -offset}, color, text.data()); + } + void EntityOverlay::add_top_bar(const Color& color, const Color& outline_color, const Color& bg_color, + const float height, float ratio, const float offset) + { + ratio = std::clamp(ratio, 0.f, 1.f); + const auto max_bar_width = std::abs(m_canvas.top_left_corner.x - m_canvas.bottom_right_corner.x); + + const auto bar_start = m_canvas.top_left_corner - Vector2{0.f, offset}; + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(max_bar_width, -height), bg_color); + + m_renderer->add_filled_rectangle(bar_start, bar_start + Vector2(max_bar_width * ratio, -height), color); + m_renderer->add_rectangle(bar_start, bar_start + Vector2(max_bar_width, -height), outline_color); + + m_text_cursor_top.y -= offset + height; + } + void EntityOverlay::add_snap_line(const Vector3& start_pos, const Color& color, const float width) + { + const Vector2 line_end = + m_canvas.bottom_left_corner + + Vector2{m_canvas.bottom_right_corner.x - m_canvas.bottom_left_corner.x, 0.f} / 2; + m_renderer->add_line(start_pos, line_end, color, width); + } + void EntityOverlay::draw_outlined_text(const Vector2& position, const Color& color, + const std::string_view& text) + { + static constexpr std::array outline_offsets = { + Vector2{-1, -1}, Vector2{-1, 0}, Vector2{-1, 1}, Vector2{0, -1}, + Vector2{0, 1}, Vector2{1, -1}, Vector2{1, 0}, Vector2{1, 1}}; + + for (const auto& outline_offset : outline_offsets) + m_renderer->add_text(position + outline_offset, Color{0.f, 0.f, 0.f, 1.f}, text.data()); + m_renderer->add_text(position, color, text.data()); + } + EntityOverlay::EntityOverlay(const Vector2& top, const Vector2& bottom, + const std::shared_ptr& renderer) + : m_canvas(top, bottom), m_text_cursor_right(m_canvas.top_right_corner), + m_text_cursor_top(m_canvas.top_left_corner), m_renderer(renderer) + { + } +} // namespace omath::hud \ No newline at end of file diff --git a/source/hud/renderer_realizations/imgui_renderer.cpp b/source/hud/renderer_realizations/imgui_renderer.cpp index 525a014..0f61f62 100644 --- a/source/hud/renderer_realizations/imgui_renderer.cpp +++ b/source/hud/renderer_realizations/imgui_renderer.cpp @@ -2,8 +2,52 @@ // Created by orange on 13.03.2026. // #include "omath/hud/renderer_realizations/imgui_renderer.hpp" +#include namespace omath::hud { + ImguiHudRenderer::~ImguiHudRenderer() = default; -} \ No newline at end of file + void ImguiHudRenderer::add_line(const Vector2& line_start, const Vector2& line_end, + const Color& color, const float thickness) + { + ImGui::GetBackgroundDrawList()->AddLine(line_start.to_im_vec2(), line_end.to_im_vec2(), + color.to_im_color(), thickness); + } + + void ImguiHudRenderer::add_polyline(const std::span>& vertexes, const Color& color, + const float thickness) + { + ImGui::GetBackgroundDrawList()->AddPolyline( + reinterpret_cast(vertexes.data()), + static_cast(vertexes.size()), + color.to_im_color(), ImDrawFlags_None, thickness); + } + + void ImguiHudRenderer::add_filled_polyline(const std::span>& vertexes, const Color& color, + const float thickness) + { + ImGui::GetBackgroundDrawList()->AddPolyline( + reinterpret_cast(vertexes.data()), + static_cast(vertexes.size()), + color.to_im_color(), ImDrawFlags_Closed, thickness); + } + + void ImguiHudRenderer::add_rectangle(const Vector2& min, const Vector2& max, const Color& color) + { + ImGui::GetBackgroundDrawList()->AddRect(min.to_im_vec2(), max.to_im_vec2(), color.to_im_color()); + } + + void ImguiHudRenderer::add_filled_rectangle(const Vector2& min, const Vector2& max, + const Color& color) + { + ImGui::GetBackgroundDrawList()->AddRectFilled(min.to_im_vec2(), max.to_im_vec2(), color.to_im_color()); + } + + void ImguiHudRenderer::add_text(const Vector2& position, const Color& color, + const std::string_view& text) + { + ImGui::GetBackgroundDrawList()->AddText(position.to_im_vec2(), color.to_im_color(), text.data(), + text.data() + text.size()); + } +} // namespace omath::hud From f188257e0ff21c4b66cc3d45120821f8c8fe2b87 Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:28:16 +0300 Subject: [PATCH 3/8] added stuff --- examples/CMakeLists.txt | 1 + examples/example_hud/CMakeLists.txt | 15 ++++ examples/example_hud/gui/main_window.cpp | 110 +++++++++++++++++++++++ examples/example_hud/gui/main_window.hpp | 28 ++++++ examples/example_hud/main.cpp | 8 ++ include/omath/hud/entity_overlay.hpp | 2 +- source/hud/entity_overlay.cpp | 2 +- vcpkg.json | 6 +- 8 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 examples/example_hud/CMakeLists.txt create mode 100644 examples/example_hud/gui/main_window.cpp create mode 100644 examples/example_hud/gui/main_window.hpp create mode 100644 examples/example_hud/main.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 52dff0e..026b487 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(example_barycentric) add_subdirectory(example_glfw3) add_subdirectory(example_proj_mat_builder) add_subdirectory(example_signature_scan) +add_subdirectory(example_hud) if(OMATH_ENABLE_VALGRIND) omath_setup_valgrind(example_projection_matrix_builder) diff --git a/examples/example_hud/CMakeLists.txt b/examples/example_hud/CMakeLists.txt new file mode 100644 index 0000000..9ec29aa --- /dev/null +++ b/examples/example_hud/CMakeLists.txt @@ -0,0 +1,15 @@ +project(example_hud) + + +add_executable(${PROJECT_NAME} main.cpp gui/main_window.cpp gui/main_window.hpp) + +set_target_properties(${PROJECT_NAME} PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" + CXX_STANDARD 23 + CXX_STANDARD_REQUIRED ON) + +find_package(OpenGL) +find_package(GLEW REQUIRED) +find_package(glfw3 CONFIG REQUIRED) +target_link_libraries(${PROJECT_NAME} PRIVATE glfw imgui::imgui omath::omath OpenGL::GL) \ No newline at end of file diff --git a/examples/example_hud/gui/main_window.cpp b/examples/example_hud/gui/main_window.cpp new file mode 100644 index 0000000..ff6c8a5 --- /dev/null +++ b/examples/example_hud/gui/main_window.cpp @@ -0,0 +1,110 @@ +// +// Created by Orange on 11/11/2024. +// + +#include "main_window.hpp" +#include "omath/hud/renderer_realizations/imgui_renderer.hpp" +#include +#include +#include +#include +#include +#include + +namespace imgui_desktop::gui +{ + bool MainWindow::m_canMoveWindow = false; + + MainWindow::MainWindow(const std::string_view& caption, int width, int height) + { + if (!glfwInit()) + std::exit(EXIT_FAILURE); + + glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, true); + m_window = glfwCreateWindow(width, height, caption.data(), nullptr, nullptr); + + glfwMakeContextCurrent(m_window); + + ImGui::CreateContext(); + ImGui::StyleColorsDark(); + + ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = {0.f, 0.f, 0.f, 0.f}; + + ImGui::GetStyle().AntiAliasedLines = false; + ImGui::GetStyle().AntiAliasedFill = false; + + ImGui_ImplGlfw_InitForOpenGL(m_window, true); + ImGui_ImplOpenGL3_Init("#version 130"); + } + + void MainWindow::Run() + { + omath::Color box_color = {0.f, 0.f, 0.f, 1.f}; + omath::Color box_fill = {0.f, 0.f, 0.f, 0.f}; + omath::Color bar_color = {0.f, 1.f, 0.f, 1.f}; + omath::Color bar_bg_color = {0.f, 0.f, 0.f, 0.0f}; + omath::Color bar_outline_color = {0.f, 0.f, 0.f, 1.f}; + float bar_width = 3.f; + float bar_value = 1.f; + + while (!glfwWindowShouldClose(m_window) && m_opened) + { + glfwPollEvents(); + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + ImGui::GetBackgroundDrawList()->AddRectFilled({}, ImGui::GetMainViewport()->Size, ImColor(40, 40, 40, 200)); + + ImGui::Begin("OHUD Showcase", &m_opened, + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoDecoration); + { + ImGui::SetWindowPos({}); + ImGui::SetWindowSize(ImGui::GetMainViewport()->Size); + + ImGui::ColorEdit4("Box", reinterpret_cast(&box_color), ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("Box fill", reinterpret_cast(&box_fill), ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("Bar", reinterpret_cast(&bar_color), ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("Bar Background", reinterpret_cast(&bar_bg_color), + ImGuiColorEditFlags_NoInputs); + ImGui::ColorEdit4("Bar Outline", reinterpret_cast(&bar_outline_color), + ImGuiColorEditFlags_NoInputs); + + ImGui::PushItemWidth(100.f); + ImGui::SliderFloat("Bar Width", &bar_width, 1.f, 20.f); + ImGui::SliderFloat("Bar Value", &bar_value, 0.f, 1.f); + ImGui::PopItemWidth(); + ImGui::End(); + } + + omath::hud::EntityOverlay ent({400.f, 100.f}, {400.f, 400.f}, std::make_shared()); + + ent.add_2d_box(box_color, box_fill, 1.f); + ent.add_cornered_2d_box(omath::Color::from_rgba(255, 0, 255, 255), box_fill); + ent.add_right_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value); + ent.add_left_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value); + ent.add_top_bar(bar_color, bar_outline_color, bar_bg_color, bar_width, bar_value); + ent.add_right_label({0.f, 1.f, 0.f, 1.f}, 3, true, "Health: {}/100", 100); + ent.add_right_label({1.f, 0.f, 0.f, 1.f}, 3, true, "Shield: {}/125", 125); + ent.add_right_label({1.f, 0.f, 1.f, 1.f}, 3, true, "*LOCKED*"); + + ent.add_top_label(omath::Color::from_rgba(255, 255, 0, 255), 3, true, "*SCOPED*"); + ent.add_top_label(omath::Color::from_rgba(255, 0, 0, 255), 3, true, "*BLEEDING*"); + ent.add_snap_line(omath::Vector2{400, 600}, omath::Color::from_rgba(255, 0, 0, 255), 2.f); + ImGui::Render(); + + int display_w, display_h; + + glfwGetFramebufferSize(m_window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + + glClearColor(0.f, 0.f, 0.f, 0.f); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + glfwSwapBuffers(m_window); + } + glfwDestroyWindow(m_window); + } +} // namespace imgui_desktop::gui +// imgui_desktop \ No newline at end of file diff --git a/examples/example_hud/gui/main_window.hpp b/examples/example_hud/gui/main_window.hpp new file mode 100644 index 0000000..f8eb072 --- /dev/null +++ b/examples/example_hud/gui/main_window.hpp @@ -0,0 +1,28 @@ +// +// Created by Vlad on 6/17/2025. +// + +// +// Created by Orange on 11/11/2024. +// +#pragma once +#include + +struct GLFWwindow; + +namespace imgui_desktop::gui +{ + class MainWindow + { + public: + MainWindow(const std::string_view &caption, int width, int height); + + void Run(); + + private: + GLFWwindow* m_window; + static bool m_canMoveWindow; + bool m_opened = true; + }; +} // gui +// imgui_desktop \ No newline at end of file diff --git a/examples/example_hud/main.cpp b/examples/example_hud/main.cpp new file mode 100644 index 0000000..2ea3730 --- /dev/null +++ b/examples/example_hud/main.cpp @@ -0,0 +1,8 @@ +// +// Created by orange on 13.03.2026. +// +#include "gui/main_window.hpp" +int main() +{ + imgui_desktop::gui::MainWindow("omath::hud", 800, 600).Run(); +} \ No newline at end of file diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index c64e523..026d2b5 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -53,7 +53,7 @@ namespace omath::hud void add_top_bar(const Color& color, const Color& outline_color, const Color& bg_color, float height, float ratio, float offset = 5.f); - void add_snap_line(const Vector3& start_pos, const Color& color, float width); + void add_snap_line(const Vector2& start_pos, const Color& color, float width); private: void draw_outlined_text(const Vector2& position, const Color& color, diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index d342364..143825f 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -113,7 +113,7 @@ namespace omath::hud m_text_cursor_top.y -= offset + height; } - void EntityOverlay::add_snap_line(const Vector3& start_pos, const Color& color, const float width) + void EntityOverlay::add_snap_line(const Vector2& start_pos, const Color& color, const float width) { const Vector2 line_end = m_canvas.bottom_left_corner diff --git a/vcpkg.json b/vcpkg.json index 52c1856..5ad46d9 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -31,7 +31,11 @@ "dependencies": [ "glfw3", "glew", - "opengl" + "opengl", + { + "name": "imgui", + "features": ["glfw-binding", "opengl3-binding"] + } ] }, "imgui": { From bb1b5ad14aaf91d55e7e9de9bf68b1084bbf75f9 Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:32:44 +0300 Subject: [PATCH 4/8] removed shit --- source/hud/entity_overlay.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index 143825f..908c1ed 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -62,8 +62,7 @@ namespace omath::hud m_renderer->add_rectangle(bar_start - Vector2(1.f, 0.f), bar_start + Vector2(width, -max_bar_height), outline_color); - // NOTE: Move text 1 pixel horizontali due to imgui logic of rendering rectangle - m_text_cursor_right.x += offset + width + 1.f; + m_text_cursor_right.x += offset + width; } void EntityOverlay::add_left_bar(const Color& color, const Color& outline_color, const Color& bg_color, const float width, float ratio, const float offset) const From d6a2165f832831fc1d5359d0364f1342367cbc87 Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:37:03 +0300 Subject: [PATCH 5/8] fix --- include/omath/hud/entity_overlay.hpp | 1 + source/hud/entity_overlay.cpp | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index 026d2b5..a88bc4f 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -8,6 +8,7 @@ #include "omath/utility/color.hpp" #include #include +#include namespace omath::hud { class EntityOverlay final diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index 908c1ed..11c8875 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -90,7 +90,6 @@ namespace omath::hud void EntityOverlay::add_top_label(const Color& color, const float offset, const bool outlined, const std::string_view text) { - m_text_cursor_top.y -= ImGui::CalcTextSize(text.data()).y; if (outlined) From 371d8154ee1b93c8d06ff49b435bc663f651778d Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:40:30 +0300 Subject: [PATCH 6/8] fix --- include/omath/hud/entity_overlay.hpp | 1 - include/omath/hud/hud_renderer_interface.hpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/include/omath/hud/entity_overlay.hpp b/include/omath/hud/entity_overlay.hpp index a88bc4f..026d2b5 100644 --- a/include/omath/hud/entity_overlay.hpp +++ b/include/omath/hud/entity_overlay.hpp @@ -8,7 +8,6 @@ #include "omath/utility/color.hpp" #include #include -#include namespace omath::hud { class EntityOverlay final diff --git a/include/omath/hud/hud_renderer_interface.hpp b/include/omath/hud/hud_renderer_interface.hpp index c563154..c131ba6 100644 --- a/include/omath/hud/hud_renderer_interface.hpp +++ b/include/omath/hud/hud_renderer_interface.hpp @@ -4,6 +4,7 @@ #pragma once #include "omath/linear_algebra/vector2.hpp" #include "omath/utility/color.hpp" +#include namespace omath::hud { From 6a2b4b90b4916f84d8e236b8b6ad135b285425e3 Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:49:56 +0300 Subject: [PATCH 7/8] fix --- include/omath/hud/canvas_box.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/omath/hud/canvas_box.hpp b/include/omath/hud/canvas_box.hpp index 021a775..a794249 100644 --- a/include/omath/hud/canvas_box.hpp +++ b/include/omath/hud/canvas_box.hpp @@ -3,7 +3,7 @@ // #pragma once #include "omath/linear_algebra/vector2.hpp" - +#include namespace omath::hud { class CanvasBox final From 6fb420642b28f29fad2ac74676e6645db876f82c Mon Sep 17 00:00:00 2001 From: orange Date: Fri, 13 Mar 2026 21:58:14 +0300 Subject: [PATCH 8/8] updated props --- examples/example_hud/CMakeLists.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/example_hud/CMakeLists.txt b/examples/example_hud/CMakeLists.txt index 9ec29aa..7902e2e 100644 --- a/examples/example_hud/CMakeLists.txt +++ b/examples/example_hud/CMakeLists.txt @@ -3,11 +3,12 @@ project(example_hud) add_executable(${PROJECT_NAME} main.cpp gui/main_window.cpp gui/main_window.hpp) -set_target_properties(${PROJECT_NAME} PROPERTIES +set_target_properties( + ${PROJECT_NAME} + PROPERTIES CXX_STANDARD 23 ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}" - CXX_STANDARD 23 - CXX_STANDARD_REQUIRED ON) + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}") find_package(OpenGL) find_package(GLEW REQUIRED)