2023-02-03 23:33:37 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <nng/nng.h>
|
2023-02-12 03:32:02 +01:00
|
|
|
|
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-08 06:39:23 +01:00
|
|
|
#include "dp_common.h"
|
|
|
|
#include "log.h"
|
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-08 06:39:23 +01:00
|
|
|
static const size_t NumDooms = 3;
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
int doom_controller_loop(nng_socket pubSock, nng_socket subSock, SDL_Window *window, SDL_Renderer *renderer)
|
2023-02-03 23:33:37 +01:00
|
|
|
{
|
2023-02-12 04:47:48 +01:00
|
|
|
bool show_demo_window = true;
|
|
|
|
bool show_another_window = false;
|
|
|
|
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
bool done = false;
|
|
|
|
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
done = true;
|
|
|
|
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
|
|
|
|
done = true;
|
|
|
|
}
|
2023-02-12 04:47:48 +01:00
|
|
|
|
|
|
|
// Start the Dear ImGui frame
|
|
|
|
ImGui_ImplSDLRenderer_NewFrame();
|
|
|
|
ImGui_ImplSDL2_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
|
|
if (show_demo_window)
|
|
|
|
ImGui::ShowDemoWindow(&show_demo_window);
|
|
|
|
|
|
|
|
// Rendering
|
|
|
|
ImGui::Render();
|
|
|
|
SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
SDL_RenderPresent(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");
|
|
|
|
|
|
|
|
dp_nng_init_limits(1, 1, 1);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
|
|
|
auto pubSock = make_ctrl_pub(CtrlUrl);
|
|
|
|
auto subSock = make_ctrl_sub(DoomUrl);
|
|
|
|
|
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();
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
|
|
|
ImGui_ImplSDLRenderer_Init(renderer);
|
2023-02-12 03:32:02 +01:00
|
|
|
|
|
|
|
int ret = doom_controller_loop(pubSock, subSock, window, renderer);
|
2023-02-11 23:52:20 +01:00
|
|
|
|
|
|
|
nng_close(pubSock);
|
|
|
|
nng_close(subSock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// doomctrl test code
|
|
|
|
#if 0
|
2023-02-04 01:56:30 +01:00
|
|
|
size_t readyCount = 0;
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("ctrl waiting for hello from dooms");
|
2023-02-04 01:56:30 +01:00
|
|
|
while (readyCount < numDooms)
|
|
|
|
{
|
|
|
|
char *buf = nullptr;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
|
|
|
if (auto res = nng_recv(subSock, &buf, &sz, NNG_FLAG_ALLOC))
|
|
|
|
dp_nng_fatal("run_ctrl/sub/go", res);
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("ctrl received %zu bytes from a doom", sz);
|
2023-02-04 01:56:30 +01:00
|
|
|
nng_free(buf, sz);
|
|
|
|
++readyCount;
|
|
|
|
}
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("ctrl is unleashing dooms");
|
2023-02-04 01:56:30 +01:00
|
|
|
|
|
|
|
if (auto res = nng_send(pubSock, (void *)&readyCount, sizeof(readyCount), 0))
|
|
|
|
dp_nng_fatal("run_ctrl/pub/unleash", res);
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
#endif
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
// doomsim test code
|
|
|
|
#if 0
|
2023-02-04 01:56:30 +01:00
|
|
|
while (true) // FIXME: ctrl code does not work this way
|
|
|
|
{
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom%zu sending hello", id);
|
2023-02-04 01:56:30 +01:00
|
|
|
if (auto res = nng_send(pubSock, (void *)&id, sizeof(id), 0))
|
|
|
|
dp_nng_fatal("run_doom/pub/hello", res);
|
|
|
|
|
|
|
|
{
|
|
|
|
char *buf = nullptr;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom%zu waiting for unleash", id);
|
2023-02-04 01:56:30 +01:00
|
|
|
auto res = nng_recv(subSock, &buf, &sz, NNG_FLAG_ALLOC | NNG_FLAG_NONBLOCK);
|
|
|
|
|
|
|
|
if (res == NNG_EAGAIN)
|
|
|
|
{
|
|
|
|
usleep(10 * 1000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (res)
|
|
|
|
dp_nng_fatal("run_doom/sub/go", res);
|
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
log_debug("doom%zu unleashed by %zu bytes!", id, sz);
|
2023-02-04 01:56:30 +01:00
|
|
|
nng_free(buf, sz);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 04:47:48 +01:00
|
|
|
#endif
|