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": {