From b5ff92c2cffcbd98f7a58adc5364025692ede7ca Mon Sep 17 00:00:00 2001 From: iwatake2222 Date: Thu, 6 Jan 2022 17:37:29 +0900 Subject: [PATCH 1/2] Add imgui as git submodule. issue #8 --- .gitmodules | 3 +++ NOTICE.md | 31 +++++++++++++++++++++++++++++++ desktop/third_party/imgui | 1 + 3 files changed, 35 insertions(+) create mode 160000 desktop/third_party/imgui diff --git a/.gitmodules b/.gitmodules index 0a92fdf..1ed78ca 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "desktop/test/third_party/googletest"] path = desktop/test/third_party/googletest url = https://github.com/google/googletest +[submodule "desktop/third_party/imgui"] + path = desktop/third_party/imgui + url = https://github.com/ocornut/imgui diff --git a/NOTICE.md b/NOTICE.md index 752eeff..2e38e22 100644 --- a/NOTICE.md +++ b/NOTICE.md @@ -1,3 +1,34 @@ +## Dear ImGui +- https://github.com/ocornut/imgui +- Copyright (c) 2014-2022 Omar Cornut +- Licensed under the MIT License (MIT) +- Modification: No +- This project uses ImGui and contains source code of ImGui as git submodule + +``` +The MIT License (MIT) + +Copyright (c) 2014-2022 Omar Cornut + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` + ## GLFW - https://github.com/glfw/glfw - Copyright (c) 2002-2006 Marcus Geelnard diff --git a/desktop/third_party/imgui b/desktop/third_party/imgui new file mode 160000 index 0000000..512c54b --- /dev/null +++ b/desktop/third_party/imgui @@ -0,0 +1 @@ +Subproject commit 512c54bbc062c41c74f8a8bd8ff1fd6bebd1e6d0 From 6ec00c54a75b532efd8fe145e1259f3e4052137c Mon Sep 17 00:00:00 2001 From: iwatake2222 Date: Thu, 6 Jan 2022 17:48:24 +0900 Subject: [PATCH 2/2] Modify cmake settings to use ImGui and add sample code. issue #8 --- desktop/CMakeLists.txt | 13 +++++++ desktop/main.cpp | 80 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/desktop/CMakeLists.txt b/desktop/CMakeLists.txt index 6ea224e..1f662e7 100644 --- a/desktop/CMakeLists.txt +++ b/desktop/CMakeLists.txt @@ -33,6 +33,19 @@ add_subdirectory("${GLEW_DIR}/build/cmake/" glew) target_link_libraries(${ProjectName} glew_s) target_include_directories(${ProjectName} PUBLIC "${GLEW_DIR}/include/") +# For ImGui (reference: example_glfw_opengl3/Makefile) +set(IMGUI_DIR "${CMAKE_CURRENT_LIST_DIR}/third_party/imgui/") +target_sources(${ProjectName} PRIVATE + ${IMGUI_DIR}/imgui.cpp + ${IMGUI_DIR}/imgui_demo.cpp + ${IMGUI_DIR}/imgui_draw.cpp + ${IMGUI_DIR}/imgui_tables.cpp + ${IMGUI_DIR}/imgui_widgets.cpp + ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp + ${IMGUI_DIR}/backends/imgui_impl_opengl3.cpp +) +target_include_directories(${ProjectName} PUBLIC ${IMGUI_DIR} ${IMGUI_DIR}/backends) + # Add test module option(BUILD_TESTS "BUILD_TESTS" ON) if (BUILD_TESTS) diff --git a/desktop/main.cpp b/desktop/main.cpp index b1f4022..64daeb6 100644 --- a/desktop/main.cpp +++ b/desktop/main.cpp @@ -22,6 +22,11 @@ limitations under the License. #include /* this must be before including glfw*/ #include +/* for ImGui */ +#include "imgui.h" +#include "imgui_impl_glfw.h" +#include "imgui_impl_opengl3.h" + /*** Macro ***/ /* macro functions */ #define RUN_CHECK(x) \ @@ -55,17 +60,84 @@ int main(int argc, char *argv[]) /* Ensure not to miss input */ glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); - /* Dark blue background */ - glClearColor(0.0f, 0.0f, 0.4f, 0.0f); - + /* Initialize ImGui */ + /* imgui: Setup Dear ImGui context */ + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + /* imgui: Setup Dear ImGui style */ + ImGui::StyleColorsDark(); + //ImGui::StyleColorsClassic(); + + /* imgui: Setup Platform/Renderer backends */ + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init("#version 130"); + + /* imgui: state */ + bool show_demo_window = true; + bool show_another_window = false; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + + /*** Start loop ***/ while(1) { + glfwPollEvents(); + + /* imgui: Start the Dear ImGui frame */ + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + + // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). + if (show_demo_window) + ImGui::ShowDemoWindow(&show_demo_window); + + // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. + { + static float f = 0.0f; + static int counter = 0; + + ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it. + + ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too) + ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state + ImGui::Checkbox("Another Window", &show_another_window); + + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f + ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color + + if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated) + counter++; + ImGui::SameLine(); + ImGui::Text("counter = %d", counter); + + ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); + ImGui::End(); + } + + // 3. Show another simple window. + if (show_another_window) + { + ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) + ImGui::Text("Hello from another window!"); + if (ImGui::Button("Close Me")) + show_another_window = false; + ImGui::End(); + } + /* Clear the screen */ + glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); glClear(GL_COLOR_BUFFER_BIT); + /* imgui: Rendering */ + ImGui::Render(); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + /* Swap buffers */ glfwSwapBuffers(window); - glfwPollEvents(); /* Check if the ESC key was pressed or the window was closed */ if (glfwWindowShouldClose(window) != 0 || glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {