From 07765b4ad7a4d37a15cb0cb1109f403d3edc75ba Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 26 Jul 2023 01:11:17 +0200 Subject: [PATCH] wire up imgui --- external/CMakeLists.txt | 1 + external/imgui/CMakeLists.txt | 48 ++++++++++++++++++++++++++ src/CMakeLists.txt | 5 +-- src/main.cpp | 41 ++++++++++++++++------ src/theme.hpp | 64 +++++++++++++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 13 deletions(-) create mode 100644 external/imgui/CMakeLists.txt create mode 100644 src/theme.hpp diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index d4af481..84ffd02 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -13,4 +13,5 @@ add_subdirectory(./solanaceae_toxcore) add_subdirectory(./solanaceae_tox) add_subdirectory(./sdl) +add_subdirectory(./imgui) diff --git a/external/imgui/CMakeLists.txt b/external/imgui/CMakeLists.txt new file mode 100644 index 0000000..7b9217a --- /dev/null +++ b/external/imgui/CMakeLists.txt @@ -0,0 +1,48 @@ +cmake_minimum_required(VERSION 3.9 FATAL_ERROR) + +project(imgui C CXX) + +add_library(imgui + imgui/imgui.h + imgui/imgui.cpp + imgui/imgui_draw.cpp + imgui/imgui_widgets.cpp + imgui/imgui_tables.cpp + imgui/imgui_demo.cpp + + imgui/imstb_rectpack.h + imgui/imstb_textedit.h + imgui/imstb_truetype.h + + imgui/misc/cpp/imgui_stdlib.h + imgui/misc/cpp/imgui_stdlib.cpp +) + +target_compile_features(imgui PUBLIC cxx_std_11) + +target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}") +target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/imgui") # im sad + +#add_library(imgui_backend_opengl3 + #"imgui/backends/imgui_impl_opengl3.h" + #"imgui/backends/imgui_impl_opengl3.cpp" +#) +#target_link_libraries(imgui_backend_opengl3 PUBLIC imgui) + +add_library(imgui_backend_sdlrenderer3 + imgui/backends/imgui_impl_sdlrenderer3.h + imgui/backends/imgui_impl_sdlrenderer3.cpp +) +target_link_libraries(imgui_backend_sdlrenderer3 PUBLIC imgui SDL3::SDL3) + +#add_library(imgui_backend_glfw + #"imgui/backends/imgui_impl_glfw.h" + #"imgui/backends/imgui_impl_glfw.cpp" +#) +#target_link_libraries(imgui_backend_glfw PUBLIC imgui glfw) + +add_library(imgui_backend_sdl3 + imgui/backends/imgui_impl_sdl3.h + imgui/backends/imgui_impl_sdl3.cpp +) +target_link_libraries(imgui_backend_sdl3 PUBLIC imgui SDL3::SDL3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 641ad36..3e55b93 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,7 +18,8 @@ target_link_libraries(tomato PUBLIC SDL3::SDL3 - #imgui - #imgui_backend_opengl3 + imgui + imgui_backend_sdl3 + imgui_backend_sdlrenderer3 ) diff --git a/src/main.cpp b/src/main.cpp index 464cb36..5ec945f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,12 @@ -#include "SDL_video.h" #include #include +#include +#include +#include + +#include "./theme.hpp" + #include #include #include @@ -14,7 +19,6 @@ int main(int argc, char** argv) { // me just messing with RAII cleanup auto sdl_scope = std::async(std::launch::deferred, &SDL_Quit); - // more RAII std::unique_ptr window { SDL_CreateWindow("tomato", 640, 480, SDL_WINDOW_RESIZABLE), @@ -26,17 +30,26 @@ int main(int argc, char** argv) { return 1; } - // more RAII std::unique_ptr renderer { SDL_CreateRenderer(window.get(), nullptr, 0), &SDL_DestroyRenderer }; - - if (!window) { - std::cerr << "SDL_CreateWindow failed (" << SDL_GetError() << ")\n"; + if (!renderer) { + std::cerr << "SDL_CreateRenderer failed (" << SDL_GetError() << ")\n"; return 1; } + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + + //ImGui::StyleColorsDark(); + setThemeGreen(); + + ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get()); + auto imgui_sdl_scope = std::async(std::launch::deferred, &ImGui_ImplSDL3_Shutdown); + ImGui_ImplSDLRenderer3_Init(renderer.get()); + auto imgui_sdlrenderer_scope = std::async(std::launch::deferred, &ImGui_ImplSDLRenderer3_Shutdown); + bool quit = false; while (!quit) { SDL_Event event; @@ -45,19 +58,25 @@ int main(int argc, char** argv) { quit = true; break; } + ImGui_ImplSDL3_ProcessEvent(&event); } if (quit) { break; } - SDL_SetRenderDrawColor(renderer.get(), 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE); - SDL_RenderClear(renderer.get()); + ImGui_ImplSDLRenderer3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + ImGui::NewFrame(); - SDL_FRect rect{100, 100, 100, 100}; - SDL_SetRenderDrawColor(renderer.get(), 0xff, 0x00, 0x00, SDL_ALPHA_OPAQUE); - SDL_RenderRect(renderer.get(), &rect); + ImGui::ShowDemoWindow(); + + ImGui::Render(); + ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData()); SDL_RenderPresent(renderer.get()); + // clearing after present is (should) more performant, but first frame is a mess + SDL_SetRenderDrawColor(renderer.get(), 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE); + SDL_RenderClear(renderer.get()); } return 0; diff --git a/src/theme.hpp b/src/theme.hpp new file mode 100644 index 0000000..6741ed2 --- /dev/null +++ b/src/theme.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include + +inline void setThemeGreen(void) { + ImVec4* colors = ImGui::GetStyle().Colors; + colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); + colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); + colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); + colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); + colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_FrameBg] = ImVec4(0.23f, 0.48f, 0.16f, 0.54f); + colors[ImGuiCol_FrameBgHovered] = ImVec4(0.41f, 0.98f, 0.26f, 0.40f); + colors[ImGuiCol_FrameBgActive] = ImVec4(0.41f, 0.98f, 0.26f, 0.67f); + colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); + colors[ImGuiCol_TitleBgActive] = ImVec4(0.23f, 0.48f, 0.16f, 1.00f); + colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); + colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); + colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); + colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); + colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); + colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); + colors[ImGuiCol_CheckMark] = ImVec4(0.41f, 0.98f, 0.26f, 1.00f); + colors[ImGuiCol_SliderGrab] = ImVec4(0.38f, 0.88f, 0.24f, 1.00f); + colors[ImGuiCol_SliderGrabActive] = ImVec4(0.41f, 0.98f, 0.26f, 1.00f); + colors[ImGuiCol_Button] = ImVec4(0.41f, 0.98f, 0.26f, 0.40f); + colors[ImGuiCol_ButtonHovered] = ImVec4(0.41f, 0.98f, 0.26f, 1.00f); + colors[ImGuiCol_ButtonActive] = ImVec4(0.26f, 0.98f, 0.06f, 1.00f); + //colors[ImGuiCol_Header] = ImVec4(0.41f, 0.98f, 0.26f, 0.31f); + colors[ImGuiCol_Header] = ImVec4(0.41f, 0.98f, 0.26f, 0.22f); + //colors[ImGuiCol_HeaderHovered] = ImVec4(0.41f, 0.98f, 0.26f, 0.80f); + //colors[ImGuiCol_HeaderActive] = ImVec4(0.41f, 0.98f, 0.26f, 1.00f); + colors[ImGuiCol_HeaderHovered] = ImVec4(0.41f, 0.98f, 0.26f, 0.68f); + colors[ImGuiCol_HeaderActive] = ImVec4(0.41f, 0.98f, 0.26f, 0.87f); + colors[ImGuiCol_Separator] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); + colors[ImGuiCol_SeparatorHovered] = ImVec4(0.24f, 0.75f, 0.10f, 0.78f); + colors[ImGuiCol_SeparatorActive] = ImVec4(0.24f, 0.75f, 0.10f, 1.00f); + colors[ImGuiCol_ResizeGrip] = ImVec4(0.41f, 0.98f, 0.26f, 0.20f); + colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.41f, 0.98f, 0.26f, 0.67f); + colors[ImGuiCol_ResizeGripActive] = ImVec4(0.41f, 0.98f, 0.26f, 0.95f); + colors[ImGuiCol_Tab] = ImVec4(0.26f, 0.58f, 0.18f, 0.86f); + colors[ImGuiCol_TabHovered] = ImVec4(0.41f, 0.98f, 0.26f, 0.80f); + colors[ImGuiCol_TabActive] = ImVec4(0.30f, 0.68f, 0.20f, 1.00f); + colors[ImGuiCol_TabUnfocused] = ImVec4(0.09f, 0.15f, 0.07f, 0.97f); + colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.20f, 0.42f, 0.14f, 1.00f); + colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); + colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); + colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f); + colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + colors[ImGuiCol_TableHeaderBg] = ImVec4(0.19f, 0.19f, 0.20f, 1.00f); + colors[ImGuiCol_TableBorderStrong] = ImVec4(0.31f, 0.31f, 0.35f, 1.00f); + colors[ImGuiCol_TableBorderLight] = ImVec4(0.23f, 0.23f, 0.25f, 1.00f); + colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f); + colors[ImGuiCol_TextSelectedBg] = ImVec4(0.41f, 0.98f, 0.26f, 0.35f); + colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); + colors[ImGuiCol_NavHighlight] = ImVec4(0.41f, 0.98f, 0.26f, 1.00f); + colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); + colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); + colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); +} +