wire up imgui

This commit is contained in:
Green Sky 2023-07-26 01:11:17 +02:00
parent f66b8d3ea2
commit 07765b4ad7
No known key found for this signature in database
5 changed files with 146 additions and 13 deletions

View File

@ -13,4 +13,5 @@ add_subdirectory(./solanaceae_toxcore)
add_subdirectory(./solanaceae_tox) add_subdirectory(./solanaceae_tox)
add_subdirectory(./sdl) add_subdirectory(./sdl)
add_subdirectory(./imgui)

48
external/imgui/CMakeLists.txt vendored Normal file
View File

@ -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)

View File

@ -18,7 +18,8 @@ target_link_libraries(tomato PUBLIC
SDL3::SDL3 SDL3::SDL3
#imgui imgui
#imgui_backend_opengl3 imgui_backend_sdl3
imgui_backend_sdlrenderer3
) )

View File

@ -1,7 +1,12 @@
#include "SDL_video.h"
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <imgui/imgui.h>
#include <imgui/backends/imgui_impl_sdl3.h>
#include <imgui/backends/imgui_impl_sdlrenderer3.h>
#include "./theme.hpp"
#include <memory> #include <memory>
#include <future> #include <future>
#include <iostream> #include <iostream>
@ -14,7 +19,6 @@ int main(int argc, char** argv) {
// me just messing with RAII cleanup // me just messing with RAII cleanup
auto sdl_scope = std::async(std::launch::deferred, &SDL_Quit); auto sdl_scope = std::async(std::launch::deferred, &SDL_Quit);
// more RAII // more RAII
std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window { std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window {
SDL_CreateWindow("tomato", 640, 480, SDL_WINDOW_RESIZABLE), SDL_CreateWindow("tomato", 640, 480, SDL_WINDOW_RESIZABLE),
@ -26,17 +30,26 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
// more RAII
std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> renderer { std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> renderer {
SDL_CreateRenderer(window.get(), nullptr, 0), SDL_CreateRenderer(window.get(), nullptr, 0),
&SDL_DestroyRenderer &SDL_DestroyRenderer
}; };
if (!renderer) {
if (!window) { std::cerr << "SDL_CreateRenderer failed (" << SDL_GetError() << ")\n";
std::cerr << "SDL_CreateWindow failed (" << SDL_GetError() << ")\n";
return 1; 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; bool quit = false;
while (!quit) { while (!quit) {
SDL_Event event; SDL_Event event;
@ -45,19 +58,25 @@ int main(int argc, char** argv) {
quit = true; quit = true;
break; break;
} }
ImGui_ImplSDL3_ProcessEvent(&event);
} }
if (quit) { if (quit) {
break; break;
} }
SDL_SetRenderDrawColor(renderer.get(), 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE); ImGui_ImplSDLRenderer3_NewFrame();
SDL_RenderClear(renderer.get()); ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
SDL_FRect rect{100, 100, 100, 100}; ImGui::ShowDemoWindow();
SDL_SetRenderDrawColor(renderer.get(), 0xff, 0x00, 0x00, SDL_ALPHA_OPAQUE);
SDL_RenderRect(renderer.get(), &rect); ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(renderer.get()); 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; return 0;

64
src/theme.hpp Normal file
View File

@ -0,0 +1,64 @@
#pragma once
#include <imgui/imgui.h>
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);
}