2023-02-03 23:33:37 +01:00
|
|
|
#include <nng/nng.h>
|
2023-02-12 04:47:48 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <backends/imgui_impl_sdl.h>
|
|
|
|
#include <backends/imgui_impl_sdlrenderer.h>
|
2023-02-12 03:32:02 +01:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
#include "dp_common.h"
|
2023-02-12 15:48:15 +01:00
|
|
|
#include "dp_util.hpp"
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
void dp_sdl_fatal(const char *const msg)
|
|
|
|
{
|
2023-02-12 04:47:48 +01:00
|
|
|
log_fatal("%s: %s", msg, SDL_GetError());
|
2023-02-12 03:32:02 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
struct DoomState
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(std::is_trivial<DoomState>::value, "DoomState must be a trivial type");
|
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
struct ControllerContext
|
|
|
|
{
|
|
|
|
nng_socket pub;
|
|
|
|
nng_socket sub;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
2023-02-12 15:48:15 +01:00
|
|
|
std::vector<DoomState> dooms;
|
|
|
|
bool quit = false;
|
2023-02-12 13:40:41 +01:00
|
|
|
};
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
int doom_controller_loop(ControllerContext *ctx)
|
2023-02-03 23:33:37 +01:00
|
|
|
{
|
2023-02-12 13:40:41 +01:00
|
|
|
static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
2023-02-12 04:47:48 +01:00
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
while (!ctx->quit)
|
2023-02-12 03:32:02 +01:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
2023-02-12 04:47:48 +01:00
|
|
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
if (event.type == SDL_QUIT)
|
2023-02-12 15:48:15 +01:00
|
|
|
ctx->quit = true;
|
2023-02-12 13:40:41 +01:00
|
|
|
|
|
|
|
if (event.type == SDL_WINDOWEVENT
|
|
|
|
&& event.window.event == SDL_WINDOWEVENT_CLOSE
|
|
|
|
&& event.window.windowID == SDL_GetWindowID(ctx->window))
|
|
|
|
{
|
2023-02-12 15:48:15 +01:00
|
|
|
ctx->quit = true;
|
2023-02-12 13:40:41 +01:00
|
|
|
}
|
2023-02-12 03:32:02 +01:00
|
|
|
}
|
2023-02-12 04:47:48 +01:00
|
|
|
|
|
|
|
// Start the Dear ImGui frame
|
|
|
|
ImGui_ImplSDLRenderer_NewFrame();
|
|
|
|
ImGui_ImplSDL2_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
// Do ImGui things here
|
|
|
|
show_ui(ctx);
|
|
|
|
|
2023-02-12 04:47:48 +01:00
|
|
|
// Rendering
|
|
|
|
ImGui::Render();
|
2023-02-12 15:48:15 +01:00
|
|
|
auto [r, g, b, a] = imvec4_to_rgba(clear_color);
|
|
|
|
SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
|
2023-02-12 13:40:41 +01:00
|
|
|
SDL_RenderClear(ctx->renderer);
|
2023-02-12 04:47:48 +01:00
|
|
|
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
2023-02-12 13:40:41 +01:00
|
|
|
SDL_RenderPresent(ctx->renderer);
|
2023-02-12 03:32:02 +01:00
|
|
|
}
|
2023-02-11 23:52:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
|
|
|
log_info("doompanning ctrl starting");
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
|
|
|
|
dp_sdl_fatal("SDL_Init");
|
|
|
|
|
|
|
|
#ifdef SDL_HINT_IME_SHOW_UI
|
|
|
|
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const auto windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
|
|
|
|
auto window = SDL_CreateWindow("doompanning", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, windowFlags);
|
|
|
|
if (!window)
|
|
|
|
dp_sdl_fatal("SDL_CreateWindow");
|
|
|
|
|
|
|
|
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
|
|
|
|
|
|
|
if (!renderer)
|
|
|
|
dp_sdl_fatal("SDL_CreateRenderer");
|
|
|
|
|
2023-02-12 04:47:48 +01:00
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
2023-02-12 15:48:15 +01:00
|
|
|
ImGui::GetIO().IniFilename = "doompanning_ui.ini";
|
2023-02-12 04:47:48 +01:00
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
|
|
|
ImGui_ImplSDLRenderer_Init(renderer);
|
2023-02-12 03:32:02 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
dp_nng_init_limits(1, 1, 1);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
auto pubSock = make_ctrl_pub(CtrlUrl);
|
|
|
|
auto subSock = make_ctrl_sub(DoomUrl);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
ControllerContext ctx
|
2023-02-04 01:56:30 +01:00
|
|
|
{
|
2023-02-12 13:40:41 +01:00
|
|
|
pubSock,
|
|
|
|
subSock,
|
|
|
|
window,
|
2023-02-12 15:48:15 +01:00
|
|
|
renderer,
|
|
|
|
{}
|
2023-02-12 13:40:41 +01:00
|
|
|
};
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
int ret = doom_controller_loop(&ctx);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
nng_close(pubSock);
|
|
|
|
nng_close(subSock);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
return ret;
|
|
|
|
}
|