From 8125c18ae63603ea298266d134d7cf80010efb3c Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sun, 8 Oct 2023 22:29:47 -0700 Subject: [PATCH] run imgui demo --- CMakeLists.txt | 10 +- Makefile | 7 ++ ext/imgui/misc/freetype/imgui_freetype.cpp | 2 +- fonts/b612_mono.h | 18 ++-- fonts/brassmono.h | 18 ++-- fonts/gen_font_includes.py | 5 +- kge.cc | 104 +++++++++++++++++++++ 7 files changed, 142 insertions(+), 22 deletions(-) create mode 100644 Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index aed7b3c..10be09b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ project(kge) find_package(SDL2 REQUIRED) find_package(OpenGL REQUIRED) +find_package(Freetype REQUIRED) add_compile_options("-Wall" "-Wextra" "-Werror") add_definitions(-DIMGUI_ENABLE_FREETYPE) @@ -20,17 +21,24 @@ add_library(imgui STATIC ext/imgui/backends/imgui_impl_sdl2.cpp ext/imgui/backends/imgui_impl_sdl2.h ext/imgui/backends/imgui_impl_opengl3.cpp - ext/imgui/backends/imgui_impl_opengl3.h) + ext/imgui/backends/imgui_impl_opengl3.h + + ext/imgui/misc/freetype/imgui_freetype.cpp + ext/imgui/misc/freetype/imgui_freetype.h +) add_library(imgui::imgui ALIAS imgui) target_link_libraries(imgui PUBLIC OpenGL::GL $ $,SDL2::SDL2,SDL2::SDL2-static> + Freetype::Freetype ) target_include_directories(imgui PUBLIC ext/imgui/ ext/imgui/backends/ + ext/imgui/misc/freetype + ${FREETYPE_INCLUDE_DIRS} ) include_directories(ext/) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f210bc8 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: all +all: + +.PHONY: deps +deps: + sudo apt-get install libfreetype-dev libsdl2-dev libopengl-dev + diff --git a/ext/imgui/misc/freetype/imgui_freetype.cpp b/ext/imgui/misc/freetype/imgui_freetype.cpp index 2e855de..fe219b4 100644 --- a/ext/imgui/misc/freetype/imgui_freetype.cpp +++ b/ext/imgui/misc/freetype/imgui_freetype.cpp @@ -38,7 +38,7 @@ #include "imgui_freetype.h" #include "imgui_internal.h" // ImMin,ImMax,ImFontAtlasBuild*, #include -#include +#include #include FT_FREETYPE_H // #include FT_MODULE_H // #include FT_GLYPH_H // diff --git a/fonts/b612_mono.h b/fonts/b612_mono.h index 6ed5700..7c192c4 100644 --- a/fonts/b612_mono.h +++ b/fonts/b612_mono.h @@ -1,12 +1,12 @@ #ifndef KGE_FONTS_B612_MONO_H -#defineKGE_FONTS_B612_MONO_H +#define KGE_FONTS_B612_MONO_H // File: 'B612_Mono/B612Mono-Regular.ttf' (136712 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int B612Mono_Regular.ttf_compressed_size = 72615; -static const unsigned int B612Mono_Regular.ttf_compressed_data[72616/4] = +static const unsigned int B612Mono_Regular_compressed_size = 72615; +static const unsigned int B612Mono_Regular_compressed_data[72616/4] = { 0x0000bc57, 0x00000000, 0x08160200, 0x00000400, 0x00010025, 0x82100000, 0x00042e04, 0x2f534f00, 0xb5872c32, 0x0100005c, 0x3b1582c4, 0x4d445660, 0x712c6a58, 0x11000098, 0x05000024, 0x616d63e0, 0x736a3e70, 0x03000069, 0x04291382, 0x74766340, 0x0c5d0420, 0x202f8240, 0x2b2f8260, 0x6770662a, @@ -1527,8 +1527,8 @@ static const unsigned int B612Mono_Regular.ttf_compressed_data[72616/4] = // File: 'B612_Mono/B612Mono-Bold.ttf' (135904 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int B612Mono_Bold.ttf_compressed_size = 74748; -static const unsigned int B612Mono_Bold.ttf_compressed_data[74748/4] = +static const unsigned int B612Mono_Bold_compressed_size = 74748; +static const unsigned int B612Mono_Bold_compressed_data[74748/4] = { 0x0000bc57, 0x00000000, 0xe0120200, 0x00000400, 0x00010025, 0x82100000, 0x00042e04, 0x2f534f00, 0xb5b32d32, 0x01000041, 0x3b1582c4, 0x4d445660, 0x712c6a58, 0x11000098, 0x0500000c, 0x616d63e0, 0x736a3e70, 0x03000069, 0x04291382, 0x74766340, 0x0c5d0420, 0x202f8240, 0x2c2f8260, 0x6770662a, @@ -3094,8 +3094,8 @@ static const unsigned int B612Mono_Bold.ttf_compressed_data[74748/4] = // File: 'B612_Mono/B612Mono-BoldItalic.ttf' (121732 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int B612Mono_BoldItalic.ttf_compressed_size = 69211; -static const unsigned int B612Mono_BoldItalic.ttf_compressed_data[69212/4] = +static const unsigned int B612Mono_BoldItalic_compressed_size = 69211; +static const unsigned int B612Mono_BoldItalic_compressed_data[69212/4] = { 0x0000bc57, 0x00000000, 0x84db0100, 0x00000400, 0x00010025, 0x82100000, 0x00042e04, 0x2f534f00, 0xb5b32d32, 0x01000042, 0x3b1582c4, 0x4d445660, 0x712c6a58, 0x11000098, 0x05000044, 0x616d63e0, 0x736a3e70, 0x03000069, 0x04291382, 0x74766340, 0x0c5d0420, 0x202f8240, 0x2c2f8260, 0x6770662a, @@ -4545,8 +4545,8 @@ static const unsigned int B612Mono_BoldItalic.ttf_compressed_data[69212/4] = // File: 'B612_Mono/B612Mono-Italic.ttf' (118888 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int B612Mono_Italic.ttf_compressed_size = 67763; -static const unsigned int B612Mono_Italic.ttf_compressed_data[67764/4] = +static const unsigned int B612Mono_Italic_compressed_size = 67763; +static const unsigned int B612Mono_Italic_compressed_data[67764/4] = { 0x0000bc57, 0x00000000, 0x68d00100, 0x00000400, 0x00010025, 0x82100000, 0x00042e04, 0x2f534f00, 0xb5872c32, 0x0100002a, 0x3b1582c4, 0x4d445660, 0x712c6a58, 0x11000098, 0x0500001c, 0x616d63e0, 0x736a3e70, 0x03000069, 0x04291382, 0x74766340, 0x0c5d0420, 0x202f8240, 0x2c2f8260, 0x6770662a, diff --git a/fonts/brassmono.h b/fonts/brassmono.h index 284c487..048ddd7 100644 --- a/fonts/brassmono.h +++ b/fonts/brassmono.h @@ -1,12 +1,12 @@ #ifndef KGE_FONTS_BRASSMONO_H -#defineKGE_FONTS_BRASSMONO_H +#define KGE_FONTS_BRASSMONO_H // File: 'BrassMono/BrassMono-Italic.ttf' (38092 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int BrassMono_Italic.ttf_compressed_size = 29682; -static const unsigned int BrassMono_Italic.ttf_compressed_data[29684/4] = +static const unsigned int BrassMono_Italic_compressed_size = 29682; +static const unsigned int BrassMono_Italic_compressed_data[29684/4] = { 0x0000bc57, 0x00000000, 0xcc940000, 0x00000400, 0x00010025, 0x82120000, 0x00042604, 0x49534420, 0x820d8247, 0xc494210c, 0x082c0782, 0x46454447, 0x9b002800, 0x2c010000, 0x1e280f82, 0x322f534f, 0x6bfcb98b, 0x4c200f82, 0x56280f82, 0x70616d63, 0xcbe7cd60, 0xa4300f82, 0x7a020000, 0x20747663, @@ -633,8 +633,8 @@ static const unsigned int BrassMono_Italic.ttf_compressed_data[29684/4] = // File: 'BrassMono/BrassMono-Bold.ttf' (38512 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int BrassMono_Bold.ttf_compressed_size = 28400; -static const unsigned int BrassMono_Bold.ttf_compressed_data[28400/4] = +static const unsigned int BrassMono_Bold_compressed_size = 28400; +static const unsigned int BrassMono_Bold_compressed_data[28400/4] = { 0x0000bc57, 0x00000000, 0x70960000, 0x00000400, 0x00010025, 0x82120000, 0x00042604, 0x49534420, 0x820d8247, 0x6896210c, 0x082c0782, 0x46454447, 0x9b002800, 0x2c010000, 0x1e280f82, 0x322f534f, 0xd6fce48c, 0x4c200f82, 0x56280f82, 0x70616d63, 0xcbe7cd60, 0xa4300f82, 0x7a020000, 0x20747663, @@ -1234,8 +1234,8 @@ static const unsigned int BrassMono_Bold.ttf_compressed_data[28400/4] = // File: 'BrassMono/BrassMono-BoldItalic.ttf' (41424 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int BrassMono_BoldItalic.ttf_compressed_size = 30331; -static const unsigned int BrassMono_BoldItalic.ttf_compressed_data[30332/4] = +static const unsigned int BrassMono_BoldItalic_compressed_size = 30331; +static const unsigned int BrassMono_BoldItalic_compressed_data[30332/4] = { 0x0000bc57, 0x00000000, 0xd0a10000, 0x00000400, 0x00010025, 0x82120000, 0x00042604, 0x49534420, 0x820d8247, 0xc8a1210c, 0x082c0782, 0x46454447, 0x9b002800, 0x2c010000, 0x1e280f82, 0x322f534f, 0x8bfce58c, 0x4c200f82, 0x56280f82, 0x70616d63, 0xcbe7cd60, 0xa4300f82, 0x7a020000, 0x20747663, @@ -1875,8 +1875,8 @@ static const unsigned int BrassMono_BoldItalic.ttf_compressed_data[30332/4] = // File: 'BrassMono/BrassMono-Regular.ttf' (36504 bytes) // Exported using binary_to_compressed_c.cpp -static const unsigned int BrassMono_Regular.ttf_compressed_size = 28091; -static const unsigned int BrassMono_Regular.ttf_compressed_data[28092/4] = +static const unsigned int BrassMono_Regular_compressed_size = 28091; +static const unsigned int BrassMono_Regular_compressed_data[28092/4] = { 0x0000bc57, 0x00000000, 0x988e0000, 0x00000400, 0x00010025, 0x82120000, 0x00042604, 0x49534420, 0x820d8247, 0x908e210c, 0x082c0782, 0x46454447, 0x9b002800, 0x2c010000, 0x1e280f82, 0x322f534f, 0xf6fcb88b, 0x4c200f82, 0x56280f82, 0x70616d63, 0xcbe7cd60, 0xa4300f82, 0x7a020000, 0x20747663, diff --git a/fonts/gen_font_includes.py b/fonts/gen_font_includes.py index 3bda471..bb86f03 100755 --- a/fonts/gen_font_includes.py +++ b/fonts/gen_font_includes.py @@ -8,7 +8,8 @@ import subprocess DEFAULT_FONTS = ['B612_Mono', 'BrassMono'] def generate_font(header_file, path): - symbol_name=os.path.basename(path).replace('-', '_') + symbol_name=os.path.splitext(os.path.basename(path))[0] + symbol_name=symbol_name.replace('-', '_') output = subprocess.check_output( f'binary_to_compressed_c "{path}" {symbol_name}', shell=True) @@ -24,7 +25,7 @@ def generate_header(header, guard, files): raise with open(header, 'wt') as header_file: header_file.write(f"""#ifndef {guard} -#define{guard} +#define {guard} """) for file in files: diff --git a/kge.cc b/kge.cc index 05e4438..cbc37bb 100644 --- a/kge.cc +++ b/kge.cc @@ -11,6 +11,9 @@ #include #endif +#include "fonts/b612_mono.h" +#include "fonts/brassmono.h" + int main() @@ -61,6 +64,107 @@ main() SDL_GL_MakeCurrent(window, gl_context); SDL_GL_SetSwapInterval(1); // Enable vsync + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + //ImGui::StyleColorsLight(); + + // Setup Platform/Renderer backends + ImGui_ImplSDL2_InitForOpenGL(window, gl_context); + ImGui_ImplOpenGL3_Init(glsl_version); + + io.Fonts->AddFontFromMemoryCompressedTTF(B612Mono_Regular_compressed_data, B612Mono_Regular_compressed_size, 12); + + bool done = false; + bool show_demo_window = true; + bool show_another_window = false; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + + while (!done) { + // Poll and handle events (inputs, window resize, etc.) + // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. + // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. + // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. + // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ImplSDL2_ProcessEvent(&event); + if (event.type == SDL_QUIT) + done = true; + if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) + done = true; + } + + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplSDL2_NewFrame(); + ImGui::NewFrame(); + + 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 create 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 / io.Framerate, io.Framerate); + ImGui::End(); + } + + // 3. Show another simple window. + if (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::Begin("Another Window", &show_another_window); + + ImGui::Text("Hello from another window!"); + if (ImGui::Button("Close Me")) + show_another_window = false; + ImGui::End(); + } + + // Rendering + ImGui::Render(); + glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); + 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_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + SDL_GL_SwapWindow(window); + } + + // Cleanup + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplSDL2_Shutdown(); + ImGui::DestroyContext(); + + SDL_GL_DeleteContext(gl_context); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; }