diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d028b30..239fb56 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,18 +22,20 @@ target_compile_features(doompanning PRIVATE cxx_std_17) target_link_libraries(doompanning PRIVATE dp_common PRIVATE imgui - ) +) add_executable(dp_imgui_demo dp_imgui_demo.cc) target_compile_features(dp_imgui_demo PRIVATE cxx_std_17) target_link_libraries(dp_imgui_demo PRIVATE dp_common PRIVATE imgui - ) +) add_executable(dp_common_c_test dp_common_c_test.c) target_compile_features(dp_common_c_test PRIVATE c_std_11) -target_link_libraries(dp_common_c_test PRIVATE dp_common) +target_link_libraries(dp_common_c_test + PRIVATE dp_common +) add_executable(doomsim doomsim.cc) target_compile_features(doomsim PRIVATE cxx_std_17) diff --git a/src/doompanning.cc b/src/doompanning.cc index 857f390..5f257a9 100644 --- a/src/doompanning.cc +++ b/src/doompanning.cc @@ -1,12 +1,15 @@ -#include #include - #include #include #include #include +#include +#include +#include + #include "dp_common.h" +#include "dp_util.hpp" void dp_sdl_fatal(const char *const msg) { @@ -14,21 +17,80 @@ void dp_sdl_fatal(const char *const msg) abort(); } +struct DoomState +{ +}; + +static_assert(std::is_trivial::value, "DoomState must be a trivial type"); + struct ControllerContext { nng_socket pub; nng_socket sub; SDL_Window *window; SDL_Renderer *renderer; + std::vector dooms; + bool quit = false; }; +void show_ui(ControllerContext *ctx) +{ + const ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 20, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver); + ImGui::SetNextWindowSize(ImVec2(550, 340), ImGuiCond_FirstUseEver); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar; + +#ifndef IMGUI_DISABLE_DEBUG_TOOLS + const bool has_debug_tools = true; +#else + const bool has_debug_tools = false; +#endif + static bool show_app_metrics = false; + static bool show_app_debug_log = false; + + if (show_app_metrics) + ImGui::ShowMetricsWindow(&show_app_metrics); + if (show_app_debug_log) + ImGui::ShowDebugLogWindow(&show_app_debug_log); + + // Main body of the Demo window starts here. + if (!ImGui::Begin("doompanning controls", nullptr, window_flags)) + { + // Early out if the window is collapsed, as an optimization. + ImGui::End(); + return; + } + + ImGui::PushItemWidth(ImGui::GetFontSize() * -12); + + // Menu Bar + if (ImGui::BeginMenuBar()) + { + if (ImGui::BeginMenu("Menu")) + { + ImGui::MenuItem("Quit", "Ctrl+Q", &ctx->quit, true); + ImGui::EndMenu(); + } + + if (ImGui::BeginMenu("Tools")) + { + ImGui::MenuItem("Dear ImGui Metrics/Debugger", NULL, &show_app_metrics, has_debug_tools); + ImGui::MenuItem("Dear ImGui Debug Log", NULL, &show_app_debug_log, has_debug_tools); + ImGui::EndMenu(); + } + + ImGui::EndMenuBar(); + } + + ImGui::PopItemWidth(); + ImGui::End(); +} + int doom_controller_loop(ControllerContext *ctx) { static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); - bool done = false; - - while (!done) + while (!ctx->quit) { SDL_Event event; while (SDL_PollEvent(&event)) @@ -36,13 +98,13 @@ int doom_controller_loop(ControllerContext *ctx) ImGui_ImplSDL2_ProcessEvent(&event); if (event.type == SDL_QUIT) - done = true; + ctx->quit = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(ctx->window)) { - done = true; + ctx->quit = true; } } @@ -51,10 +113,13 @@ int doom_controller_loop(ControllerContext *ctx) ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); + // Do ImGui things here + show_ui(ctx); + // Rendering ImGui::Render(); - SDL_SetRenderDrawColor(ctx->renderer, - (u8)(clear_color.x * 255), (u8)(clear_color.y * 255), (u8)(clear_color.z * 255), (u8)(clear_color.w * 255)); + auto [r, g, b, a] = imvec4_to_rgba(clear_color); + SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a); SDL_RenderClear(ctx->renderer); ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData()); SDL_RenderPresent(ctx->renderer); @@ -89,6 +154,7 @@ int main(int argc, char *argv[]) IMGUI_CHECKVERSION(); ImGui::CreateContext(); + ImGui::GetIO().IniFilename = "doompanning_ui.ini"; ImGui::StyleColorsDark(); ImGui_ImplSDL2_InitForSDLRenderer(window, renderer); ImGui_ImplSDLRenderer_Init(renderer); @@ -103,7 +169,8 @@ int main(int argc, char *argv[]) pubSock, subSock, window, - renderer + renderer, + {} }; int ret = doom_controller_loop(&ctx); diff --git a/src/doomsim.cc b/src/doomsim.cc index db4a950..d38335d 100644 --- a/src/doomsim.cc +++ b/src/doomsim.cc @@ -7,23 +7,6 @@ #include #include "dp_common.h" -enum DP_DoomState -{ - DP_DS_Ready, - DP_DS_Running, - DP_DS_Quit, - DP_DS_COUNT, -}; - -static const char *const DP_DoomState_Strings[DP_DS_COUNT] = -{ - "DoomState_Ready", - "DoomState_Running", - "DoomState_Quit", -}; - -#define doomstate_str(s) DP_DoomState_Strings[s] - typedef struct DoomContext DoomContext; #define DEF_DOOM_STATE_FUNC(fn) int fn(DoomContext *ctx) diff --git a/src/dp_common.c b/src/dp_common.c index f4a4c23..c109def 100644 --- a/src/dp_common.c +++ b/src/dp_common.c @@ -95,3 +95,10 @@ nng_socket make_doom_sub(const char *url) return sock; } + +const char *const DP_DoomState_Strings[DP_DS_COUNT] = +{ + "DoomState_Ready", + "DoomState_Running", + "DoomState_Quit", +}; \ No newline at end of file diff --git a/src/dp_common.h b/src/dp_common.h index 1ba9db1..91c40b0 100644 --- a/src/dp_common.h +++ b/src/dp_common.h @@ -28,15 +28,29 @@ static const char *const DoomUrl = "ipc://666_doom.socket"; // dooms publish her typedef u32 doomid_t; // unique id for each doom instance typedef u16 dmt_t; // for DP_MessageType values -enum DP_MessageType +typedef enum DP_MessageType { DP_MT_Invalid, DP_MT_DoomReady, DP_MT_RunDoom, DP_MT_QuitDoom, DP_MT_COUNT -}; +} DP_MessageType; +typedef enum DP_DoomState +{ + DP_DS_Ready, + DP_DS_Running, + DP_DS_Quit, + DP_DS_COUNT, +} DP_DoomState; + +extern const char *const DP_DoomState_Strings[DP_DS_COUNT]; + +static inline const char *doomstate_str(DP_DoomState ds) +{ + return DP_DoomState_Strings[ds]; +} #ifdef __cplusplus } diff --git a/src/dp_common_c_test.c b/src/dp_common_c_test.c index b94dbe6..dc6d3a9 100644 --- a/src/dp_common_c_test.c +++ b/src/dp_common_c_test.c @@ -5,5 +5,8 @@ int main(int argc, char *argv[]) (void) argc; (void) argv; log_info("Hello from C11!"); + + for (int i=0; i +#include +#include "dp_types.h" + +inline std::tuple imvec4_to_rgba(const ImVec4 &color) +{ + return + { + color.x * 255, + color.y * 255, + color.z * 255, + color.w * 255, + }; +} + +#endif // SRC_DP_UTIL_HPP \ No newline at end of file