Skip to content

Commit

Permalink
Modify cmake settings to use ImGui and add sample code. issue #8
Browse files Browse the repository at this point in the history
  • Loading branch information
iwatake2222 committed Jan 6, 2022
1 parent b5ff92c commit 6ec00c5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
13 changes: 13 additions & 0 deletions desktop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
80 changes: 76 additions & 4 deletions desktop/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ limitations under the License.
#include <GL/glew.h> /* this must be before including glfw*/
#include <GLFW/glfw3.h>

/* for ImGui */
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"

/*** Macro ***/
/* macro functions */
#define RUN_CHECK(x) \
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6ec00c5

Please sign in to comment.