First steps with Dear ImGui. It's awesome!
This commit is contained in:
parent
0f83e60fc5
commit
1674946bed
7 changed files with 127 additions and 32 deletions
|
@ -22,18 +22,20 @@ target_compile_features(doompanning PRIVATE cxx_std_17)
|
||||||
target_link_libraries(doompanning
|
target_link_libraries(doompanning
|
||||||
PRIVATE dp_common
|
PRIVATE dp_common
|
||||||
PRIVATE imgui
|
PRIVATE imgui
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(dp_imgui_demo dp_imgui_demo.cc)
|
add_executable(dp_imgui_demo dp_imgui_demo.cc)
|
||||||
target_compile_features(dp_imgui_demo PRIVATE cxx_std_17)
|
target_compile_features(dp_imgui_demo PRIVATE cxx_std_17)
|
||||||
target_link_libraries(dp_imgui_demo
|
target_link_libraries(dp_imgui_demo
|
||||||
PRIVATE dp_common
|
PRIVATE dp_common
|
||||||
PRIVATE imgui
|
PRIVATE imgui
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(dp_common_c_test dp_common_c_test.c)
|
add_executable(dp_common_c_test dp_common_c_test.c)
|
||||||
target_compile_features(dp_common_c_test PRIVATE c_std_11)
|
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)
|
add_executable(doomsim doomsim.cc)
|
||||||
target_compile_features(doomsim PRIVATE cxx_std_17)
|
target_compile_features(doomsim PRIVATE cxx_std_17)
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
#include <cstdlib>
|
|
||||||
#include <nng/nng.h>
|
#include <nng/nng.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
#include <backends/imgui_impl_sdl.h>
|
#include <backends/imgui_impl_sdl.h>
|
||||||
#include <backends/imgui_impl_sdlrenderer.h>
|
#include <backends/imgui_impl_sdlrenderer.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "dp_common.h"
|
#include "dp_common.h"
|
||||||
|
#include "dp_util.hpp"
|
||||||
|
|
||||||
void dp_sdl_fatal(const char *const msg)
|
void dp_sdl_fatal(const char *const msg)
|
||||||
{
|
{
|
||||||
|
@ -14,21 +17,80 @@ void dp_sdl_fatal(const char *const msg)
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DoomState
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
static_assert(std::is_trivial<DoomState>::value, "DoomState must be a trivial type");
|
||||||
|
|
||||||
struct ControllerContext
|
struct ControllerContext
|
||||||
{
|
{
|
||||||
nng_socket pub;
|
nng_socket pub;
|
||||||
nng_socket sub;
|
nng_socket sub;
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer;
|
||||||
|
std::vector<DoomState> 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)
|
int doom_controller_loop(ControllerContext *ctx)
|
||||||
{
|
{
|
||||||
static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||||
|
|
||||||
bool done = false;
|
while (!ctx->quit)
|
||||||
|
|
||||||
while (!done)
|
|
||||||
{
|
{
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event))
|
while (SDL_PollEvent(&event))
|
||||||
|
@ -36,13 +98,13 @@ int doom_controller_loop(ControllerContext *ctx)
|
||||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||||
|
|
||||||
if (event.type == SDL_QUIT)
|
if (event.type == SDL_QUIT)
|
||||||
done = true;
|
ctx->quit = true;
|
||||||
|
|
||||||
if (event.type == SDL_WINDOWEVENT
|
if (event.type == SDL_WINDOWEVENT
|
||||||
&& event.window.event == SDL_WINDOWEVENT_CLOSE
|
&& event.window.event == SDL_WINDOWEVENT_CLOSE
|
||||||
&& event.window.windowID == SDL_GetWindowID(ctx->window))
|
&& 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_ImplSDL2_NewFrame();
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
|
|
||||||
|
// Do ImGui things here
|
||||||
|
show_ui(ctx);
|
||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
SDL_SetRenderDrawColor(ctx->renderer,
|
auto [r, g, b, a] = imvec4_to_rgba(clear_color);
|
||||||
(u8)(clear_color.x * 255), (u8)(clear_color.y * 255), (u8)(clear_color.z * 255), (u8)(clear_color.w * 255));
|
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
|
||||||
SDL_RenderClear(ctx->renderer);
|
SDL_RenderClear(ctx->renderer);
|
||||||
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
||||||
SDL_RenderPresent(ctx->renderer);
|
SDL_RenderPresent(ctx->renderer);
|
||||||
|
@ -89,6 +154,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
|
ImGui::GetIO().IniFilename = "doompanning_ui.ini";
|
||||||
ImGui::StyleColorsDark();
|
ImGui::StyleColorsDark();
|
||||||
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
||||||
ImGui_ImplSDLRenderer_Init(renderer);
|
ImGui_ImplSDLRenderer_Init(renderer);
|
||||||
|
@ -103,7 +169,8 @@ int main(int argc, char *argv[])
|
||||||
pubSock,
|
pubSock,
|
||||||
subSock,
|
subSock,
|
||||||
window,
|
window,
|
||||||
renderer
|
renderer,
|
||||||
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
int ret = doom_controller_loop(&ctx);
|
int ret = doom_controller_loop(&ctx);
|
||||||
|
|
|
@ -7,23 +7,6 @@
|
||||||
#include <nng/protocol/pubsub0/sub.h>
|
#include <nng/protocol/pubsub0/sub.h>
|
||||||
#include "dp_common.h"
|
#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;
|
typedef struct DoomContext DoomContext;
|
||||||
|
|
||||||
#define DEF_DOOM_STATE_FUNC(fn) int fn(DoomContext *ctx)
|
#define DEF_DOOM_STATE_FUNC(fn) int fn(DoomContext *ctx)
|
||||||
|
|
|
@ -95,3 +95,10 @@ nng_socket make_doom_sub(const char *url)
|
||||||
|
|
||||||
return sock;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *const DP_DoomState_Strings[DP_DS_COUNT] =
|
||||||
|
{
|
||||||
|
"DoomState_Ready",
|
||||||
|
"DoomState_Running",
|
||||||
|
"DoomState_Quit",
|
||||||
|
};
|
|
@ -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 u32 doomid_t; // unique id for each doom instance
|
||||||
typedef u16 dmt_t; // for DP_MessageType values
|
typedef u16 dmt_t; // for DP_MessageType values
|
||||||
|
|
||||||
enum DP_MessageType
|
typedef enum DP_MessageType
|
||||||
{
|
{
|
||||||
DP_MT_Invalid,
|
DP_MT_Invalid,
|
||||||
DP_MT_DoomReady,
|
DP_MT_DoomReady,
|
||||||
DP_MT_RunDoom,
|
DP_MT_RunDoom,
|
||||||
DP_MT_QuitDoom,
|
DP_MT_QuitDoom,
|
||||||
DP_MT_COUNT
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,8 @@ int main(int argc, char *argv[])
|
||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
log_info("Hello from C11!");
|
log_info("Hello from C11!");
|
||||||
|
|
||||||
|
for (int i=0; i<DP_DS_COUNT; ++i)
|
||||||
|
log_info("doomstate_str: %d=%s", i, doomstate_str(i));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
19
src/dp_util.hpp
Normal file
19
src/dp_util.hpp
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef SRC_DP_UTIL_HPP
|
||||||
|
#define SRC_DP_UTIL_HPP
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <tuple>
|
||||||
|
#include "dp_types.h"
|
||||||
|
|
||||||
|
inline std::tuple<u8, u8, u8, u8> imvec4_to_rgba(const ImVec4 &color)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
{
|
||||||
|
color.x * 255,
|
||||||
|
color.y * 255,
|
||||||
|
color.z * 255,
|
||||||
|
color.w * 255,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SRC_DP_UTIL_HPP
|
Loading…
Reference in a new issue